Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert float number to Binary?

Can anyone please tell me how can I convert this float number: 12.25 to binary? I know how to convert the "12" but not the 0.25

Any help is much appreciated. Thanks

like image 673
Slim Black Avatar asked Oct 17 '10 17:10

Slim Black


People also ask

How are float values represented in binary?

The sign of a binary floating-point number is represented by a single bit. A 1 bit indicates a negative number, and a 0 bit indicates a positive number. Before a floating-point binary number can be stored correctly, its mantissa must be normalized.

Can you convert float to binary in Python?

Python doesn't provide any inbuilt method to easily convert floating point decimal numbers to binary number.


1 Answers

Consider below example

Convert 2.625 to binary.

We will consider the integer and fractional part separately.

The integral part is easy, 2 = 10.  

For the fractional part:

0.625   × 2 =   1.25    1   Generate 1 and continue with the rest. 0.25    × 2 =   0.5     0   Generate 0 and continue. 0.5     × 2 =   1.0     1   Generate 1 and nothing remains. 

So 0.625 = 0.101, and 2.625 = 10.101.

See this link for more information.

like image 188
Govind Prabhu Avatar answered Oct 13 '22 19:10

Govind Prabhu