Fraction Representation in Binary

Diane Khambu
3 min readJul 12, 2021
Photo by Tim Goedhart on Unsplash

As I was writing this article on numeric types in Python, I also wrote on float data types. There are times where we cannot have finite binary representation of a fractional number such as 1/10, 2/10.

In this article we’ll go over IEEE’s standard on floating point binary representation and simple algorithm to compute binary representation of floating numbers.

While Python does have built-in function bin() to get binary value of integer, float type is still bit obfuscated. We can import ctypes module and use it to get binary value of fractions and it takes an understanding of normalized and denormalized scientific notation to understand the result. Let’s have a quick tour on it:

A Scientific Notation has a single digit to the left of the decimal point. A number in Scientific Notation with no leading 0s is called Normalized Number. For eg: 1.23 * 10^2 for 123 .

There are advantages of using normalized scientific notation including standard representation and increase accuracy of numbers stored by not letting unnecessary leading 0s to hog space. However, we still need to represent 0 as a whole number part of fraction when needed, for eg in 0.1 .

Floating point number representation:

-1^S * M * 2^E

where,

S = Sign bit (1 bit; 0 for positive, 1 for negative)

E = Exponent ( 8 bits, range 23–30 inclusive)

M = Mantissa (23 bits, range 0–22 inclusive)

There’s an intricate calculation for finding binary representation of fraction that takes care of fraction with number preceding a decimal point being both 1 (normalised) and 0 (denormalised).

After understanding the intricate calculation, you can understand what’s happening in the code that uses ctype module to get binary value of fractions:

On running the file to find binary value of 5.5, we get:

sign (bin): 101100000000000000000000
sign (float): 1.375
base: 2
sign: 1
recreate: 5.5

For 0.1 we get:

sign (bin): 110011001100110011001101
sign (float): 1.600000023841858
base: -4
sign: 1
recreate: 0.10000000149011612

My point here is the ctype module we use to get binary value of fraction is a lot abstracted. I didn’t know what was happening behind the scene until read and understood the calculation. Here, let’s just dive into a simple algorithm to see binary representation of whole number and decimal number part of a fraction.

Let’s take 5.375 for example:

Whole number part = 5

Decimal number part: .375

For whole number part you do your normal divisibility by 2 and getting remainder part algorithm:

5%2 = 1

(5/2)%2 = 0

((5/2)/2)%2 = 1

Going bottom up, 5 (decimal) == 101 (binary).

Maybe I was not looking that hard on calculating decimal number part of a fraction, I was quite hunted by how they are calculated. After looking up, it was like oh yeah.

As we know when we move away from decimal point in decimal number part of a fraction, values are decreasing; for whole number part of a fraction, values are increasing.

So for decimal number part of a fraction, we multiply the number by 2 and get the whole number part of the product until we reach 0 in the decimal part or there is an infinite binary representation.

(0.375)*2 = 0.750

(0.750)*2 = 1.500

(0.500)*2 = 1.00

Going top down, 0.375 (decimal) = 011 (binary)

So 5.375 (decimal) == 101.011 (binary).

Let’s check the calculation:

5.375 = 1*2² + 0*2¹ + 1*2⁰ . 0*2^(-1) + 1*2^(-2) + 1*2^(-3)

5.375 = 4+0+1 . 0+0.25 + 0.125

5.375 = 5.375

No let’s try for 0.1

(0.1)*2 = 0.2

(0.2)*2 = 0.4

(0.4)*2 = 0.8

(0.8)*2 = 1.6

(0.6)*2 = 1.2

(0.2)*2 = 0.4

(0.4)*2 = 0.8

(0.8)*2 = 1.6

(0.6)*2 = 1.2

0.1 (decimal) == 0.000110011 (binary) with recurrence of 0011 infinitely. Hope this gives you more idea on how 0.1+0.1+0.1 != 0.3 (!!)

That’s all for this article. Hope it was a useful one and you now understand how floating point number are represented in binary.

Congratulations on the completion and thank you for reading! I’ll see you in my next article.

As I was working with binary (bit), came across an article on quantum computing (qubit). We are basically trying to be God?? or emulate nature. Goodluck saving them as well.

Reference:

--

--