Question
Why does 0.1 + 0.2 give me 0.30000000000000004 instead of 0.3 in most programming languages?
Claude response
Computers store decimal numbers in binary floating-point, and just like 1/3 can't be written exactly in base 10, many simple decimals like 0.1 can't be written exactly in base 2 — it becomes an infinitely repeating binary fraction that gets cut off at some precision. Adding two of these rounded approximations together can leave a tiny leftover error. It's not a bug in any particular language; it's the IEEE 754 standard nearly every language uses, and the fix is usually to compare numbers with a small tolerance instead of ==.
AI rating
9/10The answer correctly explains binary floating‑point representation and rounding errors, mentions IEEE‑754 and suggests tolerance‑based comparison, making it clear and useful, though it could note alternatives like decimal types.
- accuracy
- 9/10
- clarity
- 9/10
- completeness
- 8/10
- helpfulness
- 8/10
Automatically generated by an AI model (openai.gpt-oss-120b). Scores are approximate and may be imperfect.
Comments
Sign in with Google to leave a comment.
No comments yet.