c++ - When a float variable goes out of the float limits, what happens? -
i remarked 2 things:
std::numeric_limits<float>::max()+(a small number)
gives:std::numeric_limits<float>::max()
.std::numeric_limits<float>::max()+(a large number
like:std::numeric_limits<float>::max()/3)
gives inf.
why difference? 1 or 2 results in overflow , undefined behavior?
edit: code testing this:
1.
float d = std::numeric_limits<float>::max(); float q = d + 100; cout << "q: " << q << endl;
2.
float d = std::numeric_limits<float>::max(); float q = d + (d/3); cout << "q: " << q << endl;
formally, behavior undefined. on machine ieee floating point, however, overflow after rounding result in inf
. precision limited, however, , results after rounding of flt_max + 1
flt_max
.
you can see same effect values under flt_max
. try like:
float f1 = 1e20; // less flt_max float f2 = f1 + 1.0; if ( f1 == f2 ) ...
the if
evaluate true
, @ least ieee arithmetic. (there exist, or @ least have existed, machines float
has enough precision if
evaluate false
, aren't common today.)
Comments
Post a Comment