c++ - How does floating-point arithmetic work when one is added to a big number? -
if run code:
#include <iostream> int main () { using namespace std; float = 2.34e+22f; float b = a+1.0f; cout<<"a="<<a<<endl; cout<<"b-a"<<b-a<<endl; return 0; }
then result 0, because float number has 6 significant digits. float number 1.0 tries added 23 digit of number. so, how program realizes there no place number 1, algorithm?
step step:
ieee-754 32-bit binary floating-point format:
sign 1 bit significand 23 bits exponent 8 bits
i) float = 23400000000.f;
convert 23400000000.f
float
:
23,400,000,000 = 101 0111 0010 1011 1111 1010 1010 0000 00002 = 1.01011100101011111110101010000000002 • 234.
but significand can store 23 bits after point. must round:
1.01011100101011111110101 010000000002 • 234 ≈ 1.010111001010111111101012 • 234
so, after:
float = 23400000000.f;
a
equal 23,399,991,808.
ii) float b = + 1;
= 101011100101011111110101000000000002. b = 101011100101011111110101000000000012 = 1.01011100101011111110101000000000012 • 234.
but, again, significand can store 23 binary digits after point. must round:
1.01011100101011111110101 000000000012 • 234 ≈ 1.010111001010111111101012 • 234
so, after:
float b = + 1;
b
equal 23,399,991,808.
iii) float c = b - a;
101011100101011111110101000000000002 - 101011100101011111110101000000000002 = 0
this value can stored in float
without rounding.
so, after:
float c = b - a;
с
equal 0.
Comments
Post a Comment