views:

312

answers:

3

Generally in programming, the floating point data types should not be compared for equality since the values stored are very often an approximation.

Can two non-integer Oracle NUMBER values be compared reliably for equality since they are stored differently (base-10)?

+2  A: 

Yes, Oracle NUMBER types are precise. They're more like integers with a scale than float/double types. So a NUMBER(10,3) has 10 digits, 3 after the decimal point, which is really a 10 digit integer with a scale of 3. In fact, that's precise how Java BigDecimals work (being a BigInteger plus a scale internally).

cletus
any doc avaible ?
Scorpi0
http://download.oracle.com/docs/cd/B19306_01/server.102/b14220/datatype.htm#i16209
cletus
No, they are not precise.
Quassnoi
If two NUMBER types contain a number such as 10.32 and you compare them, they will be equal and it won't matter how you get 10.32. This is different to floating point numbers. 0.1 + 0.1 + 0.1 + 0.1 + 0.1 = 0.5 in NUMBER types but not in floating point math necessary. That's what I mean by precise (assuming the NUMBER type has a scale of at least 1).
cletus
Quassnoi, please elaborate.
Steven
A: 

Oracle NUMBER types are stored as sets of centesimal digits (that is base 100, not base 10), one digit per byte.

The first byte represents the exponent, other bytes represent mantissa.

That means that for extremely large numbers, even the integer numbers can be rounded:

SELECT  10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 -
        10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001
FROM    dual


---
0
Quassnoi
A: 

Oracle guarantees 38 digits of precision in a NUMBER, although 40 can be represented. See Oracle Concepts for a reference.

DCookie