C++ price type to properly deal with rounding error?

Your problem is the above. "Getting it right the first time" depends on what usage you need. If you need to sort numbers, maybe int64 will be OK for you, given some limitations. Or maybe int64 + last int64 for exponent part (forgoing unlimited precision and numerical range)?

What limitations can you live with? If you want to be completely unbounded, these problems will keep popping up at every turn, which is why serious software vendors design with sane limitations in the first place.

By using C++ and some STL/Boost libraries, maybe you can template away from the problem, but do you really really need to?

The huge difference here is that businesses focus on the businessproblems, while you take on this not as a business, but for exploration and research, with a technical focus.
Usage is for price representation on the Ritchie Stock Exchange. How should I store prices in the order book, market data, logs... prices are everywhere.

For U.S. equities, the price must be accurate to the penny; for forex, to the pip (0.0001). Zero tolerance for error in price!
 
Usage is for price representation on the Ritchie Stock Exchange. How should I store prices in the order book, market data, logs... prices are everywhere.

For U.S. equities, the price must be accurate to the penny; for forex, to the pip (0.0001). Zero tolerance for error in price!

Ok, design for those use cases then, not more! :D
 
Hey All,

What type is considered is "best practice" for price representation in C++?

I'm building an order book, and have run into the rounding error gremlin.

The problem is ubiquitous, but in this example, I'm using a double to both represent a price point, and also as a key in the bid/ask queues. Rounding error is causing the key lookup to be empty. Example: here I am simply trying to print L2 book data +/- 10 price points above/below market price. Using a loop and an increment of $0.01 (penny pricing), I try to index bid[12.78]. Due to rounding error, I get bid[12.78000000000000001], which maps to nothing. :mad:

Any recommendations on how to represent price type? I'm not sure what direction to head here. I've heard:

1. Internally represent prices as integers, and convert to doubles only when necessary.
2. Represent the whole number and decimal part of numbers as integers. This seems like a lot of work.
3. Use the Intel scientific library.

This is pretty much driving me crazy, so any help is appreciated.

What is the best practice for price representation in C++?

Thank you.

roundingError.png

I think you should stick to decimals. Doubles are inherently problematic and have epsilon issues (https://en.wikipedia.org/wiki/Machine_epsilon). To be honest, unless you are doing somehting that requires c++, you'd be better off building this in c#, imho. But if c++ is the way to go, then yeah, get a library like boost that implements a c++ decimal (http://stackoverflow.com/a/15320495)
 
I think you should stick to decimals. Doubles are inherently problematic and have epsilon issues (https://en.wikipedia.org/wiki/Machine_epsilon). To be honest, unless you are doing somehting that requires c++, you'd be better off building this in c#, imho. But if c++ is the way to go, then yeah, get a library like boost that implements a c++ decimal (http://stackoverflow.com/a/15320495)
Thanks for that wiki link. I couldn't remember what that was called.

I've used GMP in the past as well for some crypto stuff. It was a total PITA to get it compiled under Windows, though it's a no-brainer in Linux.
 
I think you should stick to decimals. Doubles are inherently problematic and have epsilon issues (https://en.wikipedia.org/wiki/Machine_epsilon). To be honest, unless you are doing somehting that requires c++, you'd be better off building this in c#, imho. But if c++ is the way to go, then yeah, get a library like boost that implements a c++ decimal (http://stackoverflow.com/a/15320495)
Why do you think that I'd be better off building this in C#? As I see it, C++ has some clear advantages:

1. Implement in Linux, making available an open-source, open-kernel platform for perf tweaking, and all other Linux beauty. C# can't be compiled on Linux (at least not a C# compiler that I would trust with financial data).
2. Avoiding Windows fees, crashes, and "The Microsoft Way"
3. Portable code that can be compiled under Linux, Windows, Mac, UNIX (Sun, AIX, HPUX), even OS/2 :D
4. Terminal and shell scripts available for console-based applications. (I suppose you could argue for PowerShell here; it's come a long way, but the terminal is the terminal).
5. Perf. C++ beats C# everywhere from a performance perspective:
https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=csharpcore&lang2=gpp
6. C++ is the standard in the financial industry

Also, what do you mean by "Stick to decimals." You mean floating point base (2 ^ -n) internal representation?
 
Last edited:
If you're doing this all yourself and you're most familiar with C++ then you should just stick with it. It's the fastest way from point A to point B.
 
I think doubles are accurate to 15 significant digits.
I don't know of any financial instrument that needs more than 15, perhaps other posters can?
The largest one i can think of are bonds futures that trade in 1/128 increments.
A price like 120 1/128 = 120.0078125
That is 10 significant digits.

Then you just round off any rounding errors outside those 10 significant digits.

So i think doubles might be fine.

But perhaps you might want to define your own 'Price' class that encapsulates a double to start with, but can easily change in future if double proves problematic.

You could also extend class 'Price' with specialised versions, eg stock prices, fx prices, bond prices if needed.
 
I think doubles are accurate to 15 significant digits.
I don't know of any financial instrument that needs more than 15, perhaps other posters can?
The largest one i can think of are bonds futures that trade in 1/128 increments.
A price like 120 1/128 = 120.0078125
That is 10 significant digits.

Then you just round off any rounding errors outside those 10 significant digits.

So i think doubles might be fine.

But perhaps you might want to define your own 'Price' class that encapsulates a double to start with, but can easily change in future if double proves problematic.

You could also extend class 'Price' with specialised versions, eg stock prices, fx prices, bond prices if needed.
Thank you! That was very helpful! I really like the idea of creating and extending a "Price" class. I had thought of making price a fundamental type (int, double, some size_t, etc).
 
Why do you think that I'd be better off building this in C#? As I see it, C++ has some clear advantages:

1. Implement in Linux, making available an open kernel for perf tweaking, and all other Linux beauty
2. Avoiding Windows fees, crashes, and "The Microsoft Way"
3. Portable code that can be compiled under Linux, Windows, Mac, UNIX (Sun, AIX, HPUX), even OS/2 :D
4. Terminal and shell scripts available for console-based applications. (I suppose you could argue for PowerShell here, but the terminal is the terminal).
5. Perf. C++ beats C# everywhere from a performance perspective:
https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=csharpcore&lang2=gpp
6. C++ is the standard in the financial industry


Yeah those are good points. Here are the pros of using c#

1) Perf - True, native c++ holds an advantage, but that only really materializes when you have optimized your entire network (slow processor, 1 gig switch, slow disks on your database). Honestly, with gpu's and mutithread cores, the perf difference is not as great as you would expect.

2) Ease of maintenance - managed code is a lot easier to maintain because of all of c++ gotcha's. Also today we have more c#/java programmers than c++ programmers (don't know if that is good or bad, but as a hiring manager, I can tell you it's easier to hire a java/c#).

3) Ease of use - c# and java are just syntactically easier to use and have a cleaner design. Building classes in c# feels much more cleaner than c++. Plus less gothca's!

2) Memory leaks - they happen a lot more in c++ than c#

3) Window fees - can't argue that here, but I think this is a bit overblown. Depending on your ROI expectations, a three year server running windows will cost about $800 for the OS and # -4 k for the server. But the real killer will be the maintenance. I good linux admin willl cost you about 50k over the equivalent Windows admin (agian, this is my own experience, so please don't be offended).

4) c++ is the standard - I would argue with that. I worked at Direct edge and we implemented the exchange in c#. Bats bought us out and they are a linux/c++ shop, but I'm not sure if they had to start over, they would use c++ (Although they had better speeds of execution, but I'd argue again, hardware can remedy that). I think java/c# are the more prevalent platforms now.
 
Why do you think that I'd be better off building this in C#? As I see it, C++ has some clear advantages:

1. Implement in Linux, making available an open-source, open-kernel platform for perf tweaking, and all other Linux beauty. C# can't be compiled on Linux (at least not a C# compiler that I would trust with financial data).
2. Avoiding Windows fees, crashes, and "The Microsoft Way"
3. Portable code that can be compiled under Linux, Windows, Mac, UNIX (Sun, AIX, HPUX), even OS/2 :D
4. Terminal and shell scripts available for console-based applications. (I suppose you could argue for PowerShell here; it's come a long way, but the terminal is the terminal).
5. Perf. C++ beats C# everywhere from a performance perspective:
https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=csharpcore&lang2=gpp
6. C++ is the standard in the financial industry

Also, what do you mean by "Stick to decimals." You mean floating point base (2 ^ -n) internal representation?

If it were me, I would use C# (or F#) over C++ in this situation just because the .NET languages have a built in "decimal" type (and that is what you need). It will make your life easier.

You should be searching for a language that makes your life easier, especially in the prototyping stage.

Also, I'm pretty sure C# (or F#) runs on Linux without much problem.
 
Back
Top