Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
sariel84473yI don't disagree, storing floats as not floats because the language doesn't allow for it just sounds like a shitty language.
But a manager also has no business dictating how tech works, but they could absolutely require a better stack. -
Once I told the back end devs about using floats as price. They said I was talking shit.
Months later management asks why the math doesn't add up. -
iiii90853y@ChristoPy wait what? Using cents as the base should have made math more precise, instead of less precise. Do you know why the math did not add up in that anecdotal case?
PS: wait, did you mean that they were using floats and you've told them it was bad? English is confusing. -
hitko31453y@sariel It's not JavaScript, it's a mathematical fact that any rational number has a finite base-N representation when and only when N is a multiple of that number's reduced denominator's prime factors.
For example:
22/24 in reduced form is 11/12 => reduced denominator is 12. Prime factorisation of 12 is 2^2 * 3, prime factors are 2 and 3, which means the number 22/24 only has a finite representation when base is a multiple of 2 * 3 = 6.
Conversely, any base-12, base-18, and base-24 number can e represented as a finite base-6 number, but base-30 numbers can't necessarily be represented in base-6 because prime factors of 30 are 2, 3, and 5. -
@hitko I would pay you to explain this to me. I know you just did but I don’t have the math background to understand. This is a behaviour of numbers in code that has always eluded me.
-
sariel84473y@hitko why are you doing fractional based arithmetic on a financial based transaction?
-
hitko31453y@sariel A float is a number in the form of "A * B ^ X", where A is the mantissa, B is the numerical base, and X is the exponent. In base-2 a float is A * 2^X, and in base-10 a float is A * 10^X. Now, A and X must both be integers, otherwise *you dawg, I heard you like floats, so I put a float inside your float so you can float while you float*.
Unless you can find an integer solution to 7 * 10^-20 = A * 2^X, you have to accept that base-10 floats are nothing like base-2 floats. -
@sariel decimal numbers are base 10 (its in the name), IEEE 754 floats (what most programming languages use as the "float" type) are base2 and will fuck you up if you try to treat them as decimals. For money its best to use a proper fixed point decimal type.
-
@iiii yeah, floats
The math ended up with few cents off. And after months those cents where a big difference -
The Floating Point Problem is a thing where you can't just go "okay, your mistake"
I think it's necessary to realise it's 100% unintuive for non-programmers and they will have trouble getting it. but we must explain it. Over and over again if required.
Links often help. "See - this is a know thing ". -
@iiii just saying in its unintuive to people that a computer, thought of as being perfect with math and numbers, could have any kind of issue with decimal numbers... Or any other number for that matter.
-
hitko31453y@jiraTicket It's only unintuitive if you don't acknowledge computer memory is limited, or that most mathematical operations take at least O(n^2).
Once you keep that in mind, it makes perfect sense to try and store only the most important part of each number.
Related Rants
-
boombodies15Manager: We need to setup the security in the Mexico server Dev: You mean that 3rd party firewall add on? Ma...
-
boombodies26Manager: Why aren’t you working? Dev: I am, I’m just not typing because I’m thinking an issue out. Man...
-
boombodies19Manager: How come the intern does way more tickets than you? Dev: Because you told me to only give him the ea...
Dev: You’ll want to store money values such as $2001.01 as 200121 when using javascript.
Manager: Why? That’s stupid.
Dev: Javascript doesn’t behave with decimals the way you think. It’ll show up as $2001.01000001 when you least expect it
Manager: Well I’ve never had that issue before! Besides that’s only a fraction of a cent off, that won’t even matter!
Dev: … literally the plot of office space but ok
rant
garbage collection
management logic