Is Ceil broken or what?

On reading the documentation for Ceil I came across the following:

It sounded like exactly what I was look for so, I grabbed the example code and start to play around with it, but it seems to be broken!

Using Xojo.Math

Dim d1, d2 As Double
Const kDecimalPlaces = 1

d1 = Ceil(1.2345 * (10 ^ kDecimalPlaces)) / (10 ^ kDecimalPlaces)
d2 = Ceil(301.4499999999999886 * (10 ^ kDecimalPlaces)) / (10 ^ kDecimalPlaces)

When I run this code with the decimal places constant set to 1, I get what I expected: d1 is set to 1.3 and d2 is set to 301.5. However if I change it to 2, I do not get what I expected: d1 is set to 1.24, but d2 remains unchanged at 301.4499999999999886. I even tried with 301.4499 and I still ended up with the same big bloody number!

So what am I doing wrong???

Nothing wrong. Test this:

Dim d As Double = 301.45

Double notes:
Double is an IEEE double-precision, floating-point value. This means it is speedy but has some limits for values it can contain. For more information, refer to the wikipedia page about floating point.

perhaps at some instant during those operations it was a “correct” value (based on your expectations), but because you are moving from double to double with a double divide… all bets are off… just a Alberto indicated.

I think that if you need exact decimals you could use Currency or Bob Delaney’s Decimal Plugin

Dim d2 As Currency Const kDecimalPlaces = 2 d2 = Ceil(301.4499999999999886 * (10 ^ kDecimalPlaces)) / (10 ^ kDecimalPlaces) // you get 301.45

Use Format with the correct number of decimal places when you display the value. Nothing is inherently wrong with your results, just be aware of the limitations of double and work with them.

formatstring = left("#.###############", kDecimalPlaces+2)
msgbox Format(d2, formatstring)

Other thing: after the XDC announcement I think is better just to use Ceil without Using Xojo.Math. Something about API 2.0

So basically it Currency’s four digits or a plug-in…