Skip to main content
GameDev.net gamedev.net
🔒 Locked

floating point binary representation

Started by alnite Sep 20, 2004 at 6:19 PM 9 replies 1.4k views
Original Post
alnite
alnite
This is actually a homework question, but I am having trouble converting decimal number to its binary representation. Most examples and articles on the web use easy numbers like 7.5 or 8.875 that the decimals (the numbers after the point) are a fraction with a 2n denominator. (1) 7.5 = 7 1/2 = 111.1 (2) 8.875 = 8 7/8 = 1000.111 This is how I read it: For (1), the fraction can be read as 1 divided by 21, where 1 is the amount of binary digits to the right of the decimal point. For (2), the fraction can be read as 7 divided by 23. There are 3 digits to the right of the decimal point. Now, my teacher gave me 2.2. 2.2 is 2 1/5. So, using that rule, the binary representation should be...er I don't know. 5 can't be represented as 2n format. I know its 101. I mean it can't be represented by one single 2n number. If I put 10.10, that is 2.5, not 2.2. So...help?
smart_idiot
smart_idiot
2^2  2^1  2^0  2^-1  2^-2  ?    ?    ?.    ?     ?


How many 2^2's are there? write that down, subtract the value.
How many 2^1's are there? Write that down, subtract the value.
How many 2^0's are there? Write that down, etc, etc.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
capn_midnight
capn_midnight
or you could do it the mantissa/exponent way. Convert the number to scientific notation, store the mantissa and exponent as integers. This is how it is usually done.

For example,
234.4356
2.344356 x 102
mantissa=2344356
exponent=2
Sneftel
Sneftel
Rational numbers that are not exactly representable in a particular base end in an infinitely repeating series of digits. For instance, 1/6 is 0.166666, with the 6 repeating. Likewise, rational numbers not exactly representable in binary will have a repeating series of 0's and 1's.
alnite
alnite
Quote:
Original post by smart_idiot
2^2  2^1  2^0  2^-1  2^-2  ?    ?    ?.    ?     ?


How many 2^2's are there? write that down, subtract the value.
How many 2^1's are there? Write that down, subtract the value.
How many 2^0's are there? Write that down, etc, etc.


The problem is you can't represent 1/5 as a sum of 1/2n.
Or at least I can't.


@capn_midnight:
IEEE problems are in another section. This one I can't use IEEE.
alnite
alnite
Quote:
Original post by Sneftel
Rational numbers that are not exactly representable in a particular base end in an infinitely repeating series of digits. For instance, 1/6 is 0.166666, with the 6 repeating. Likewise, rational numbers not exactly representable in binary will have a repeating series of 0's and 1's.

So, how I do find this pattern?

Using 1/6 as an example:
In base 10, 6 is 6*100.

Not sure where they get the 1 from.
smart_idiot
smart_idiot
The answer involves 0011 repeating a whole bunch of times. . .
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other
alnite
alnite
Quote:
Original post by smart_idiot
The answer involves 0011 repeating a whole bunch of times. . .

but why 0011?

anyway, brb, I have a class now. I am late 15 minutes. will check this thread later this evening.
Sneftel
Sneftel
Well, there's a couple of ways. The first, and easiest, way is to just calculate out a bunch of decimals and look for patterns. Let's do it for 1/6th:

0.166666666 is LESS than 1/2, so we put in a 0: .0
0.166666666 is LESS than 1/4, so we put in a 0: .00
0.166666666 is MORE than 1/8, so we put in a 1: .001
...Now we subtract 1/8, leaving 0.0416666666
0.041666666 is LESS than 1/16, so we put in a 0: .0010
0.041666666 is MORE than 1/32, so we put in a 1: .00101
...Now we subtract 1/32, leaving 0.010416666
0.010416666 is LESS than 1/64, so we put in a 0: .001010
0.010416666 is MORE than 1/128, so we put in a 1: .0010101

You can probably see the pattern now: starting with the eighths, it repeats 10.

The other way involves series summations and I don't recall the exact details.
Pouya
Pouya
Quote:
Original post by alnite
[...] brb, [...] will check this thread later this evening.

"later this evening" is not quite "right back" you know.
smart_idiot
smart_idiot
#include <stdio.h>#include <math.h>int main(int argc, char *argv[]) {  double value = 2.2;    int digit = 5;  for(; digit > -30 ; --digit)   {    int number = floor(value / pow(2, digit));    value -= number * pow(2, digit);        fputc('0'+number, stdout);        if(digit == 0)     fputc('.', stdout);   }    return 0; }


Output: 000010.00110011001100110011001100110

Good enough for me. *vanishes in a puff of logic*
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.