News for hackers. Stuff that matters.
Re:9999999999999999.0 – 9999999999999998.0 by 3pt14159 on Saturday January 05, 2019 @07:08PM (#18835302)
Outside of the academic world decimals are almost always a better solution if performance isn't critical.

Most logic is multiplicative. For example, apply a 30% tax on a dollar quantity and display both subtotal and grand total. With floats, there are inequalities. With decimal there usually aren't unless you're dividing, but we already have to deal with divide errors in base ten, and it is much more likely to need to represent 0.30 than 1/3 and because decimal shares a base with binary (since it's factors are 5 and 2) binary doesn't really get us anything but headaches anyway. It's true that there are still gotchas, but they happen less often and usually don't end up looking stupid and weird for no reason. That 0.1 + 0.2 = 0.300000000000001 is dumb and we all know it.

Re:9999999999999999.0 – 9999999999999998.0 by jacobolus on Sunday January 06, 2019 @01:41AM (#18836954)
> Outside of the academic world decimals are almost always a better solution

Is “academic world” now a shorthand for “all numerical computing”?

Decimals basically never make sense, except possibly in some calculations related to money. Those make up a minuscule part of modern computer use.

Maybe decimals are also better for homework assignments for schoolchildren?

The type of applications where decimals are useful are by and large insensitive to compute speed and need no special hardware support. You can easily write your code for decimal arithmetic on top of integer arithmetic hardware.

Those of us who need binary floating point for graphics, audio, games, engineering, science, .... won’t stop you.

Re:9999999999999999.0 – 9999999999999998.0 by dotancohen on Sunday January 06, 2019 @08:47AM (#18838270)
Even with money I use integers. Instead of dollars (or local currency), I store values internally as pennies (or local equivalent 1/100 of main currency). Sometimes when working with interest I'll need to work with floats, and some databases I have values stored as DECIMAL(8,2) instead of INT, but for the most part I've saved quite a few headaches by keeping my values in INTs.
Re:9999999999999999.0 – 9999999999999998.0 by loa-in-backup on Tuesday January 08, 2019 @09:48AM (#18855799)
There are currencies that have the lowest value coin as 1/20 of main coin.
Re:9999999999999999.0 – 9999999999999998.0 by CamouflagedKiwi on Sunday January 06, 2019 @10:10AM (#18838675)
That seems okay until you need to track sub-penny accuracy somewhere, then you have a big problem.

Lots of applications don't need that, but a surprising amount do, so it's not a global solution.

Re:9999999999999999.0 – 9999999999999998.0 by hakfoo on Sunday January 06, 2019 @02:49PM (#18840171)
But there's still a minimum significant value which can be defined from the problem space. Do DECIMAL(16,8) or whatever.

In some situations, there may even be industry or legal standards as to what can be considered rounding noise.

Re:9999999999999999.0 – 9999999999999998.0 by toyg on Sunday January 06, 2019 @06:53AM (#18837874)
No need for snark. You might be correct that, by “computational volume”, handling currency values might be considered a niche; but even something like World of Warcraft has to handle money at some point.
Re:9999999999999999.0 – 9999999999999998.0 by nearbuy on Sunday January 06, 2019 @12:56AM (#18836819)
That doesn't seem dumb at all. Making BCD the default would mean floats use 17% more space for the same precision. That might seem like a small loss, but it's also for an incredibly small gain. Programmers would still have to be aware that testing two decimals for exact equality is dangerous. I don't see the problem with 0.1 + 0.2 = 0.300000000000001 if you aren't testing floats for direct equality.
Re:9999999999999999.0 – 9999999999999998.0 by otabdeveloper2 on Sunday January 06, 2019 @09:10AM (#18838371)
> it is much more likely to need to represent 0.30 than 1/3

Citation needed, because this isn't really true.

Even if one concedes your (unspoken) idea that only financial transactions aren't "academic" (which also isn't true), in the real world financial transactions will typically include currency conversions, and those will have all sorts of weird non-decimal factors.

Re:9999999999999999.0 – 9999999999999998.0 by ChrisLomont on Saturday January 05, 2019 @10:50PM (#18836450)
You cannot do anything in finance with such reasoning. Take something simple, say a mortgage at 5% compounded 12 times a year. To compute payments using some fixed length representation or decimal is going to lead to more error than to use the usual floating point. This rabbit hole would continue for many applications.

Floating point makes them all much easier to do well.

Re:9999999999999999.0 – 9999999999999998.0 by jes5199 on Saturday January 05, 2019 @11:08PM (#18836509)
Have you worked on finance software? I have - we always used ints for everything, so we could avoid rounding suprises
Re:9999999999999999.0 – 9999999999999998.0 by bigiain on Saturday January 05, 2019 @11:28PM (#18836578)
I did a web project in the gambling space ~10 years back - we were legally required to perform all calculations as integers in ten thousandths of a cent (or millionths of a dollar).

We chose to _not_ do _any_ calculations client side in Javascript...

Re:9999999999999999.0 – 9999999999999998.0 by dotancohen on Sunday January 06, 2019 @08:48AM (#18838274)
Which regulation is that? I've worked on financial applications, but not gambling, and I've not heard of this regulation. I should probably know about it!
Re:9999999999999999.0 – 9999999999999998.0 by bigiain on Sunday January 06, 2019 @05:18PM (#18841098)
Australian, or possibly Tasmanian state regs. This would have been around 2011 or so (The Samsung Galaxy S2 was the "top of the range Android phone" at the time...)
Re:9999999999999999.0 – 9999999999999998.0 by ajanuary on Sunday January 06, 2019 @10:59AM (#18838919)
I worked for a gambling company in the UK for a bit. They did all their maths in pennies, not thousandths of a penny.
Re:9999999999999999.0 – 9999999999999998.0 by ChrisLomont on Monday January 07, 2019 @04:17PM (#18850206)
Yes, I have. I also have a math PhD, have written scads of scientific and numerical software, and have written articles on floating-point math. So now that we have enough of our personal accolades out of the way, let's focus on facts regarding calculations:

How did you use ints to compute compounded interest on loans? I asked that above, and you avoided it. I ask again.

For example, suppose you have a mortgage where you lent $100K at 5% annual, compounded 12 times a year, for 30 years, and you need basic values regarding this loan.

Often in such calculations you need to compute 100K*(1+0.05/12)^360. How do you do that with integers? Naively you need (1+(1/20)/12)^360, which as a reduced fraction each of the numerator and denominator have over 2800 binary digits. Do you really do this with integers?

Now put that in a mortgage trading or pricing system where it needs to do millions/billions of those per second.

Doing this as double gives enough precision to make the difference between computed and infinitely precise negligible (approx. 10^-17 error).

It's easy to make examples where doing incremental calculations, rounding to pennies and storing, results in long term error. In these cases I don't see how do to it with integers without massive overhead.

And this is a trivial, common example. Doing stuff like hedge fund stuff, or anything using numerical integration to make models for pricing, would be astounding hardly to do with integer only math.

What finance software did you write? A simple ledger works fine as integers. Anything more complex will hit performance and scaling issues soon after the basics.

Re:9999999999999999.0 – 9999999999999998.0 by DougBTX on Sunday January 06, 2019 @09:01AM (#18838327)
I’ve not done any real finance programming, so is this a reasonable explanation?

Currency is stored as a count of cents (millicents if being fancy). Therefore the two main features of floats are not useful:

- Support for very small numbers is not needed. Floats dedicate approx half their range to numbers between -1 and +1, this is wasted when counting whole cents.

- Support for very large numbers at the expense of precision is actively bad, as the precision must always be down to individual cents.

So the useful range of floats is much reduced when using floats for counting, approx 54 bits out of a 64 bit float are used. Instead ints (“counting numbers”) are much better for counting cents than floats (which approximate the continuous real numbers in a finite number of bits).

Re:9999999999999999.0 – 9999999999999998.0 by shakna on Saturday January 05, 2019 @11:27PM (#18836571)
IBM Decimals are the standard for finance applications. Integers, not floating point.

Floats don't have the precision required.