jose luis fernandez diaz wrote:
Hi,
I have made the program below to try to understand how a floating
point number is made by my computer (HP-UX cronos B.11.11 U 9000/800):
Your program won't help you in understanding what's the deal with
floating point numbers and why they have a greater range then eg. long.
Let us simplify the whole thing in a more familiar environment.
Assume you are my 'computer'. But I will allow you to calulate
with 5 digits only (and no sign for simplicity)
If you use those 5 digits for 'long' only , you can use those 5
digits to count from 00000 to 99999, so your range is 100000 numbers.
But you can do different also. You can divide the available 5 digits
into a mantissa and an exponent. Say you use 2 digits for the exponent
and the remaining 3 digits for the mantissa (you are familiar with
sientific notation, are you?). So eg. a number 100 can be represented
in various ways:
number scientific not. our notation
*************** *************** ********
100 100 * 10^0 100 00
100 10 * 10^1 010 01
100 1 * 10^2 001 02
Note: in the column 'our notation', no representation uses more then
5 digits which fits perfectly to what I allowed to you :-)
Now lets see some other numbers.
Say you need to represent 1000 in 'out notation'. How can you do that?
Well you can eg. do 001 03. That would be 1 * 10^3. Or you could do
010 02 (10 * 10^2), or 100 01 (100 * 10^1). Fine. But now try to represent
1001 in 'out notation'. You can't. That's because the only way to get there
would be to increment the mantissa. Let's see whats happening then:
(1000) 001 03 -> (~1001) 002 03 -> 2 * 10^3 -> 2000
(1000) 010 02 -> (~1001) 011 02 -> 11 * 10^2 -> 1100
(1000) 100 01 -> (~1001) 101 01 -> 101 * 10^1 -> 1010
You see, using only 5 digits, it is impossible to get at the number 1001. You can
get 1000 and you can get 1010, but no numbers inbetween. It follows from the fact
that you reserve some of the available 5 digits to represent an exponent, which
drops the 'range' of numbers representable with the remaining 3 digits. But you
get the freedome to move the comma around by fiddeling with the exponent. Using
those 5 digit 'scientific notation' it is easy to represent
1000 -> 100 01
10000 -> 100 02
100000 -> 100 03
1000000 -> 100 04
10000000 -> 100 05
Right now we are way 'over' the upper limit for 5-digit-long, which was
99999. But we bought this by reducing the granularity. We cannot represent
all numbers in 10 millions, just some of them.
100 05 -> 10000000
101 05 -> 10100000
All numbers between 10000000 and 10100000 are not representable by our 5 digit
scheme but need to be approximated by either 10000000 or 10100000.
Back to your computer. It doesn't use only 5 digits, but much more. Also the
exponent is not taken to base 10, but to base 2. This reduces the effect you
just saw. But the principle is still the same. You buy the greater range
by reducing the granularity.
--
Karl Heinz Buchegger
kb******@gascad .at