(root)/
mpfr-4.2.1/
tests/
tcot.c
       1  /* Test file for mpfr_cot.
       2  
       3  Copyright 2005-2023 Free Software Foundation, Inc.
       4  Contributed by the AriC and Caramba projects, INRIA.
       5  
       6  This file is part of the GNU MPFR Library.
       7  
       8  The GNU MPFR Library is free software; you can redistribute it and/or modify
       9  it under the terms of the GNU Lesser General Public License as published by
      10  the Free Software Foundation; either version 3 of the License, or (at your
      11  option) any later version.
      12  
      13  The GNU MPFR Library is distributed in the hope that it will be useful, but
      14  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
      15  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
      16  License for more details.
      17  
      18  You should have received a copy of the GNU Lesser General Public License
      19  along with the GNU MPFR Library; see the file COPYING.LESSER.  If not, see
      20  https://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc.,
      21  51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. */
      22  
      23  #include "mpfr-test.h"
      24  
      25  #define TEST_FUNCTION mpfr_cot
      26  #define REDUCE_EMAX 262143 /* otherwise arg. reduction is too expensive */
      27  #include "tgeneric.c"
      28  
      29  static void
      30  check_specials (void)
      31  {
      32    mpfr_t  x, y;
      33  
      34    mpfr_init2 (x, 123L);
      35    mpfr_init2 (y, 123L);
      36  
      37    mpfr_set_nan (x);
      38    mpfr_cot (y, x, MPFR_RNDN);
      39    if (! mpfr_nan_p (y))
      40      {
      41        printf ("Error: cot(NaN) != NaN\n");
      42        exit (1);
      43      }
      44  
      45    mpfr_set_inf (x, 1);
      46    mpfr_cot (y, x, MPFR_RNDN);
      47    if (! mpfr_nan_p (y))
      48      {
      49        printf ("Error: cot(Inf) != NaN\n");
      50        exit (1);
      51      }
      52  
      53    mpfr_set_inf (x, -1);
      54    mpfr_cot (y, x, MPFR_RNDN);
      55    if (! mpfr_nan_p (y))
      56      {
      57        printf ("Error: cot(-Inf) != NaN\n");
      58        exit (1);
      59      }
      60  
      61    /* cot(+/-0) = +/-Inf */
      62    mpfr_set_ui (x, 0, MPFR_RNDN);
      63    mpfr_cot (y, x, MPFR_RNDN);
      64    if (! (mpfr_inf_p (y) && mpfr_sgn (y) > 0))
      65      {
      66        printf ("Error: cot(+0) != +Inf\n");
      67        exit (1);
      68      }
      69    mpfr_neg (x, x, MPFR_RNDN);
      70    mpfr_cot (y, x, MPFR_RNDN);
      71    if (! (mpfr_inf_p (y) && mpfr_sgn (y) < 0))
      72      {
      73        printf ("Error: cot(-0) != -Inf\n");
      74        exit (1);
      75      }
      76  
      77    mpfr_clear (x);
      78    mpfr_clear (y);
      79  }
      80  
      81  static void
      82  two2emin (mpfr_exp_t e)
      83  {
      84    mpfr_exp_t old_emin, old_emax;
      85    mpfr_t x, y;
      86    int i, rnd;
      87  
      88    old_emin = mpfr_get_emin ();
      89    old_emax = mpfr_get_emax ();
      90  
      91    set_emin (-e);
      92    set_emax (e);
      93  
      94    mpfr_inits2 (53, x, y, (mpfr_ptr) 0);
      95    for (i = -4; i <= 4; i++)
      96      RND_LOOP (rnd)
      97        {
      98          mpfr_set_si (y, i, MPFR_RNDN);
      99          mpfr_ui_div (y, 1, y, (mpfr_rnd_t) rnd);  /* no overflow/underflow */
     100          mpfr_set_si_2exp (x, i, -e, MPFR_RNDN);
     101          if (ABS (i) != 3)  /* not a power of 2 (not 0 either) */
     102            mpfr_sub (y, y, x, (mpfr_rnd_t) rnd);  /* no overflow/underflow */
     103          mpfr_set_ui_2exp (x, 1, -e, MPFR_RNDN);
     104          mpfr_div (y, y, x, (mpfr_rnd_t) rnd);  /* 1/x - SIGN(x).epsilon */
     105          mpfr_set_si_2exp (x, i, -e, MPFR_RNDN);
     106          mpfr_cot (x, x, (mpfr_rnd_t) rnd);
     107          if (! mpfr_equal_p (x, y) && rnd != MPFR_RNDF)
     108            {
     109              printf ("Error in two2emin for i = %d and rnd = %s\n",
     110                      i, mpfr_print_rnd_mode ((mpfr_rnd_t) rnd));
     111              printf ("Got        ");
     112              mpfr_dump (x);
     113              printf ("instead of ");
     114              mpfr_dump (y);
     115              exit (1);
     116            }
     117        }
     118    mpfr_clears (x, y, (mpfr_ptr) 0);
     119  
     120    set_emin (old_emin);
     121    set_emax (old_emax);
     122  }
     123  
     124  int
     125  main (int argc, char *argv[])
     126  {
     127    tests_start_mpfr ();
     128  
     129    check_specials ();
     130    two2emin (256);
     131    two2emin (MPFR_EMAX_DEFAULT);
     132    if (MPFR_EMAX_MAX != MPFR_EMAX_DEFAULT)
     133      two2emin (MPFR_EMAX_MAX);
     134    test_generic (MPFR_PREC_MIN, 200, 5);
     135  
     136    tests_end_mpfr ();
     137    return 0;
     138  }