Results 1 to 2 of 2

Thread: tan(pi/2) overflow

  1. #1
    Join Date
    Jun 2008
    Posts
    8

    Default tan(pi/2) overflow

    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\n");
      else
        printf ("No overflow\n");
      printf ("%G\n",val1);
    #else
      printf ("FE_OVERFLOW not defined\n");
      printf ("%G\n",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.

  2. #2
    Join Date
    Nov 2009
    Location
    ND, USA
    Posts
    1,131

    Default Re: tan(pi/2) overflow

    On Fri April 8 2011 01:36 pm, GNW wrote:

    >
    > 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\n");
    > else
    > printf ("No overflow\n");
    > printf ("%G\n",val1);
    > #else
    > printf ("FE_OVERFLOW not defined\n");
    > printf ("%G\n",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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •