

02152006, 05:28 PM

#1

Elite Member
Join Date: Oct 2004
Posts: 12,974

How are floating points stored in memory?
Well with integers, if you have 8 bits, then the number 52 would look like the following:
00110100
But, how what would 5.23 look in memory as a 8bit floating point (is 8bit enough for that)? How does binary accomodate places after the decimal (tenths, hundredths, thousandths, etc.)??



02152006, 05:40 PM

#2

Diamond Member
Join Date: Nov 2001
Posts: 4,545

How are floating points stored in memory?
Computers can only store integers, so they need a way to represent floating point numbers. To do this, they represent parts of a floating point number using integers. See the following links:
Floating point numbers in general:
http://en.wikipedia.org/wiki/Floating_Point
IEEE754 standard for floating point number representation, the most popular standard by far:
http://en.wikipedia.org/wiki/IEEE_fl...point_standard



02152006, 05:48 PM

#3

Diamond Member
Join Date: Oct 2003
Posts: 8,808

How are floating points stored in memory?
Quote:
Originally posted by: xtknight
Well with integers, if you have 8 bits, then the number 52 would look like the following:
00110100
But, how what would 5.23 look in memory as a 8bit floating point (is 8bit enough for that)? How does binary accomodate places after the decimal (tenths, hundredths, thousandths, etc.)??

By using a floatingpoint number format.
It's actually simpler to consider fixedpoint binary numbers first; in this case, some of the bits are used for the part before the decimal point, and some of them are used for the part after the decimal point.
In any case, you have to treat the part after the decimal point as a sum of binary fractions. It's treated the same way as any other number base; the first digit after the decimal point is 2^(1), then 2^(2), then 2^(3), etc.
So, with a fixedpoint format with four bits for each 'side':
0100.0101
would be:
0 * 2^3 + 1 * 2^2 + 0 * 2^1 + 0 * 2^0 + 0 * 2 ^ (1) + 1 * 2 ^ (2) + 0 * 2 ^ (3) + 1 * 2 ^ (4)
or:
4 + 1/4 + 1/16
or 4.3125 in base 10.
A socalled 'floatingpoint' format uses some of the bits of the number to specify how many bits are on each side of the decimal point. The IEEE floatingpoint formats used in most of today's computers work like this, although it is usually interpreted as a fixedpoint real number 'mantissa' and an 'exponent' that is used to shift the decimal point (as in scientific notation).



02162006, 03:59 AM

#4

Platinum Member
Join Date: Oct 2000
Posts: 2,689

How are floating points stored in memory?
There's a IEEE specification for storing floating point numbers, which usually consists of 8 bytes of memory.



02162006, 11:09 AM

#5

Golden Member
Join Date: Oct 1999
Posts: 1,645

How are floating points stored in memory?
There are single precision floating points (float data type in C/C++) and double precision floating points (double data type in C/C++). Single precision requires 32 bits, and double precision requires 64 bits.
Floating point numbers are stored in this format:
m x b^e
Where m is the mantissa, an integer number, b is base (in our case 2) and e is exponent.
For single precision floating point, we have the following:
1 sign bit
8 exponent bits (biased by 127)
23 mantissa bits
For double precision floating point, we have:
1 sign bit
11 exponent bits (biased by 1023)
52 mantissa bits
To make a number, say 143.40625 into single precision floating point, first let's do its binary representation with the decimal point in there: 10001111.01101 (note, each bit after the decimal point is 1/2, 1/4, 1/8, etc.).
Then we justify it so that it's 1.xxxxx * 2^x, so we move the decimal place over 7 places: 1.000111101101 * 2^7.
so our exponent is 7, but it needs to be biased by 127 (this is done so that we can support really tiny numbers, e.g. what if we had a fraction with a negative exponent of 104...can't write 104 in normal binary, so we would bias it by 127 and the exponent would become 23). In our case, our exponent is now 134, or 10000110 in binary.
Our mantissa is 000111101101, filled out with zeroes to a length of 23 bits, it's 00011110110100000000000.
Our sign bit is 1 because it's a negative number, so we get:
1 10000110 00011110110100000000000
or 0xC30F6800 in hexadecimal.
Hope that made sense...
__________________
Yes I am a medical doctor.
No I can't answer your medical question.



02162006, 09:00 PM

#6

Elite Member
Join Date: Oct 2004
Posts: 12,974

How are floating points stored in memory?
Thanks for the explanations guys, especially interchange! I understand it now for the most part. But what is bias?



02162006, 09:14 PM

#7

Diamond Member
Join Date: Oct 2005
Location: Texas
Posts: 9,836

How are floating points stored in memory?
Quote:
Originally posted by: xtknight
Thanks for the explanations guys, especially interchange! I understand it now for the most part. But what is bias?

Normally, negative numbers are represented by designating the MSb as being the sign bit. This solves the problem of differentiating between positive and negative numbers but halves the range of possible values. To solve this, the exponent is biased by 127 or 1023 such that it is always positive. So if your exponent represented in memory is 0, then the actual exponent is 127 or 1023 depending upon the precision.



02162006, 10:57 PM

#8

Diamond Member
Join Date: Sep 2005
Posts: 4,507

How are floating points stored in memory?
Quote:
Originally posted by: Born2bwire
Quote:
Originally posted by: xtknight
Thanks for the explanations guys, especially interchange! I understand it now for the most part. But what is bias?

Normally, negative numbers are represented by designating the MSb as being the sign bit. This solves the problem of differentiating between positive and negative numbers but halves the range of possible values. To solve this, the exponent is biased by 127 or 1023 such that it is always positive. So if your exponent represented in memory is 0, then the actual exponent is 127 or 1023 depending upon the precision.

Most of the time negitive numbers are represented in 2's complement format because it allows the same hardware to do addition and subtraction. Using a sign bit wastes only one value, there is both a plus and minus zero. The bias is used for no good reason.



02162006, 11:35 PM

#9

Elite Member
Join Date: Oct 2004
Posts: 12,974

How are floating points stored in memory?
Sorry, I'm not exceptionally strong in mathematics and I'm having a hard time finding out what the term 'bias' means in this context. Is it just a multiplier or divisor or what?



02172006, 10:06 AM

#10

Diamond Member
Join Date: Oct 2005
Location: Texas
Posts: 9,836

How are floating points stored in memory?
Quote:
Originally posted by: smack Down
Quote:
Originally posted by: Born2bwire
Quote:
Originally posted by: xtknight
Thanks for the explanations guys, especially interchange! I understand it now for the most part. But what is bias?

Normally, negative numbers are represented by designating the MSb as being the sign bit. This solves the problem of differentiating between positive and negative numbers but halves the range of possible values. To solve this, the exponent is biased by 127 or 1023 such that it is always positive. So if your exponent represented in memory is 0, then the actual exponent is 127 or 1023 depending upon the precision.

Most of the time negitive numbers are represented in 2's complement format because it allows the same hardware to do addition and subtraction. Using a sign bit wastes only one value, there is both a plus and minus zero. The bias is used for no good reason.

Did I write that crap? Geez, that's it. New rule. No more posting when I've had only four hours of sleep. Obviously while the maximum value of a number with a sign bit is half of what it was originally, you have both the positive and negative range which effectively doubles the number of values. Thus, the sign bit decreases the absolute value represented but does not decrease the range of values. Go college education. :roll:
Bias is just the term they use to mean offset. Every exponential number stored in memory is the sum of the actual exponent and the bias number. Hence, 2^10 would have the exponent of 137 (for single precision) stored as the exponent in memory.



02172006, 10:43 AM

#11

Golden Member
Join Date: Oct 1999
Posts: 1,645

How are floating points stored in memory?
Quote:
Originally posted by: Born2bwire
Most of the time negitive numbers are represented in 2's complement format because it allows the same hardware to do addition and subtraction. Using a sign bit wastes only one value, there is both a plus and minus zero. The bias is used for no good reason.

Did I write that crap? Geez, that's it. New rule. No more posting when I've had only four hours of sleep. Obviously while the maximum value of a number with a sign bit is half of what it was originally, you have both the positive and negative range which effectively doubles the number of values. Thus, the sign bit decreases the absolute value represented but does not decrease the range of values. Go college education. :roll:
Bias is just the term they use to mean offset. Every exponential number stored in memory is the sum of the actual exponent and the bias number. Hence, 2^10 would have the exponent of 137 (for single precision) stored as the exponent in memory.[/quote]
I'm not sure why they don't use 2's complement for the exponent. It works as well as having an offset. The range would be 128 to 127...whereas the range with a 127 offset is 127 to 128. Oh well...it's just the method they went with to ensure that you can have negative exponents to represent very tiny numbers.
__________________
Yes I am a medical doctor.
No I can't answer your medical question.



02172006, 10:56 AM

#12

Elite Member
Join Date: Jul 2000
Posts: 9,214

How are floating points stored in memory?
Quote:
I'm not sure why they don't use 2's complement for the exponent. It works as well as having an offset. The range would be 128 to 127...whereas the range with a 127 offset is 127 to 128. Oh well...it's just the method they went with to ensure that you can have negative exponents to represent very tiny numbers.

The nice thing about the way they do it is you can check for > and < with standard 2's complement comparison logic, because the bit pattern for a larger floating point number always represents a larger 2's complement integer.
__________________
*Not speaking for any company



02172006, 08:55 PM

#13

Diamond Member
Join Date: Sep 2005
Posts: 4,507

How are floating points stored in memory?
Quote:
Originally posted by: CTho9305
Quote:
I'm not sure why they don't use 2's complement for the exponent. It works as well as having an offset. The range would be 128 to 127...whereas the range with a 127 offset is 127 to 128. Oh well...it's just the method they went with to ensure that you can have negative exponents to represent very tiny numbers.

The nice thing about the way they do it is you can check for > and < with standard 2's complement comparison logic, because the bit pattern for a larger floating point number always represents a larger 2's complement integer.

I don't think that is true but I haven't looked in detail how floating point defines < > wouldn't plus and minus zero be difened as equal but they have different bit parterns so an interger comparision would give a not equals result.



02182006, 10:48 AM

#14

Elite Member
Join Date: Jul 2000
Posts: 9,214

How are floating points stored in memory?
Quote:
Originally posted by: smack Down
Quote:
Originally posted by: CTho9305
Quote:
I'm not sure why they don't use 2's complement for the exponent. It works as well as having an offset. The range would be 128 to 127...whereas the range with a 127 offset is 127 to 128. Oh well...it's just the method they went with to ensure that you can have negative exponents to represent very tiny numbers.

The nice thing about the way they do it is you can check for > and < with standard 2's complement comparison logic, because the bit pattern for a larger floating point number always represents a larger 2's complement integer.

I don't think that is true but I haven't looked in detail how floating point defines < > wouldn't plus and minus zero be difened as equal but they have different bit parterns so an interger comparision would give a not equals result.

Will a correct implementation produce negative zero?
__________________
*Not speaking for any company



02192006, 12:10 PM

#15

Diamond Member
Join Date: Sep 2005
Posts: 4,507

How are floating points stored in memory?
Quote:
Originally posted by: CTho9305
Quote:
Originally posted by: smack Down
Quote:
Originally posted by: CTho9305
Quote:
I'm not sure why they don't use 2's complement for the exponent. It works as well as having an offset. The range would be 128 to 127...whereas the range with a 127 offset is 127 to 128. Oh well...it's just the method they went with to ensure that you can have negative exponents to represent very tiny numbers.

The nice thing about the way they do it is you can check for > and < with standard 2's complement comparison logic, because the bit pattern for a larger floating point number always represents a larger 2's complement integer.

I don't think that is true but I haven't looked in detail how floating point defines < > wouldn't plus and minus zero be difened as equal but they have different bit parterns so an interger comparision would give a not equals result.

Will a correct implementation produce negative zero?

It is a valid floating point number, so a correct implementation can produce negitive zero. It is also valid to turn any negitive zeros into a positive zeros so an implementation could never produce negitive zero but it still would need to have the hardware to ensure 0=+0



02192006, 04:39 PM

#16

Elite Member
Join Date: Jul 2000
Posts: 9,214

How are floating points stored in memory?
I guess you have to specialcase negative 0.
__________________
*Not speaking for any company



02192006, 05:28 PM

#17

Diamond Member
Join Date: Oct 2005
Location: Texas
Posts: 9,836

How are floating points stored in memory?
Quote:
Originally posted by: CTho9305
I guess you have to specialcase negative 0.

Not really. The IEEE floating point standard, which is what is generally used, requires that the leading term of the mantissa to implicitly incorporate 1. That is, if the number to be represented in binary is: 101.01001... etc, then the exponent is 129 (base 10) and the mantissa is represented as .0101001... The leading one is not expressed in the mantissa because it is assumed to always be implied. This way, we can save on a digit and increase the precision of our number. This is a small detail that I do not think anyone has stated here previously. Hence, there is no way to express zero in floating point. What they do is that they specify a special number to represent zero, just as they use a special number to represent infinite, NAN (not a number), and I recall that there is a third error number that comes up as well (IDE, I think it means indefinite. It comes up for me if I multiply an infinite number by 0 I believe). Anyways, the point is, there is no positive or negative zero, just a special case that is 0.



02192006, 06:22 PM

#18

Diamond Member
Join Date: Sep 2005
Posts: 4,507

How are floating points stored in memory?
Quote:
Originally posted by: Born2bwire
Quote:
Originally posted by: CTho9305
I guess you have to specialcase negative 0.

Not really. The IEEE floating point standard, which is what is generally used, requires that the leading term of the mantissa to implicitly incorporate 1. That is, if the number to be represented in binary is: 101.01001... etc, then the exponent is 129 (base 10) and the mantissa is represented as .0101001... The leading one is not expressed in the mantissa because it is assumed to always be implied. This way, we can save on a digit and increase the precision of our number. This is a small detail that I do not think anyone has stated here previously. Hence, there is no way to express zero in floating point. What they do is that they specify a special number to represent zero, just as they use a special number to represent infinite, NAN (not a number), and I recall that there is a third error number that comes up as well (IDE, I think it means indefinite. It comes up for me if I multiply an infinite number by 0 I believe). Anyways, the point is, there is no positive or negative zero, just a special case that is 0.

The IEEE standard defines two zeros. Positive zero is defend as all bit in the number are zero and negitive zero is defined as all zeros except that the sign bit is set. They are both special cases of zero defined by the IEEE standard.
Having two zeros makes calculating the sign bit quicker. For multiplication it will always be the xor of the two sign bits.



02192006, 07:19 PM

#19

Lifer
Join Date: Oct 2002
Posts: 10,570

How are floating points stored in memory?
Quote:
Originally posted by: Born2bwire
Quote:
Originally posted by: CTho9305
I guess you have to specialcase negative 0.

Not really. The IEEE floating point standard, which is what is generally used, requires that the leading term of the mantissa to implicitly incorporate 1. That is, if the number to be represented in binary is: 101.01001... etc, then the exponent is 129 (base 10) and the mantissa is represented as .0101001... The leading one is not expressed in the mantissa because it is assumed to always be implied. This way, we can save on a digit and increase the precision of our number. This is a small detail that I do not think anyone has stated here previously. Hence, there is no way to express zero in floating point. What they do is that they specify a special number to represent zero, just as they use a special number to represent infinite, NAN (not a number), and I recall that there is a third error number that comes up as well (IDE, I think it means indefinite. It comes up for me if I multiply an infinite number by 0 I believe). Anyways, the point is, there is no positive or negative zero, just a special case that is 0.

Then again there are cases of denormalized numbers where the leading one is not present because the number is too small. For example, assuming the exponent can only obtain values of +127 to 127, the number 0.00001 x 2^127 will not have the leading one but at the same time will not be rounded to zero since we still have enough precision to represent it. But you are correct that the typical implementation assumes a leading 1 and requires a case to detect denormalized cases like the above.
__________________
post count = post count + 0.999.....
(\__/)
(='.'=)This is Bunny. Copy and paste bunny into your
(")_(")signature to help him gain world domination.



Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off



All times are GMT 5. The time now is 09:38 AM.
