57

I write financial applications where I constantly battle the decision to use a double vs using a decimal.

All of my math works on numbers with no more than 5 decimal places and are not larger than ~100,000. I have a feeling that all of these can be represented as doubles anyways without rounding error, but have never been sure.

I would go ahead and make the switch from decimals to doubles for the obvious speed advantage, except that at the end of the day, I still use the ToString method to transmit prices to exchanges, and need to make sure it always outputs the number I expect. (89.99 instead of 89.99000000001)

Questions:

  1. Is the speed advantage really as large as naive tests suggest? (~100 times)
  2. Is there a way to guarantee the output from ToString to be what I want? Is this assured by the fact that my number is always representable?

UPDATE: I have to process ~ 10 billion price updates before my app can run, and I have implemented with decimal right now for the obvious protective reasons, but it takes ~3 hours just to turn on, doubles would dramatically reduce my turn on time. Is there a safe way to do it with doubles?

Superman
  • 3,686
  • 6
  • 34
  • 46
  • 4
    Because decimal denominators are powers of 10, and binary denominators are powers of 2, even 1 decimal place cannot be represented without error. For instance, 0.1 (one tenth) has no exact equivalent in binary, which is the same principle as one third not having an exact representation in decimal. – Matt Curtis Jul 28 '11 at 00:22
  • 1
    What's your bottleneck? Exactly where are you burning all your CPU cycles? Without solid measurements there is a good chacne you are looking at the entirely wrong thing. – Jonathan Allen Dec 04 '08 at 07:59

7 Answers7

86
  1. Floating point arithmetic will almost always be significantly faster because it is supported directly by the hardware. So far almost no widely used hardware supports decimal arithmetic (although this is changing, see comments).
  2. Financial applications should always use decimal numbers, the number of horror stories stemming from using floating point in financial applications is endless, you should be able to find many such examples with a Google search.
  3. While decimal arithmetic may be significantly slower than floating point arithmetic, unless you are spending a significant amount of time processing decimal data the impact on your program is likely to be negligible. As always, do the appropriate profiling before you start worrying about the difference.
Robert Gamble
  • 106,424
  • 25
  • 145
  • 137
  • 15
    +1: Avoid premature optimization. Measure first, optimize only after you have proof. – S.Lott Dec 01 '08 at 00:44
  • 1
    "no widely-used hardware..." IBM Mainframes (still remarkably popular) have hardware decimal. – S.Lott Dec 01 '08 at 00:45
  • 4
    New machines supporting the new IEEE 754 floating point standard will have decimal arithmetic support in hardware. IBM Power6 is one such, I believe. – Jonathan Leffler Dec 01 '08 at 00:49
  • 3
    @S.Lott: "*almost* no widely-used hardware". – Robert Gamble Dec 01 '08 at 01:02
  • @Jonathan: I know that IBM recently announced decimal support in Power6 and they are expected to integrate support in other lines as well, others may follow which is a good thing but it will be many years before decimal support in hardware is wide-spread. – Robert Gamble Dec 01 '08 at 01:05
  • I did work on a system where Decimal performance was a bottle neck. It was a windows based nightly batch process. In 1996. – sal Jul 16 '09 at 14:50
27

There are two separable issues here. One is whether the double has enough precision to hold all the bits you need, and the other is where it can represent your numbers exactly.

As for the exact representation, you are right to be cautious, because an exact decimal fraction like 1/10 has no exact binary counterpart. However, if you know that you only need 5 decimal digits of precision, you can use scaled arithmetic in which you operate on numbers multiplied by 10^5. So for example if you want to represent 23.7205 exactly you represent it as 2372050.

Let's see if there is enough precision: double precision gives you 53 bits of precision. This is equivalent to 15+ decimal digits of precision. So this would allow you five digits after the decimal point and 10 digits before the decimal point, which seems ample for your application.

I would put this C code in a .h file:

typedef double scaled_int;

#define SCALE_FACTOR 1.0e5  /* number of digits needed after decimal point */

static inline scaled_int adds(scaled_int x, scaled_int y) { return x + y; }
static inline scaled_int muls(scaled_int x, scaled_int y) { return x * y / SCALE_FACTOR; }

static inline scaled_int scaled_of_int(int x) { return (scaled_int) x * SCALE_FACTOR; }
static inline int intpart_of_scaled(scaled_int x) { return floor(x / SCALE_FACTOR); }
static inline int fraction_of_scaled(scaled_int x) { return x - SCALE_FACTOR * intpart_of_scaled(x); }

void fprint_scaled(FILE *out, scaled_int x) {
  fprintf(out, "%d.%05d", intpart_of_scaled(x), fraction_of_scaled(x));
}

There are probably a few rough spots but that should be enough to get you started.

No overhead for addition, cost of a multiply or divide doubles.

If you have access to C99, you can also try scaled integer arithmetic using the int64_t 64-bit integer type. Which is faster will depend on your hardware platform.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • What are the limits of multiplication and division here? – Joe Mar 05 '18 at 18:30
  • 1
    Hard to believe this isn't voted higher, as it is a much better answer for the OP, who was seeking speed improvements based on observation of perf bottlenecks. Scaling and descaling is a good answer that completely avoids rounding error while boosting speed 100X. – Craig.Feied Mar 08 '18 at 14:14
  • This is called 'Fixed Point Arithmetic'. https://en.wikipedia.org/wiki/Fixed-point_arithmetic There are further computational gains to be made if you use base 2 scale factors rather than a base 10 as you have done, then divide and multiply by bit shits. Whether that performance gain is material is of course dependent on your overall algorithm and performance profiling – JimbobTheSailor Sep 21 '20 at 22:13
21

Always use Decimal for any financial calculations or you will be forever chasing 1cent rounding errors.

Craig
  • 36,306
  • 34
  • 114
  • 197
14
  1. Yes; software arithmetic really is 100 times slower than hardware. Or, at least, it is a lot slower, and a factor of 100, give or take an order of magnitude, is about right. Back in the bad old days when you could not assume that every 80386 had an 80387 floating-point co-processor, then you had software simulation of binary floating point too, and that was slow.
  2. No; you are living in a fantasy land if you think that a pure binary floating point can ever exactly represent all decimal numbers. Binary numbers can combine halves, quarters, eighths, etc, but since an exact decimal of 0.01 requires two factors of one fifth and one factor of one quarter (1/100 = (1/4)*(1/5)*(1/5)) and since one fifth has no exact representation in binary, you cannot exactly represent all decimal values with binary values (because 0.01 is a counter-example which cannot be represented exactly, but is representative of a huge class of decimal numbers that cannot be represented exactly).

So, you have to decide whether you can deal with the rounding before you call ToString() or whether you need to find some other mechanism that will deal with rounding your results as they are converted to a string. Or you can continue to use decimal arithmetic since it will remain accurate, and it will get faster once machines are released that support the new IEEE 754 decimal arithmetic in hardware.

Obligatory cross-reference: What Every Computer Scientist Should Know About Floating-Point Arithmetic. That's one of many possible URLs.

Information on decimal arithmetic and the new IEEE 754:2008 standard at this Speleotrove site.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1/100 needs a divisor of 5 as well as a binary fraction - or, more precisely, needs two factors of five: 1/100 = (1/4)*(1/5)*(1/5). The 1/4 can be represented exactly in a binary floating point number; the 1/5 factors cannot. – Jonathan Leffler Dec 01 '08 at 01:20
  • @LawrenceDol They are, of course, and no one said otherwise. What Jonathan *did* say, correctly, is that they cannot be exactly represented in binary. Hopefully your confusion has lifted in the intervening years. – Jim Balter Oct 10 '18 at 04:42
8

Just use a long and multiply by a power of 10. After you're done, divide by the same power of 10.

  • 1
    you would think Decimal type could be implemented this way by the database server, providing competitive performance. – philwalk Aug 22 '18 at 22:31
6

Decimals should always be used for financial calculations. The size of the numbers isn't important.

The easiest way for me to explain is via some C# code.

double one = 3.05;
double two = 0.05;

System.Console.WriteLine((one + two) == 3.1);

That bit of code will print out False even though 3.1 is equal to 3.1...

Same thing...but using decimal:

decimal one = 3.05m;
decimal two = 0.05m;

System.Console.WriteLine((one + two) == 3.1m);

This will now print out True!

If you want to avoid this sort of issue, I recommend you stick with decimals.

mezoid
  • 28,090
  • 37
  • 107
  • 148
  • 1
    What's truly irritating is that the .NET ToString() methods seems to be incapable of displaying this difference, even when asked to display up to 20 decimal figures. If you subtract the numbers, you can see that there is about 4.44E-16 difference between them... but I don't see a 4 anywhere in the string "3.10000000000000000000" output by the ToString method. If you use BitConverter.ToString( BitConverter.GetBytes( one + two )) and the same thing for (3.1), then the bytes are indeed different (CD-CC-CC-CC-CC-CC-08-40 vs. CC-CC-CC-CC-CC-CC-08-40), however double.ToString() does not show this! – Triynko Apr 21 '10 at 20:36
  • 3
    If one uses proper scaling and has well-defined rounding rules, `double` can be every bit as precise as `decimal`. If one doesn't have precise rounding rules, `decimal` will often have the same problems as `double`. For example, if three people each buy one hundred items that sell for a price of "3 for $5", how much money should be received? – supercat Jun 05 '12 at 17:50
  • This doesn't answer any of the questions that the OP asked. – Jim Balter Apr 19 '16 at 06:01
  • So if 3.1 can't be expressed in `double`, then what exactly is `3.1` that `one + two` is being compared with? Is it a float? A double? A decimal? None of the above? Cus I don't get it lol. – NotAPro May 30 '22 at 00:24
1

I refer you to my answer given to this question.

Use a long, store the smallest amount you need to track, and display the values accordingly.

Community
  • 1
  • 1
gbjbaanb
  • 51,617
  • 12
  • 104
  • 148