Floating point computer - Trouble with getting back correct results
Having trouble with a challenge.
Let's say I have a th开发者_运维问答eoretical, base 10, floating point calculator with the following characteristics
- Only 3 digits for mantissa
- 1 digit for exponent
- Sign for mantissa and exponent
How would this machine compute the following?
300 + \sum_{i=1}^{100} 0.2
The correct result is 320. The machine's result is 300. But why? Can't get where the 20 goes goes missing...
Thanks for your time.
Notwithstanding davin's answer, the number 0.2 is not 0: 2e-1 is representable in your system. What's happening here is that 300.2 is not representable to full precision, so 300 + 0.2 gets rounded down to 300. If you do this 100 times, you still end up with 300 :-)
Your system should be able to compute 300 + 100*0.2 correctly, however.
The number 0.2 in this calculator is 0, because it should evaluate to 2 x 10^(-1), although the exponent has only 1 digit, which allows a range of 0 and 1, so -1 is outside its range. The underflow results in a truncation, which produces 0.
So if we sum 0 one hundred times and add the result to 300, we get 300.
精彩评论