How can you explain to a senior dev, with more than 15 years of experience, that for money calculation (like VAT) you can't use the fucking floating types?!?!?!?!?

  • 3
    Tell him to print the first 20 digits of a float with exactly the value 0.1 without any deviations.
    Then laugh at him when he can't do it.
  • 0
    Make him implement the float type
  • 0
    @deadlyRants why is that? Me no understand šŸ˜
  • 4
    Please correct me where I'm wrong but here'sā€‹ as far as I understand it:

    Floating point arithmetic is inherently not 100% accurate, because of bit-representations, which means all Floating point calculations are only correct within some Margin of error. Which can lead to errors, which propagate through calculations and become bigger and bigger over Time.
    Starting with that doing an equivalent operation in a different order can change the result. (A+B)*C != AC+BC
    That is not reliable because money will disappear or be created out of nowhere.

    Again I am not 100% certain about some things I say here so if any of that is wrong please call me out!
  • 0
    See: machine epsilon
  • 1
    @Hojasdemanzano Floating-point numbers are usually not able to hold perfectly precise values.

    For example, if you assign 0.3 to a variable in reality it will have the value 0.29999999999999998890.
    Which means if you use that for currency you just destroyed a tiny amount of money.
    Even worse: The larger the number is, the more the precision decreases.

    And while this isn't an issue for most applications, if you're handling money you don't want that behaviour.
  • 0
    @deadlyRants ok, it makes sense! And what do people use for money?
  • 0
    @Hojasdemanzano There are datatypes specifically for accurate decimal numbers(e.g. Decimal in python).

    But I don't know how they're implemented(my guess is they're stored as fractions internally, but that might be totally wrong).
  • 0
    @deadlyRants aw. In Apex there is also a Decimal data type, I do wonder tho how it works internally šŸ˜Š
Add Comment