"Rounding is used when the exact result of a floating-point operation (or a conversion to floating-point format) would need more digits than there are digits in the significand. There are several different rounding schemes (or rounding modes). Historically, truncation was the typical approach. Since the introduction of IEEE 754, the default method (round to nearest, ties to even, sometimes called Banker's Rounding) is more commonly used."
As for the original issue, how numbers ending in 0.05 are rounded for display with 1 digit after the decimal:
It does seem "inconsistent", but it isn't wrong. Both 32.1 and 32.0 are equally distant from 32.05. Internally, floating point numbers are represented as binary digits, so 32.05 isn't *exactly* 32.05, but the closest value that can be represented in binary, which will be ever so slightly higher or lower.
Try looking at the numbers with more decimal points and you'll see what I mean:
So what it's rounding isn't the exact number you're typing in, but the internal representation of that number in binary floating point format.
There are ways of tweaking the math so that the rounding seems more consistent, but no matter what, the values you enter won't be precisely equal to that value when you're using floating point numbers.
If you're not careful about rounding floating point digits, you can end up with something like this:
I did discover that there are various ways of rounding just before your
reply at Rounding - Wikipedia
I am writing a ruby web based unit testing for my java students to
submit their programs to.
My real problem is that sprintf in ruby behaves differently in this
regard to how java rounds.
Oh well, I fixed it by creating unit test data that was within the
precision of the final java output, so that rounding was not used in the
unit testing.
"Rounding is used when the exact result of a floating-point operation
(or a conversion to floating-point format) would need more digits than
there are digits in the significand. There are several different
rounding schemes (or rounding modes). Historically, truncation was the
typical approach. Since the introduction of IEEE 754, the default method
(round to nearest, ties to even, sometimes called Banker's Rounding) is
more commonly used."
As for the original issue, how numbers ending in 0.05 are rounded for
display with 1 digit after the decimal:
It does seem "inconsistent", but it isn't wrong. Both 32.1 and 32.0 are
equally distant from 32.05. Internally, floating point numbers are
represented as binary digits, so 32.05 isn't *exactly* 32.05, but the
closest value that can be represented in binary, which will be ever so
slightly higher or lower.
Try looking at the numbers with more decimal points and you'll see what
I mean:
So what it's rounding isn't the exact number you're typing in, but the
internal representation of that number in binary floating point format.
There are ways of tweaking the math so that the rounding seems more
consistent, but no matter what, the values you enter won't be precisely
equal to that value when you're using floating point numbers.
If you're not careful about rounding floating point digits, you can end
up with something like this:
Oh well, I fixed it by creating unit test data that was within the
precision of the final java output, so that rounding was not used in the
unit testing.
The proper solution IMHO would be a comparison of numeric values (i.e. not of strings) that takes the delta into account much the same way as it's done in JUnit because there is also named mismatch between the binary internal representation and the decimal external representation that we humans love to use.