Fundamentals 10 min read

Why 0.1 + 0.2 ≠ 0.3 in JavaScript? The Hidden Truth Behind Floating‑Point Math

This article explains why adding 0.1 and 0.2 in JavaScript (and most other languages) does not yield exactly 0.3, covering binary representation of decimal fractions, IEEE‑754 precision limits, single‑ and double‑precision formats, and practical implications for developers.

Java Backend Technology
Java Backend Technology
Java Backend Technology
Why 0.1 + 0.2 ≠ 0.3 in JavaScript? The Hidden Truth Behind Floating‑Point Math

JavaScript, created in the 1990s, suffers from surprising floating‑point results such as 0.1 + 0.2 ≠ 0.3 , a phenomenon that earned William Kahan the 1989 Turing Award.

0.1 + 0.2
0.30000000000000004

The same issue appears in almost every modern language—Java, Ruby, Python, Swift, Go—because they all follow the IEEE‑754 floating‑point standard, not because the languages compute incorrectly.

floating-point-math
floating-point-math

When the author first learned C, they encountered the float type and initially treated programming floats and mathematical decimals as the same, but binary representation reveals fundamental differences.

Programming floats have limited precision: 32‑bit single precision or 64‑bit double precision.

Mathematical decimals can be expressed with infinite series, allowing arbitrary precision.

Binary cannot represent decimal fractions like 0.1 or 0.2 exactly within a finite number of bits, and both numbers and their sum 0.3 become infinitely repeating binary fractions that must be rounded.

Binary and Decimal

Every integer can be converted losslessly between decimal and binary, but decimal fractions such as 0.1 require an infinite binary expansion. For example, 0.375 converts cleanly to binary, while 0.1 and 0.2 do not.

decimals-binary-representation
decimals-binary-representation

Both 0.1 and 0.2 become repeating binary sequences, and their sum 0.3 also repeats, so the computer stores only an approximation.

dot-three-binary-representation
dot-three-binary-representation

Precision Limits

Programming languages typically provide 32‑bit float (single precision) and 64‑bit double (double precision). Some languages expose float32 and float64 aliases. Single precision offers about 7 decimal digits of accuracy; double precision about 15.

float-and-double
float-and-double

Single precision ( float): 1 sign bit, 8 exponent bits, 23 fraction bits.

Double precision ( double): 1 sign bit, 11 exponent bits, 52 fraction bits.

For example, the single‑precision representation of 0.15625 uses a sign bit of 0, an exponent of 124 (binary 01111100), and a fraction of 0.25.

floating-number-example
floating-number-example

Converting this binary format back to decimal yields the exact value 0.15625, but 0.1 and 0.2 cannot be represented exactly; their stored values are 0.100000001490116119384765625 and 0.20000000298023223876953125 respectively.

dot-one-dot-two-floating-number
dot-one-dot-two-floating-number

Adding these approximations yields 0.300000004 (single precision) or 0.30000000000000004 (double precision), matching the earlier observed result.

Conclusion

Seeing values like 0.300000004 is normal; it shows that the language correctly implements IEEE‑754. Single precision provides about 7 significant digits, double precision about 15. For higher‑precision needs, use decimal types with 28 digits or rational representations, accepting the performance cost.

Binary cannot exactly represent some decimal fractions within a finite number of bits.

Single and double precision store only approximations of such fractions.

Floating‑point design balances hardware constraints (32‑ or 64‑bit architectures) with the need for compact, fast arithmetic. Open questions remain, such as which languages offer built‑in high‑precision numbers and how to represent all real numbers exactly.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptprecisionfloating-pointBinaryIEEE-754
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.