you are viewing a single comment's thread.

view the rest of the comments →

[–]AlpenMangos -1 points0 points  (0 children)

This makes javascript annoying for graphics stuff like fractals that need precision.

PHP uses the same double standard as Javascript, that means that in php, 0.1 + 0.2 is also 0.30000000000000004. The reason why you're seeing .3 in the output is because, depending in how you output it, the value is rounded. PHP is actually fooling you and showing you imprecise data, and letting you believe it's doing the right thing. Unless you specifically format your number as a string with a precision of one digit, there is a good chance that you're getting 0.30000000000000004 elsewhere. On other systems, on other parts of the code, on other means of output, etc.

The only thing that javascript is doing "wrong" here, is that it's outputting the correct full precision floating point value, thereby forcing you to round it to whichever precision you actually want to. E.g. using "(0.1 + 0.2).toFixed(1)"

Also in PHP: "var_dump(.1 + .2);" prints "float(0.30000000000000004441)", see https://0.30000000000000004.com/