“Mathematically, the tangent function has singularities at odd multiples of pi/2. If the
argument x is too close to one of these singularities, tan will signal
overflow.”

the next code:

#include <stdio.h>
#include <math.h>
#include <fenv.h>
int
main (void)
{
double val1;
int raised;
#if defined(FE_OVERFLOW)
feclearexcept (FE_ALL_EXCEPT);
val1 = tan (M_PI/2);
raised = fetestexcept (FE_OVERFLOW);
if (raised)
printf ("Overflow
");
else
printf ("No overflow
");
printf ("%G
",val1);
#else
printf ("FE_OVERFLOW not defined
");
printf ("%G
",tan(M_PI/2));
#endif
return 0;
}

compiled this way:

gcc -Wall -lm -g -o test-float.out test-float.c

is giving this output:

> ./test-float.out
No overflow
1.63312E+16

So, anyone knows how can I get tan(pi/2) to raise the overflow exception.

>
> Hello.
>
> Though the GNU libc manual says:
>
> “Mathematically, the tangent function has singularities at odd
> multiples of pi/2. If the
> argument x is too close to one of these singularities, tan will signal
> overflow.”
>
> the next code:
>
>
> Code:
> --------------------
>
> #include <stdio.h>
> #include <math.h>
> #include <fenv.h>
>
> int
> main (void)
> {
> double val1;
> int raised;
>
> #if defined(FE_OVERFLOW)
> feclearexcept (FE_ALL_EXCEPT);
> val1 = tan (M_PI/2);
> raised = fetestexcept (FE_OVERFLOW);
> if (raised)
> printf ("Overflow
");
> else
> printf ("No overflow
“);
> printf (”%G
",val1);
> #else
> printf ("FE_OVERFLOW not defined
“);
> printf (”%G
",tan(M_PI/2));
> #endif
>
> return 0;
> }
>
> --------------------
>
>
>
> compiled this way:
>
> Code:
> --------------------
>
> gcc -Wall -lm -g -o test-float.out test-float.c
>
> --------------------
>
> is giving this output:
>
>> ./test-float.out
> No overflow
> 1.63312E+16
>
> So, anyone knows how can I get tan(pi/2) to raise the overflow
> exception.
>
> Thanks.
>
>
GNW;

Keep in mind that M_PI is only an approximation of PI. I’m not sure of the
exact algorithm that C uses for the tangent function, but this again can only
yield approximate values. None of the transcendental functions can be
computed exactly. This write up will give you a bit more information: http://en.wikipedia.org/wiki/Floating_point
Notice the section “Accuracy Problems” addresses this exact issue. It might
be illuminating to you if you alter your program a bit to calculate
cos(M_PI/2). I’ll bet the result is not 0.

P. V.
“We’re all in this together, I’m pulling for you.” Red Green