Kevin Crays

Ranch Hand

Posts: 41

posted 11 years ago

I was playing around the other day and got some very strange floating point addition results. I have others, which now seem to produce the correct 9.3, but did not the other day (nothing changed, AFAIK, but I rebooted at some point).

anyway, these are the equations and the results. I think of any way that the first and last equation can have those results.

anyway, these are the equations and the results. I think of any way that the first and last equation can have those results.

double dd;

dd = (float) (4.1d + 5.2d)

dd = (double) (4.1d + 5.2d)

dd = 4.1d + 5.2f

double dd;

dd = (float) (4.1d + 5.2d)

**//= 9.300000190734863**dd = (double) (4.1d + 5.2d)

**//= 9.3**dd = 4.1d + 5.2f

**//= 9.299999809265136**We're binary code: a one and a zero<br />You wanted violins and you got Nero

posted 11 years ago

Floating-point arithmetic often provides inaccurate results. Google on floating-point arithmetic and you will find several thorough discussions of the issue.

Basically, the problem occurs as a result of representing decimal fractions in binary. Any fraction that is not a multiple of a negative power of two (.5, .25, .75, .125, etc.) cannot be represented accurately in a binary system. The approximation is usually close enough that after rounding, we're not aware of the discrepancy.

The BigDecimal class will solve the problem for you if you need absolute accuracy without spurious rounding errors.

Basically, the problem occurs as a result of representing decimal fractions in binary. Any fraction that is not a multiple of a negative power of two (.5, .25, .75, .125, etc.) cannot be represented accurately in a binary system. The approximation is usually close enough that after rounding, we're not aware of the discrepancy.

The BigDecimal class will solve the problem for you if you need absolute accuracy without spurious rounding errors.

SCJP, SCWCD

Edwin Dalorzo

Ranch Hand

Posts: 961

posted 11 years ago

Some fractions cannot be represented in decimal format due to its infinite notation. Like 1/3 which is 0.33333333...

The same happens with some binary numbers. When you conver 9.3 to binary, it is 1001.01001100110011001...

Hence, some operations may not yield an exact result. That's why you should use BigDecimal insteand of primitive floating point arithmethic if you expect to do operations with a more accurate result (like those related with money or astronomy).

[ April 28, 2006: Message edited by: Edwin Dalorzo ]

The same happens with some binary numbers. When you conver 9.3 to binary, it is 1001.01001100110011001...

Hence, some operations may not yield an exact result. That's why you should use BigDecimal insteand of primitive floating point arithmethic if you expect to do operations with a more accurate result (like those related with money or astronomy).

[ April 28, 2006: Message edited by: Edwin Dalorzo ]

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |