How to convert float number to Binary?

131,352

Solution 1

Keep multiplying the number after decimal by 2 till it becomes 1.0:

0.25*2 = 0.50
0.50*2 = 1.00

and the result is in reverse order being .01

Solution 2

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.

Solution 3

(d means decimal, b means binary)

  1. 12.25d is your float.
  2. You write 12d in binary and remove it from your float. Only the remainder (.25d) will be left.
  3. You write the dot.
  4. While the remainder (0.25d) is not zero (and/or you want more digits), multiply it with 2 (-> 0.50d), remove and write the digit left of the dot (0), and continue with the new remainder (.50d).

Solution 4

The float value is stored in IEEE 754 format so we can't convert it directly like integer, char to binary.

But we can convert float to binary through a pointer.

#include <stdio.h>

int main()
{
    float a = 7.5;
    int i;
    int * p;

    p = &a;
    for (i = sizeof(int) * 8 - 1; i >= 0; i--)
    {   
        printf("%d", (*p) >> i & 1); 
    }   

    return 0;
}

Output

0 10000001 11100000000000000000000

Spaces added for clarification, they are not included as part of the program.

Solution 5

x = float(raw_input("enter number between 0 and 1: "))

p = 0
while ((2**p)*x) %1 != 0:
    p += 1
    # print p

    num = int (x * (2 ** p))
    # print num

    result = ''
    if num == 0:
        result = '0'
    while num > 0:
        result = str(num%2) + result
        num = num / 2

    for i in range (p - len(result)):
        result = '0' + result
    result = result[0:-p] + '.' + result[-p:]

print result #this will print result for the decimal portion
Share:
131,352
Slim Black
Author by

Slim Black

Updated on October 30, 2020

Comments

  • Slim Black
    Slim Black over 3 years

    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

  • Rick Regan
    Rick Regan over 13 years
    @Slim Black: Beware: that works fine for numbers like 0.25, which have exact representations in binary, but not for numbers like 0.1, which don't: 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, ... It repeats forever, and the result is 0.0(0011) (the part in parentheses repeats).
  • Rick Regan
    Rick Regan over 13 years
    @Slim Black: And note, to implement this correctly programmatically, you'll need decimal arithmetic -- see my article exploringbinary.com/base-conversion-in-php-using-bcmath, specifically section dec2bin_f() .)
  • nautilusvn
    nautilusvn almost 10 years
    What if my float is 5.1? Doing yours steps, I got into a infinite loop, please help!
  • comonad
    comonad almost 10 years
    @nautilusvn: because there is no power of 2 that is also a multiple of 10, that number has an infinite sequence of digits. you might want to abort your computation somewhere.
  • Kin Cheung
    Kin Cheung over 8 years
    maybe you also want to add the reference to where you got that from. this python code looks familiar to me.
  • comonad
    comonad almost 8 years
    @nautilusvn hm, yes, "while not zero" is a silly statement.. changing that.
  • Michi
    Michi about 7 years
    I know is old, but What's the value of i here?
  • Michi
    Michi about 6 years
    Me I’m fine, I think you need to replace it into your Answer:))
  • Krzysztof Madej
    Krzysztof Madej over 3 years
    Hi. Welcome on SO. Thank you for your code, but can you explain a bit how it solves the issue?