If you type int(19.99 * 100) into Python you'll get 1998. I wanted to know how often that happens, so I went through every price from 0.01 to 100.00, multiplied it by 100 and truncated it, and 573 of those 10,000 prices came out a cent short. The first few are 0.29, 0.57, 0.58, 1.13, 1.14 and 1.15, so you don't have to go very far before you meet one.
bad = [c for c in range(1, 10001)
if int(float(f"{c/100:.2f}") * 100) != c]
print(len(bad)) # 573
It's because 19.99 can't be written exactly in binary, and when you multiply what's stored by 100 you get 1998.9999999999998, which truncation then takes down to 1998. If you change int to round all 10,000 of them come out right, so that's what I'd do at the point where a decimal price comes into the program (a cast in C++ truncates as well, and there I'd use std::llround).
Comparing prices
You can also get caught when you compare two prices. I took every price c from 0.01 up to 99.99, added one cent to it as a double, and asked whether the result equals the double for the next price up. It doesn't for 3,203 of the 9,999, which is nearly a third. So if your order book is a map with a double as the key, you'll sooner or later have an order sitting at a level nobody can find.
Some people would use a small tolerance for this, and I can see why you'd want to. The trouble I have with it is that a tolerance doesn't work as a map key, and you've also then got to agree on what it is everywhere in the code.
What it looks like with ticks
I'd turn the price into a whole number of ticks once, when it comes in, and it stays like that until somebody has to read it. If the tick is a cent then 19.99 is just 1999, and the next price up is 2000. You can compare them with == and use them as an index into an array, which is also quicker than a map.
struct Price { std::int64_t ticks; };
Price from_double(double p, double tick) {
return { std::llround(p / tick) };
}
Sums are safe too. I added 0.01 to itself a million times in double and got 10000.000000171856, whereas with ticks you've got exactly a million of them. You might not mind about that for a chart, but I'd mind if it was a position.
If you get this in an interview for a low-latency job, I think it's enough to say why a double can't hold 19.99, where you'd round, and that you'd keep a 64-bit integer after that. I've gone through the rest of it (instruments with different ticks, notional values and overflow) in the free lesson at https://academicquant.co.uk/qd-integer-ticks?utm_source=devto&utm_medium=post&utm_campaign=devto-integer-ticks-prices
Top comments (1)
The 573 out of 10,000 is the number that makes this land — everyone has seen the 19.99 example, almost nobody has counted how often it actually bites. One wrinkle your two fixes don't quite share: Python's round() rounds halves to even, while std::llround rounds them away from zero. So a price landing exactly on a half-tick — 19.985 with a 0.01 tick — comes out 1998 ticks in the Python version and 1999 in the C++ one. Do you pick a rounding rule deliberately for sub-tick prices from a venue, or does the tick size save you because nothing ever arrives at a half tick?