Comparing double and float values

Alexander aldem+dmars at nk7.net
Thu May 12 03:41:38 PDT 2011


On 12.05.2011 12:08, Matthew Ong wrote:

> Is there anyway to ensure the comparison of float/double to be done with the raw
> bits like in Java
> Double.doubleToRawLongBits(double value)

  The problem is that not every decimal value may be stored with the same bits in float or double, again, due to rounding and other errors.

  Try it for yourself: http://babbage.cs.qc.edu/IEEE-754/Decimal.html

  Sure, for simple cases, when no operations are performed and values are simply stored, doubles/floats *may* be sufficient, but anything more important - and errors will accumulate very quickly.

> when it comes to financial data and percentage computation, that seems to be rather important.

  When it comes to financial data, where you really need *exact* calculations within given precision, forget about standard floating point formats.

  Instead, you should use something like http://www.mpfr.org/#intro - with exact and guaranteed precision.

  Alternatively, you may use something like std.bignum in Phobos with artificial fixed point position (if you need the fast way to go with D).

/Alexander


More information about the Digitalmars-d mailing list