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.
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.
![]()
Solucion numero uno.


