... and again.

Since I thought this must be my fault and I am surely missing something, I decided to ask Wikipedia. What I've found is this page:

http://en.wikipedia.org/wiki/IEEE_754-2008#Rounding_algorithms.

So IEEE defines two rounding algorithms (it actually defines more, but only these two are relevant for my case):

* Round to nearest, ties to even – rounds to the nearest value; if the number falls midway it is rounded to the nearest value with an even (zero) least significant bit, which occurs 50% of the time; this is the default algorithm for binary floating-point and the recommended default for decimal

* Round to nearest, ties away from zero – rounds to the nearest value; if the number falls midway it is rounded to the nearest value above (for positive numbers) or below (for negative numbers)

The results of rounding in VA are somewhat surprising (needless to say that Pharo delivers the very same results)

(0.015 asScaledDecimal: 3) roundTo: 0.01 --> 0.02

(1.015 asScaledDecimal: 3) roundTo: 0.01 --> 1.01

(2.015 asScaledDecimal: 3) roundTo: 0.01 --> 2.02

(3.015 asScaledDecimal: 3) roundTo: 0.01 --> 3.02

(4.015 asScaledDecimal: 3) roundTo: 0.01 --> 4.01

(5.015 asScaledDecimal: 3) roundTo: 0.01 --> 5.01

So at first sight, definition 2 is not the one that's implemented. (But it's the one I'd like to see here)

Then, it seems like I am bitten by the least significant bit stuff. Both 0.015 and 1.015 have their LSB set, so they have to be rounded to the nearest value with an unset LSB.

I could possibly live with this effect for Float numbers, but surely not for Scaled Decimals...

Joachim