Fundamentals 9 min read

Why 0.2 + 0.1 Equals 0.30000000000000004 in JavaScript and How IEEE‑754 Handles Decimal Fractions

JavaScript’s 0.2 + 0.1 yielding 0.30000000000000004 stems from binary floating‑point representation, where many decimal fractions cannot be expressed exactly; the article explains decimal‑to‑binary conversion, the repeating binary of 0.1, IEEE‑754 format, and how to avoid precision loss using BigDecimal in Java.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Why 0.2 + 0.1 Equals 0.30000000000000004 in JavaScript and How IEEE‑754 Handles Decimal Fractions

Background: Unexpected Decimal Results

When adding 0.2 + 0.1 in a web browser console, the result is 0.30000000000000004 instead of the expected 0.3. The same phenomenon appears for other decimal sums, such as 0.1 + 0.6 = 0.7. This is not a JavaScript‑specific bug; it is a consequence of how computers represent fractional numbers.

How Computers Represent Numbers

Computers store data as binary bits (0 and 1). Integers are stored using various encodings (sign‑magnitude, one's complement, two's complement). For signed numbers, two’s complement is most common. To represent a decimal integer in binary, the “divide‑by‑2, record remainders, reverse order” method is used.

For decimal fractions, the “multiply‑by‑2, take the integer part, repeat” algorithm converts them to binary. For example, 0.625 becomes 0.101 in binary.

Why Some Decimals Cannot Be Represented Exactly

Applying the fraction‑conversion algorithm to 0.1 yields an infinite repeating binary sequence: 0.000110011001100…. Because the binary expansion never terminates, a finite‑length binary word cannot store the exact value.

Consequently, numbers like 0.1, 0.2, and many others are stored as approximations.

IEEE 754 Floating‑Point Standard

To provide a uniform way of approximating real numbers, the IEEE 754 standard defines binary floating‑point formats (single‑precision 32‑bit and double‑precision 64‑bit being the most common). A floating‑point value consists of three fields:

S (sign) : 0 for positive, 1 for negative.

E (exponent) : biased integer representing the power of two.

M (mantissa or significand) : the fractional part of the number.

The actual numeric value is calculated as (‑1)^S × 1.M × 2^(E‑bias) for normalized numbers.

Because the mantissa is limited to 23 (single) or 52 (double) bits, the binary representation of 0.1 in double precision is:

0.00011001100110011001100110011001100110011001100110011001

Similarly, 0.2 is:

0.00110011001100110011001100110011001100110011001100110011

Adding these two binary numbers and converting the result back to decimal yields the familiar 0.30000000000000004 artifact.

Avoiding Precision Loss in Practice

In Java, float (single‑precision) and double (double‑precision) store only approximations, making them unsuitable for high‑precision calculations such as monetary values.

Java provides java.math.BigDecimal, which represents decimal numbers as arbitrary‑precision integer mantissas with a scaling factor, allowing exact arithmetic.

Key Takeaways

Most decimal fractions have infinite binary expansions; computers store only a finite approximation.

IEEE 754 defines how these approximations are encoded and interpreted.

The rounding errors manifest as seemingly strange results like 0.30000000000000004.

For critical precision, use arbitrary‑precision libraries (e.g., Java’s BigDecimal) instead of binary floating‑point types.

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.

JavaJavaScriptprecisionBigDecimalfloating-pointbinary conversionIEEE-754
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

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.