1

Whenever I'm dealing with numbers in PHP which represent money, I'm forced to use bcadd() and the other functions in that "family", instead of simply using the nice symbols such as +, -, / and *.

The reason is something about "floats" being inaccurate for some reason. I've many times tried to understand why exactly this is, but I can't make any sense of it.

It would be much nicer to use the symbols instead of the BC Math functions.

Why aren't the floats just accurate instead? Or made into "numerics" if "floats" by their nature are inaccurate?

It's not a problem anymore because I know about this, but for most of my life when I'm coded PHP, I had no clue even about the existence of those functions, let alone the need to use them.

  • There is significant overhead to function calls and the processing of numbers represented as strings. Why not just multiply your values by the required number of decimal places and do integer arithmetic instead? – Nick Jan 03 '21 at 04:35
  • @Nick I don't understand what you're suggesting that I do, but it doesn't sound more elegant than the BC Math functions method. – user14756437 Jan 03 '21 at 04:43
  • Treat dollars as integer cents. No issues. Cents as a unit is non-float – GetSet Jan 03 '21 at 05:40
  • Performance penalty. And PHP doesn't offer operator overloading in general. But there's https://wiki.php.net/rfc/operator_overloading_gmp – mario Jan 03 '21 at 06:06

0 Answers0