4 ms·
Those 0.1, 0.2, and 0.3 numbers do not have any floating point representation at all. Computers just can not use them.
by marcosdumay 11y ago
Those 0.1, 0.2, and 0.3 numbers do not have any floating point representation at all. Computers just can not use them.
- Grue3 11y agoBut these numbers are perfectly computable! Sure they cannot be represented in the commonly used binary implementation of floating point numbers, but that doesn't mean computers cannot operate with these numbers at all.
- amalcon 11y agoWell, they can't be specified as IEEE 754 floats. A computer can still use those numbers in the form of decimal fixed-point or numerator-denominator formats.
- Kristine1975 11y agoSince 2008 IEEE-754 also supports decimal floating-point: https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange_formats https://en.wikipedia.org/wiki/IEEE_754#Basic_and_interchange... >There are three binary floating-point basic formats (encoded with 32, 64 or 128 bits) and two decimal floating-point basic formats (encoded with 64 or 128 bits)
- skrebbel 11y agoOf course they do, just not a binary floating point representation. They can be represented with decimal floating point just fine and there are plenty of programs and libraries that can do this. It's very commonly used in finance, for example.
- lsaferite 11y agoI thought finance worked using integers internally for this exact reason.
- roblabla 11y agoI figured they used Binary Coded Decimal [0] when they need to support decimals. [0] : https://en.wikipedia.org/wiki/Binary-coded_decimal https://en.wikipedia.org/wiki/Binary-coded_decimal
- lsaferite 11y agoWell, I know from doing payment gateway integration that many of them use integers for currency amounts. They are the smallest currency unit and for display you then convert based on the currency specific precision value.
- mannykannot 11y agoMaybe for accounts bookkeeping, but I doubt it is feasible, or even desirable, for financial statistical work.
- astine 11y agoAccounting systems I've worked with do most of their logic in a RDBM and store monetary types using a 'decimal' type.
- ars 11y agoThere are several ways to do it. One it so use fixed point decimal with a 1/100 scaling instead of floating point. So yes, integers and then you shift the decimal point when you output. But you have to watch out for interest calculations, you might earn a fraction of a penny on a daily basis but over a year it adds up and you can't throw away that accuracy. So you might do all math as floating point, but with pennies instead of dollars. Or you can do Binary Coded Decimal, which stores each group of decimal number after the decimal point exactly, in many ways it similar to fixed point decimal, since it is a fixed point.
- lsaferite 11y agoDo they do USD interest calculations in something smaller than 1/10000 (decimill I think)? The issue with BCD would be data store support and CPU support right? Do CPUs support doing math directly with BCD values and does your DB support doing math directly with BCD values? In any case, using floating point for any currency calculations is a huge no-no, even though a lot of things do it anyway. I work with Magento daily and all their currency math uses PHP floats and the rounding errors drive me bonkers.