Urgent.News

What's breaking now, across hundreds of outlets.

Tech

How floating-point numbers are stored (and why 0.1 isn't exact)

Disclosure: this post was written with AI assistance and checked by the FlashKit team. Open any console and type 0.1 + 0.2 . You get 0.30000000000000004 . This is not a bug in Python, JavaScript or your browser. It is what happens when a number with a repeating binary expansion is squeezed into a fixed number of bits. Once you can read the bits of a float by hand, the "surprise" stops being one.…

Floating-point numbers are stored using three fields: a sign bit, an exponent, and a mantissa. The value of a normal number is calculated as (-1)^sign × 1.mantissa × 2^(exponent - bias). The leading 1 in the mantissa is not stored; it is implied, which saves one bit of precision. Exponents can have reserved patterns: all zeros represent zero or subnormal numbers, while all ones indicate infinity or NaN (not-a-number) if the mantissa is zero.

To demonstrate, let's look at two examples: 5.75 and 0.1. For 5.75, we first convert it to binary, obtaining 101.11. Normalizing it, we get 1.0111 × 2^2. The sign is positive, so the sign bit is 0. The exponent (2) plus the bias (127) equals 129, which is 10000001 in binary. The mantissa is 0111, padded with zeros to 23 bits. Thus, 5.75 is stored as 0x40B80000 in hexadecimal.

Conversely, 0.1 cannot be stored exactly. Doubling the fraction repeatedly yields a repeating pattern of 0011, similar to how 1/3 = 0.333... in decimal. Since 1/10 has a factor of 5 in the denominator, it never terminates in binary. Normalizing 0.1 gives 1.100110011... × 2^-4. The sign is 0, and the exponent (−4 + 127 = 123) is 01111011 in binary.

The mantissa is 10011001100110011001100, rounded up to 10011001100110011001101 due to the leftover bits. The final representation of 0.1 is 0x3DCCCCCD, which is not equal to the expected 0.1. The error is approximately 1.49 × 10⁻⁹.

When adding 0.1 and 0.2, both numbers are already rounded: 0.1 becomes 0.1000000000000000055511151231257827..., and 0.2 is 0.2000000000000000111022302462515654.... Their exact sum is 0.3000000000000000166533453693773481..., which is halfway between two neighbouring doubles. According to IEEE 754's default rule (round half to even), the sum is stored as 0.300000000000000044408920985006..., appearing as 0.30000000000000004 when printed with enough precision.

Therefore, 0.1 + 0.2 does not equal 0.3. To compare floats accurately, use a tolerance, such as math.isclose(a, b) in Python, rather than the equality operator (==).

Rewritten by urgent.news; may contain errors — check the original.

Source: dev.to

Read the original at dev.to →

Report a problem with this story

More in Tech

Audit the JSON-LD on your site with Python

JSON-LD, in this context, is the block of structured data inside a <script type="application/ld+json"> tag that tells search engines what a page describes: a product, an event, an organisation.

  • JSON-LD provides structured data for web pages to search engines
  • Validators check JSON-LD for individual URLs, but script audits all pages
  • Script identifies common issues like invalid JSON types and past event dates

More from Friday 27 November →