Do all the things like ++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatarSign Up
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple APILearn More
Cmon, everyone knows you don’t work with floats in JS, unless you really know what you’re doing
YouAllSuck1580158dIs that a real console ss ???!!
Why only on .14 ??
jak6452048158dWait what ! If that real its fucking idiots !
coffeeholic1755158dI had something like this with java's double
binary numbers are represented as a sum of fractions. this is defined by the IEEE754 standard.
I will provide a simplified explanation of why you can't essentially get 0.14 exactly:
for example, you can represent the numbers 0 and 0.5 with a single bit
0 = nothing = 0
1 = 1 / (2^1) = 0.5
by adding more bits, you can represent more numbers!
00 = nothing = 0
10 = 1 / (2^1) = 0.5
01 = 1 / (2^2) = 0.25
11 = 1 / (2^1) + 1 / (2^2) = 0.75
so with 2 bits, we can represent whole 4 floating-point numbers!... but notice... that's not all of them!. you can keep adding bits and floating points are represented with 23 bits (the other 9 are reserved for the integer part and sign)
that's a whole lot of fractions you can represent with 23 bits! 8388608 to be precise... and 0.14 isn't one of them! but 0.14000000059604644775390625
but a 64bit double has 52bit fraction part, which allows it to go as precise as 0.140000000000000017763568394002504646778106689453125
which is rounded into 14.000000000000002 after multiplied by 100 because the computer is thinking, that the number is significant enough to show to the user in a print now... since you moved by 2 spaces when you multiplied by 100
but you need to keep in mind, that the number in the memory is *always* the long ugly unprecise number... the computer just conveniently ignored it for you when showing it to you.. but if you add 0.14 to 0.14, you're also adding all the fraction parts :)
YouAllSuck1580158dYou know I can’t tell you how familiar this post looks
Berkmann181290158dIt amazes me how I still see people shitting on JS for what is an IEEE 754 issue that happens in other languages too.
siddharth-neo26156d😵 I will be back in second!
I need to quickly check my last deployment of a feature "which calculates people's balance sheets for taxes"