yourname.dev

Floating point is not broken

Why 0.1 + 0.2 isn't 0.3, and why that's the correct answer.

Published 1 min read

A tiny surprise

console.log(0.1 + 0.2); // 0.30000000000000004

Binary cannot represent one tenth exactly, just as decimal cannot represent one third. The machine is doing precisely what the standard says.

What to do about it

Compare with a tolerance, or use integers for money.

Filed under fundamentals