Have you ever wondered how computers store the floating-point numbers like 3.1415 (𝝿) or 9.109 × 10⁻³¹ (the mass of the electron in kg) in the

Binary representation of the floating-point numbers

submited by
Style Pass
2021-07-16 11:30:11

Have you ever wondered how computers store the floating-point numbers like 3.1415 (𝝿) or 9.109 × 10⁻³¹ (the mass of the electron in kg) in the memory which is limited by a finite number of ones and zeroes (aka bits)?

It seems pretty straightforward for integers (i.e. 17). Let's say we have 16 bits (2 bytes) to store the number. In 16 bits we may store the integers in a range of [0, 65535]:

If we need a signed integer we may use two's complement and shift the range of [0, 65535] towards the negative numbers. In this case, our 16 bits would represent the numbers in a range of [-32768, +32767].

As you might have noticed, this approach won't allow you to represent the numbers like -27.15625 (numbers after the decimal point are just being ignored).

We're not the first ones who have noticed this issue though. Around ≈36 years ago some smart folks overcame this limitation by introducing the IEEE 754 standard for floating-point arithmetic.

Leave a Comment