News for hackers. Stuff that matters.
Re:0.30000000000000004 by marcosdumay on Friday November 13, 2015 @06:51AM (#10559308)
Those 0.1, 0.2, and 0.3 numbers do not have any floating point representation at all. Computers just can not use them.
Re:0.30000000000000004 by skrebbel on Friday November 13, 2015 @07:25AM (#10559419)
Of 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.

Re:0.30000000000000004 by lsaferite on Friday November 13, 2015 @07:51AM (#10559517)
I thought finance worked using integers internally for this exact reason.
Re:0.30000000000000004 by dragonwriter on Friday November 13, 2015 @05:26PM (#10562799)
"Finance" uses lots of different things. Sometimes it uses symbolic math; sometimes it uses binary floating point, sometimes it uses decimal floating point, sometimes it uses arbitrary precision decimals or rationals, sometimes it uses integers in the smallest unit of concern for the application.

Finance is a big domain.

Re:0.30000000000000004 by ars on Friday November 13, 2015 @12:47PM (#10561157)
There 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.

Re:0.30000000000000004 by lsaferite on Friday November 13, 2015 @04:44PM (#10562595)
Do 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.

Re:0.30000000000000004 by mehrdada on Friday November 13, 2015 @11:58PM (#10564530)
> Do CPUs support doing math directly with BCD values

Perhaps surprisingly to some, x86 has instructions for BCD manipulation at the CPU level. Legacy.

Re:0.30000000000000004 by jerven on Saturday November 14, 2015 @04:06PM (#10567257)
In IBM POWER 6 this returned as an optimisation in hardware.

https://books.google.ch/books?id=SPXHAgAAQBAJ&pg=PA44&lpg=PA...

Re:0.30000000000000004 by mannykannot on Friday November 13, 2015 @08:33AM (#10559723)
Maybe for accounts bookkeeping, but I doubt it is feasible, or even desirable, for financial statistical work.
Re:0.30000000000000004 by roblabla on Friday November 13, 2015 @08:00AM (#10559559)
I figured they used Binary Coded Decimal [0] when they need to support decimals.

[0] : https://en.wikipedia.org/wiki/Binary-coded_decimal

Re:0.30000000000000004 by lsaferite on Friday November 13, 2015 @09:29AM (#10559983)
Well, 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.
Re:0.30000000000000004 by astine on Friday November 13, 2015 @09:38AM (#10560027)
Accounting systems I've worked with do most of their logic in a RDBM and store monetary types using a 'decimal' type.
Re:0.30000000000000004 by Grue3 on Friday November 13, 2015 @07:03AM (#10559353)
But 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.
Re:0.30000000000000004 by amalcon on Friday November 13, 2015 @07:08AM (#10559364)
Well, 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.
Re:0.30000000000000004 by Kristine1975 on Friday November 13, 2015 @08:12AM (#10559621)
Since 2008 IEEE-754 also supports decimal floating-point: 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)