Question

I'm wondering why my floats are not rounding. When I do that :

$shipping_cost = float(57.599999999999994) 
round($shipping_cost, 2)

The result is : 57.600000000000001

Do you have any idea why this is so ? Does it has something to do with the server ?

Thank you

Was it helpful?

Solution

Although you did not specify what language or implementation you are using, it is almost certainly using the IEEE-754 64-bit binary floating-point format.

In this format, finite numbers are represented as an integer f from -253 to +253 (exclusive) and two raised to some power e in the range -1074 to 971 (inclusive). Every finite floating-point value has the form f•2e. (These parts are slightly altered when encoded for storage, but this description is mathematically equivalent.)

To represent a number, it is necessary to put it into this form. For example, .75 is 3•2-2.

The value 57.6 cannot be written exactly in this form. The closest possible value is 8106479329266893•2-47 = 8106479329266893 / 140737488355328 = 57.60000000000000142108547152020037174224853515625. (You cannot get any closer to 57.6 because adding +1 or -1 to the numerator moves the value farther away from 57.6 and adding one to the exponent would make the numerator exceed 253. So this is the closest you can get within the bounds of the floating-point format.)

So that is what round(57.599999999999994, 2) returns.

When it is printed, the software you are using omits some of the digits. It appears to be printing enough digits to uniquely distinguish the exact value (somebody who knows the floating-point format can use the digits shown to figure out the exact value) but not enough to show the exact value completely.

OTHER TIPS

This is happening due to the the way most languages represent floating point values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top