News for hackers. Stuff that matters.
Re:How to Handle Monetary Values in JavaScript by maxxxxx on Tuesday October 30, 2018 @03:36PM (#18340545)
You can't model 1.0 correctly but it will be something like 1.000006667. If you add enough of these numbers you will be off.
Re:How to Handle Monetary Values in JavaScript by recursive on Tuesday October 30, 2018 @03:50PM (#18340672)
Your details are off. 1.0 is precisely representable in double precision floating point with no error.
Re:How to Handle Monetary Values in JavaScript by maxxxxx on Tuesday October 30, 2018 @03:58PM (#18340760)
Sorry, picked the wrong number. But I think the general idea is correct.
Re:How to Handle Monetary Values in JavaScript by nestorD on Tuesday October 30, 2018 @07:05PM (#18341988)
I am pretty sure you were thinking of 0.1 which, indeed, requires an infinite number of digits to be represented in binary (like 1/3 in decimal).
Re:How to Handle Monetary Values in JavaScript by maxxxxx on Tuesday October 30, 2018 @10:33PM (#18342930)
Probably. I just remember that this tripped me up big time a long time ago.
Re:How to Handle Monetary Values in JavaScript by aaaaaaaaaab on Tuesday October 30, 2018 @04:20PM (#18340928)
Integers can be represented exactly in IEEE754.
Re:How to Handle Monetary Values in JavaScript by noselasd on Tuesday October 30, 2018 @05:20PM (#18341375)
Not all of them, e.g. 9007199254740993 can't be represented in a double precision format (what javascript uses)

For single precision floats you start having issues representing integers as low as 16777217

Re:How to Handle Monetary Values in JavaScript by aaaaaaaaaab on Tuesday October 30, 2018 @06:05PM (#18341683)
Sure, you can’t represent Graham’s number either...

I know you knew what I meant.

Re:How to Handle Monetary Values in JavaScript by noselasd on Tuesday October 30, 2018 @07:56PM (#18342267)
No not really. You have to define the bounds you were talking about.

People believing that a float type can represent all integers that the int type in their language can represent is not exactly uncommon.

Re:How to Handle Monetary Values in JavaScript by maxxxxx on Tuesday October 30, 2018 @05:05PM (#18341269)
How about 2.1?

According to this https://www.h-schmidt.net/FloatConverter/IEEE754.html

2.1 will be represented as 2.099999904632568359375

Re:How to Handle Monetary Values in JavaScript by noselasd on Tuesday October 30, 2018 @05:21PM (#18341383)
well, 2.1 isn't an integer.