(int)(314.96*1000)/10 => 31496
(int)(314.96*100) => 31495

#php <3

The Floating-Point Guide - What Every Programmer Should Know About Floating-Point Arithmetic

Aims to provide both short and simple answers to the common recurring questions of novice programmers about floating-point numbers not 'adding up' correctly, and more in-depth information about how IEEE 754 floats work, when and how to use them correctly, and what to use instead when they are not appropriate.

@connorhu Same happens #JavaScript and most other languages.

Floating point maths is hard, and that's why you round, rather than cast to int, which just cuts off decimals…

Yes, the first step to writing robust floating point code is to assume every floating point operation will have a small unpredictable rounding error. Next step is to consider what impact such a rounding error will have on your result.

And whenever turning a float into an int, one has to consider which rounding semantics you need.

Some languages have decimal and fraction numeric types which have different semantics from floating point but are also slower. I have seen people use floating point and expect the semantics of a decimal type.

Here is an example of behavior in Python:

>>> int((314.96*1000)/10) 31496 >>> int((314.96*100)) 31495 >>> import decimal >>> int((decimal.Decimal("314.96")*1000)/10) 31496 >>> int((decimal.Decimal("314.96")*100)) 31496 >>>
@connorhu It's unrelated to #JavaScript, but to IEEE 754 floating-point standard. #Ruby, #Python and #Java all evaluate `314.96 * 100` to `31495.999999999996`. #PHP as well, but by default the output is rounded, that's why you might get `31496`. To be honest, such behavior is much more surprising