Do the Totals Add Up?

totals-check with tolerance

Part of: Receipt Scanner

A scanner that parrots back whatever total the model claims isn't a scanner. It's a transcription of a guess. The check that catches the most bad reads is plain arithmetic: the line items should sum to the subtotal, and the subtotal plus tax should equal the total. When they don't, something on the receipt got misread. Why == won't work here Money lives in floats, and floats don't compare the way you'd hope. 0.1 + 0.2 == 0.3 is False in Python. The math isn't broken; binary floating point just can't represent every decimal exactly. Receipts add their own wrinkle: they round to the cent, while your intermediate math doesn't. So a computed sum can land a fraction of a cent off the printed subtotal even when every number was read correctly. The fix is a tolerance . Instead of "are these exactly equal," you ask "are these within a cent or two of each other." Building the totals check Two comparisons cover the whole receipt. The items should sum to the subtotal, and the subtotal plus tax should equal the total. Notice that items sum multiplies price by quantity for each item instead of just adding prices. Two lattes at \$4.50 contribute \$9.00, not \$4.50. What a mismatch actually tells

Challenge: Does the Math Check Out