(root)/
mpfr-4.2.1/
tests/
tcbrt.c
       1  /* Test file for mpfr_cbrt.
       2  
       3  Copyright 2002-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  static void
      26  special (void)
      27  {
      28    mpfr_t x, y;
      29    int inex;
      30  
      31    mpfr_init (x);
      32    mpfr_init (y);
      33  
      34    /* cbrt(NaN) = NaN */
      35    mpfr_set_nan (x);
      36    inex = mpfr_cbrt (y, x, MPFR_RNDN);
      37    if (!mpfr_nan_p (y))
      38      {
      39        printf ("Error: cbrt(NaN) is not NaN\n");
      40        exit (1);
      41      }
      42    if (inex != 0)
      43      {
      44        printf ("Error: cbrt(NaN): incorrect ternary value %d\n", inex);
      45        exit (1);
      46      }
      47  
      48    /* cbrt(+Inf) = +Inf */
      49    mpfr_set_inf (x, 1);
      50    inex = mpfr_cbrt (y, x, MPFR_RNDN);
      51    if (!mpfr_inf_p (y) || mpfr_sgn (y) < 0)
      52      {
      53        printf ("Error: cbrt(+Inf) <> +Inf\n");
      54        exit (1);
      55      }
      56    if (inex != 0)
      57      {
      58        printf ("Error: cbrt(+Inf): incorrect ternary value %d\n", inex);
      59        exit (1);
      60      }
      61  
      62    /* cbrt(-Inf) =  -Inf */
      63    mpfr_set_inf (x, -1);
      64    inex = mpfr_cbrt (y, x, MPFR_RNDN);
      65    if (!mpfr_inf_p (y) || mpfr_sgn (y) > 0)
      66      {
      67        printf ("Error: cbrt(-Inf) <> -Inf\n");
      68        exit (1);
      69      }
      70    if (inex != 0)
      71      {
      72        printf ("Error: cbrt(-Inf): incorrect ternary value %d\n", inex);
      73        exit (1);
      74      }
      75  
      76    /* cbrt(+/-0) =  +/-0 */
      77    mpfr_set_ui (x, 0, MPFR_RNDN);
      78    inex = mpfr_cbrt (y, x, MPFR_RNDN);
      79    if (MPFR_NOTZERO (y) || MPFR_IS_NEG (y))
      80      {
      81        printf ("Error: cbrt(+0) <> +0\n");
      82        exit (1);
      83      }
      84    if (inex != 0)
      85      {
      86        printf ("Error: cbrt(+0): incorrect ternary value %d\n", inex);
      87        exit (1);
      88      }
      89    mpfr_neg (x, x, MPFR_RNDN);
      90    inex = mpfr_cbrt (y, x, MPFR_RNDN);
      91    if (MPFR_NOTZERO (y) || MPFR_IS_POS (y))
      92      {
      93        printf ("Error: cbrt(-0) <> -0\n");
      94        exit (1);
      95      }
      96    if (inex != 0)
      97      {
      98        printf ("Error: cbrt(-0): incorrect ternary value %d\n", inex);
      99        exit (1);
     100      }
     101  
     102    mpfr_set_prec (x, 53);
     103    mpfr_set_str (x, "8.39005285514734966412e-01", 10, MPFR_RNDN);
     104    mpfr_cbrt (x, x, MPFR_RNDN);
     105    if (mpfr_cmp_str1 (x, "9.43166207799662426048e-01"))
     106      {
     107        printf ("Error in crbrt (1)\n");
     108        exit (1);
     109      }
     110  
     111    mpfr_set_prec (x, 32);
     112    mpfr_set_prec (y, 32);
     113    mpfr_set_str_binary (x, "0.10000100001100101001001001011001");
     114    mpfr_cbrt (x, x, MPFR_RNDN);
     115    mpfr_set_str_binary (y, "0.11001101011000100111000111111001");
     116    if (mpfr_cmp (x, y))
     117      {
     118        printf ("Error in cbrt (2)\n");
     119        exit (1);
     120      }
     121  
     122    mpfr_set_prec (x, 32);
     123    mpfr_set_prec (y, 32);
     124    mpfr_set_str_binary (x, "-0.1100001110110000010101011001011");
     125    mpfr_cbrt (x, x, MPFR_RNDD);
     126    mpfr_set_str_binary (y, "-0.11101010000100100101000101011001");
     127    if (mpfr_cmp (x, y))
     128      {
     129        printf ("Error in cbrt (3)\n");
     130        exit (1);
     131      }
     132  
     133    mpfr_set_prec (x, 82);
     134    mpfr_set_prec (y, 27);
     135    mpfr_set_str_binary (x, "0.1010001111011101011011000111001011001101100011110110010011011011011010011001100101e-7");
     136    mpfr_cbrt (y, x, MPFR_RNDD);
     137    mpfr_set_str_binary (x, "0.101011110001110001000100011E-2");
     138    if (mpfr_cmp (x, y))
     139      {
     140        printf ("Error in cbrt (4)\n");
     141        exit (1);
     142      }
     143  
     144    mpfr_set_prec (x, 204);
     145    mpfr_set_prec (y, 38);
     146    mpfr_set_str_binary (x, "0.101000000001101000000001100111111011111001110110100001111000100110100111001101100111110001110001011011010110010011100101111001111100001010010100111011101100000011011000101100010000000011000101001010001001E-5");
     147    mpfr_cbrt (y, x, MPFR_RNDD);
     148    mpfr_set_str_binary (x, "0.10001001111010011011101000010110110010E-1");
     149    if (mpfr_cmp (x, y))
     150      {
     151        printf ("Error in cbrt (5)\n");
     152        exit (1);
     153      }
     154  
     155    /* Bug (in the compiler?) found on Linux/m68k with gcc 4.0.2 */
     156    mpfr_set_prec (x, 5);
     157    mpfr_set_prec (y, 5);
     158    mpfr_set_str_binary (x, "1.1000E-2");
     159    mpfr_cbrt (y, x, MPFR_RNDN);
     160    mpfr_set_str_binary (x, "1.0111E-1");
     161    if (mpfr_cmp (x, y))
     162      {
     163        printf ("Error in cbrt (6)\n");
     164        exit (1);
     165      }
     166  
     167    mpfr_clear (x);
     168    mpfr_clear (y);
     169  }
     170  
     171  #define TEST_FUNCTION mpfr_cbrt
     172  #include "tgeneric.c"
     173  
     174  static int
     175  cube (mpfr_ptr a, mpfr_srcptr b, mpfr_rnd_t rnd_mode)
     176  {
     177    return mpfr_pow_ui (a, b, 3, rnd_mode);
     178  }
     179  
     180  int
     181  main (void)
     182  {
     183    mpfr_t x;
     184    int r, inex;
     185    mpfr_prec_t p;
     186  
     187    tests_start_mpfr ();
     188  
     189    special ();
     190  
     191    mpfr_init (x);
     192  
     193    for (p = 2; p < 100; p++)
     194      {
     195        mpfr_set_prec (x, p);
     196        RND_LOOP (r)
     197          {
     198            mpfr_set_ui (x, 1, MPFR_RNDN);
     199            inex = mpfr_cbrt (x, x, (mpfr_rnd_t) r);
     200            if (mpfr_cmp_ui (x, 1) || inex != 0)
     201              {
     202                printf ("Error in mpfr_cbrt for x=1, rnd=%s\ngot ",
     203                        mpfr_print_rnd_mode ((mpfr_rnd_t) r));
     204                mpfr_dump (x);
     205                printf ("inex = %d\n", inex);
     206                exit (1);
     207              }
     208  
     209            mpfr_set_si (x, -1, MPFR_RNDN);
     210            inex = mpfr_cbrt (x, x, (mpfr_rnd_t) r);
     211            if (mpfr_cmp_si (x, -1) || inex != 0)
     212              {
     213                printf ("Error in mpfr_cbrt for x=-1, rnd=%s\ngot ",
     214                        mpfr_print_rnd_mode ((mpfr_rnd_t) r));
     215                mpfr_dump (x);
     216                printf ("inex = %d\n", inex);
     217                exit (1);
     218              }
     219  
     220            if (p >= 5)
     221              {
     222                int i;
     223                for (i = -12; i <= 12; i++)
     224                  {
     225                    mpfr_set_ui (x, 27, MPFR_RNDN);
     226                    mpfr_mul_2si (x, x, 3*i, MPFR_RNDN);
     227                    inex = mpfr_cbrt (x, x, MPFR_RNDN);
     228                    if (mpfr_cmp_si_2exp (x, 3, i) || inex != 0)
     229                      {
     230                        printf ("Error in mpfr_cbrt for "
     231                                "x = 27.0 * 2^(%d), rnd=%s\ngot ",
     232                                3*i, mpfr_print_rnd_mode ((mpfr_rnd_t) r));
     233                        mpfr_dump (x);
     234                        printf (", expected 3 * 2^(%d)\n", i);
     235                        printf ("inex = %d, expected 0\n", inex);
     236                        exit (1);
     237                      }
     238                  }
     239              }
     240          }
     241      }
     242    mpfr_clear (x);
     243  
     244    test_generic (MPFR_PREC_MIN, 200, 10);
     245  
     246    data_check ("data/cbrt", mpfr_cbrt, "mpfr_cbrt");
     247    bad_cases (mpfr_cbrt, cube, "mpfr_cbrt", 256, -256, 255, 4, 128, 200, 50);
     248  
     249    tests_end_mpfr ();
     250    return 0;
     251  }