I get into a situation where calculating 1.77e-308/10
triggers an underflow exception, but calculating 1.777e-308/10
does not. This is strange because:
Underflow occurs when the true result of a floating point operation is smaller in magnitude (that is, closer to zero) than the smallest value representable as a normal floating point number in the target datatype (from Arithmetic Underflow, Wikipedia)
In other words, if we calculate x/y
where both x
and y
are double
, then underflow should occur if 0 < |x/y| < 2.2251e-308
(the smallest positive normalized double
is 2.2251e-308
). In theory, therefore, both 1.77e-308/10
and 1.777e-308/10
should trigger an underflow exception. The theory contradicts with what I have tested with the C program below.
#include <stdio.h>
#include <fenv.h>
#include <math.h>
int main(){
double x,y;
// x = 1.77e-308 => underflow
// x = 1.777e-308 gives ==> no underflow
x=1.77e-308;
feclearexcept(FE_ALL_EXCEPT);
y=x/10.0;
if (fetestexcept(FE_UNDERFLOW)) {
puts("Underflow\n");
}
else puts("No underflow\n");
}
To compile the program, I used gcc program.c -lm
; I also tried Clang, which gave me the same result. Any explanation?
[Edits] I have shared the code above via this online IDE.
1.77e-308
trigger an underflow while 1.777e-308;` doesn't.g++ (Debian 4.9.2-10) 4.9.2
– Barkergcc
also: still the same on my platform. AndDBL_MIN
is2.2251e-308
. – Barker