43

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

5
  • 5
    Manually :) I can do it programatically after that. Commented Oct 17, 2010 at 18:06
  • Yes :) I need to know how to calculate the 0.25 to binary Commented Oct 17, 2010 at 18:13
  • What's your algorithm for the "12"? I think the same algorithm would work just the same for the ".25", with perhaps only the change of "2" to "1/2". Commented Oct 17, 2010 at 18:14
  • For the 12 I just keep on dividing it by 2 and get the remainders. Commented Oct 17, 2010 at 18:17
  • A deleted post below linked to a blog that has great intuition about how floating point numbers are stored on disk, and should be helpful. blog.penjee.com/binary-numbers-floating-point-conversion Commented Oct 25, 2017 at 11:42

6 Answers 6

38

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.

Sign up to request clarification or add additional context in comments.

Comments

31

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

2 Comments

@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).
@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() .)
15

(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).

3 Comments

What if my float is 5.1? Doing yours steps, I got into a infinite loop, please help!
@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.
@nautilusvn hm, yes, "while not zero" is a silly statement.. changing that.
7

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.

2 Comments

I know is old, but What's the value of i here?
Me I’m fine, I think you need to replace it into your Answer:))
1
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 

1 Comment

maybe you also want to add the reference to where you got that from. this python code looks familiar to me.
0
void transfer(double x) { unsigned long long * p = (unsigned long long * ) & x; for (int i = sizeof(unsigned long long) * 8 - 1; i >= 0; i--) { cout << (( * p) >> i & 1); } } 

1 Comment

Hi. Welcome on SO. Thank you for your code, but can you explain a bit how it solves the issue?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.