27

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

Comments
  • 3
    I 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.
  • 1
    @sariel also, manager can fuck of with deciding tech questions. It's not their job.
  • 4
    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.
  • 3
    @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.
  • 0
    Also, isn’t it the plot of Superman III?
  • 10
    @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.
  • 1
  • 1
    @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.
  • 3
    @hitko why are you doing fractional based arithmetic on a financial based transaction?
  • 0
    @sariel but... But... Rational numbers...
  • 0
    @iiii @sariel err Sales tax?
  • 0
  • 0
    @OldManOfTheC isn't that a percentage, which is a decimal, which is a float?
  • 3
    @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.
  • 1
    @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.
  • 2
    @iiii yeah, floats

    The math ended up with few cents off. And after months those cents where a big difference
  • 1
  • 0
    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 ".
  • 0
    @jiraTicket what's unintuitive in using the smallest currency value possible?
  • 0
    @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.
  • 1
    @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.
Add Comment