Skip to main content

$DOUBLE Numbers

$DOUBLE Numbers

$DOUBLE IEEE floating point numbers are encoded using binary notation. Most decimal fractions cannot be exactly represented in this binary notation. When a $DOUBLE value is input to $NUMBER with a rounding factor, the return value frequently contains more fractional digits than specified in the rounding factor. This is because the fractional decimal result is not representable in binary, so the return value must be rounded to the nearest representable $DOUBLE value, as shown in the following example:

  SET x=1234.5678
  SET y=$DOUBLE(1234.5678)
  WRITE "Decimal: ",x," rounded ",$NUMBER(x,2),!
  WRITE "Double: ",y," rounded ",$NUMBER(y,2)

When using $DOUBLE numbers, be aware that IEEE floating point numbers and standard InterSystems IRIS fractional numbers differ in precision. $DOUBLE IEEE floating point numbers are encoded using binary notation. They have a precision of 53 binary bits, which corresponds to 15.95 decimal digits of precision. (Note that the binary representation does not correspond exactly to a decimal fraction.) Because most decimal fractions cannot be exactly represented in this binary notation, an IEEE floating point number may differ slightly from the corresponding standard InterSystems IRIS floating point number. Standard InterSystems IRIS fractional numbers have a precision of 18 decimal digits on all supported InterSystems IRIS system platforms. When an IEEE floating point number is displayed as a fractional number, the binary bits are often converted to a fractional number with far more than 18 decimal digits. This does not mean that IEEE floating point numbers are more precise than standard InterSystems IRIS fractional numbers.

If you are using $NUMBER to round a $DOUBLE value and wish to return a specific number of fractional digits, you should convert the $DOUBLE value to decimal representation before rounding the result. For example:

USER>set mydouble=$double(33/100)
 
USER>write mydouble
.33000000000000001554
USER>w $number(mydouble,2)
.33000000000000001554
USER>set mydecimal=$decimal(mydouble)
 
USER>write $number(mydecimal,2)
.33

$NUMBER returns $DOUBLE("INF") or $DOUBLE("NAN") as the empty string.

FeedbackOpens in a new tab