Firstly, you need to decide if the floating point numbers you are working with are "normalized" or not.
Think about how binary numbers are represented in scientific notation.
Consider the number 100101.010101
The expression given above is not given in scientific notation, but what if we change that?
To re-write 100101.010101
in scientific notation, we move the binary point to the left until we have $1.[\text{something}] * 2^{\text{some power}} $
For the specific example I gave, we have $(1.00101010101)_2*(2^5)_{10}$
Note that binary numbers written in scientific notation almost always begin as <one><dot><something>
As such, most computers today do not store the leading $1$. Normally, this leading $1$ would be the most significant bit of the mantissa. However, its existence is implied in normalized numbers. There is only one number whose binary scientific notation representation does not begin as "one-dot-something." That number, is zero. As such 1.0000[...] * 2 ^ [some large magnitude negative number]
represents the number zero in many computer systems today.
You say that you want to represent $0.3$ as a floating point number.
First, I think it will help to represent $(1/2), (1/4), (1/8), (1/16),$ etc... in decimal form.
$(1/2) = 0.5$
$(1/4) = 0.25$
$(1/8) = 0.125$
$(1/16) = 0.0625$
$(1/32) = 0.03125$
$(1/64) = 0.015625$
Okay; $0.3 = 0.25 + 0.05$
$0.25 = (1/4)$. $0.05$ is the leftover remainder.
$0.05 = 0.03125 + 0.01875$
$0.03125 = (1/32)$.
$0.01875$ is the leftover remainder.
Okay; $0.3 = 0.25 + 0.03125 + 0.015625 + 0.00029296875$
We have $0.3 = (1/4) + (1/32) + (1/64) + 0.00029296875$
$(0.3)_{10} = (0.010011)_{2} + $ a remainder of $(0.00029296875)_{10}$
You said that you only wanted a 4 bit mantissa. I think our leftovers/remainder are small enough at this point.
Let's write the number in scientific notation.
$(0.3)_{10} = (1.0011)_{2} *(2^{2})_{10} + $ a remainder of $(0.00029296875)_{10}$
I suppose we want the exponent in binary-format instead of decimal. We can do this:
$(0.3)_{10} \approx (1.0011)_{2} *(2)_{10}^{(100)_{2}}$
SIGN: POSITIVE
MANTISSA: 10011
EXPONENT: 100
You wanted a 4 bit mantissa, but I went out to 5 bits. We round the number to obtain that $0.3$ is represent-able as:
SIGN: POSITIVE
MANTISSA: 1001
EXPONENT: 100
A normalized float would omit the leading $1$ of the mantissa. However, simply explain in your homework, that it was not specified whether the float be normalized or non-normalized. Therefore, you chose to use non-normalized floating point numbers.
You asked about significant figures. There are many different definitions of "significant figure." All of these different definitions are about how many of the leftmost digits of the approximation are correct.
Imagine writing two numbers lined up on a sheet of paper
APROX: 0.00070384
EXACT: 0.00070392
Now, cover-up the rightmost digits with a blank sheet of paper
APROX: 0.0007
EXACT: 0.0007
They are both correct for the number 7
. That's good. slide the topmost sheet of paper to the right
APROX: 0.00070
EXACT: 0.00070
They're still the same. That's 2 significant figures so far.
APROX: 0.000703
EXACT: 0.000703
Still the same up to 3 figures.
APROX: 0.0007038
EXACT: 0.0007039
Uh oh; 8
and 9
are different. So the exact value, and the approximation are the same up to 3 significant figures, but not 4 significant figures.
The above method does not work very well.
Note that 0.499999999999999999999999999999
is a very good approximation of 0.5
However, if you use the sliding paper method, then not a single digit matches, and so, the approximation is not correct to even 1 significant figure!
My procedure for calculating significant figures will be different from the sliding paper method. The procedure is as follows:
- Put the exact value in scientific notation.
- Put the approximate value almost in scientific notation. However, make sure the approximate number has same exponential term as the exact value. Maybe the approximation is not of the form X.XXX Maybe the approximate number looks like 0.0XXXX or XXX.X
- Subtract the approximate from the exact, or vis versa.
- The number of significant digits is the number of zeros on the left.
For example,
0.000XXX
means 4 significant figures. 0.XXXXX
means 1 significant figure.
To calculate teh number of significant figures, convert your floating-point number back into decimal:
SIGN: POSITIVE
MANTISSA: 1001
EXPONENT: 100
$(1.0011^{100})_{2} = (0.28125)_{10}$
Notice that the floating point approximation is not the same as the exact value of $0.3$
Just how different are they?
$3.0 *\text{some exponent} - 2.8125 *\text{the same exponent} = 0.1875$
The floating point approximation of 0.3
is correct to 1 significant figure.
Repeat the process for -0.29
:
1. Calculate the floating point approximation.
2. Take the difference between the approximation and the exact value.
3. Count the number of leftward zeros in the difference.
$-0.29 = -(1/4 + 1/32 + 0.00875)$
Note that the remainder $0.00875$ is less than $(2^-6)$. Your mantissa is only 4 bits long, so we needn't worry about the remainder.
$-0.29 \approx -(1/4 + 1/32)$
$-0.29 \approx -0.28125$
$2.9 - 2.8125 = 0.0875$
The floating point approximation of 0.29 is correct to 2 significant figures.
The exact value of 0.3 + -0.29
is 0.01
.
Compute the following:
(float approx of 0.3) + (float approx of -0.29) =
(2.8125) + -0.28125) =
0
Then compare the approximate value (0
) to the exact value (0.1
) for the number of significant figures of $z = float(x_1) + float(x_2)$
1.0 - 0.0 = 1.0
The result has zero significant figures of accuracy.
Note that your textbook's definition of significant figures is probably different than mine. In your homework, explain to your teacher what definition you used. The concept of significant figures is more important that the details. Knowing roughly how many digits of the approximation are correct is more important than knowing exactly how to compute the number of significant figures.