On 2019-01-07 21:17, Legale.legale wrote:
> I think your solution by changing precision is not good enough because
> float summation is still not working properly.
>
> <?php
> var_dump(0.1 + 0.7);
>
> returns:
>
> 0.7999999999
>
> expected: 0.8
As you may know, most computers use binary numbers. This applies to
floating-point values as well. They have a limited number of binary
digits. Many base-10 numbers cannot be expressed exactly in base-2 with
limited number of digits. When you write 0.1 + 0.7, what you get (with
double-precision floats) is closer to 0.1000000000000000055511 +
0.6999999999999999555911 = 0.7999999999999999333866. This is expected
and proper and well known.
You have to use formatting functions like number_format if you need
neatly rounded base-10 output.
There are also a lot of libraries for precise base-10 calculations. They
will be a lot slower than native binary floating-point calculations,
though.
Regards,
Lauri Kenttä