
Why We Replaced float64 With Decimal in Our Matching Engine
Every financial system eventually faces the same question: can we trust floating-point arithmetic with real money? For MatchEngine , an open-source order matching engine written in Go, the answer was no. This article explains the problem, walks through the migration from float64 to shopspring/decimal , and shows the specific code patterns that changed. The Problem: float64 Cannot Represent Money IEEE 754 double-precision floating-point ( float64 in Go) stores numbers in binary. Most decimal fractions — the kind humans use for prices — have no exact binary representation. The classic example: fmt . Println ( 0.1 + 0.2 == 0.3 ) // false fmt . Println ( 0.1 + 0.2 ) // 0.30000000000000004 This is not a Go bug. It is how binary floating-point works in every language. The error is tiny — about 5.5e-17 — but in a matching engine, tiny errors compound. How This Breaks a Matching Engine Consider a sell order for 0.1 BTC and another for 0.2 BTC, both at price 0.3 . A buy order arrives for 0.3 BT
Continue reading on Dev.to
Opens in a new tab

