(root)/
mpfr-4.2.1/
tests/
ttrunc.c
       1  /* Test file for mpfr_trunc, mpfr_ceil, mpfr_floor.
       2  
       3  Copyright 1999-2004, 2006-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 SIZEX 100
      26  
      27  int
      28  main (void)
      29  {
      30    int j, k;
      31    mpfr_t x, y, z, t, y2, z2, t2;
      32  
      33    tests_start_mpfr ();
      34  
      35    mpfr_inits2 (SIZEX, x, y, z, t, y2, z2, t2, (mpfr_ptr) 0);
      36  
      37    mpfr_set_str1 (x, "0.5");
      38    mpfr_ceil(y, x);
      39    if (mpfr_cmp_ui (y, 1))
      40      {
      41        printf ("Error in mpfr_ceil for x=0.5: expected 1.0, got ");
      42        mpfr_dump (y);
      43        exit (1);
      44      }
      45  
      46    mpfr_set_ui (x, 0, MPFR_RNDN);
      47    mpfr_ceil(y, x);
      48    if (mpfr_cmp_ui(y,0))
      49      {
      50        printf ("Error in mpfr_ceil for x=0.0: expected 0.0, got ");
      51        mpfr_dump (y);
      52        exit (1);
      53      }
      54  
      55    mpfr_set_ui (x, 1, MPFR_RNDN);
      56    mpfr_ceil(y, x);
      57    if (mpfr_cmp_ui(y,1))
      58      {
      59        printf ("Error in mpfr_ceil for x=1.0: expected 1.0, got ");
      60        mpfr_dump (y);
      61        exit (1);
      62      }
      63  
      64    for (j=0;j<1000;j++)
      65      {
      66        mpfr_urandomb (x, RANDS);
      67        MPFR_EXP (x) = 2;
      68  
      69        for (k = 2; k <= SIZEX; k++)
      70          {
      71            mpfr_set_prec(y, k);
      72            mpfr_set_prec(y2, k);
      73            mpfr_set_prec(z, k);
      74            mpfr_set_prec(z2, k);
      75            mpfr_set_prec(t, k);
      76            mpfr_set_prec(t2, k);
      77  
      78            mpfr_floor(y, x);
      79            mpfr_set(y2, x, MPFR_RNDD);
      80  
      81            mpfr_trunc(z, x);
      82            mpfr_set(z2, x, MPFR_RNDZ);
      83  
      84            mpfr_ceil(t, x);
      85            mpfr_set(t2, x, MPFR_RNDU);
      86  
      87            if (!mpfr_eq(y, y2, k))
      88              {
      89                printf ("Error in floor, x = "); mpfr_dump (x);
      90                printf ("floor(x) = "); mpfr_dump (y);
      91                printf ("round(x, RNDD) = "); mpfr_dump (y2);
      92                exit(1);
      93              }
      94  
      95            if (!mpfr_eq(z, z2, k))
      96              {
      97                printf ("Error in trunc, x = "); mpfr_dump (x);
      98                printf ("trunc(x) = "); mpfr_dump (z);
      99                printf ("round(x, RNDZ) = "); mpfr_dump (z2);
     100                exit(1);
     101              }
     102  
     103            if (!mpfr_eq(y, y2, k))
     104              {
     105                printf ("Error in ceil, x = "); mpfr_dump (x);
     106                printf ("ceil(x) = "); mpfr_dump (t);
     107                printf ("round(x, RNDU) = "); mpfr_dump (t2);
     108                exit(1);
     109              }
     110            MPFR_EXP(x)++;
     111          }
     112      }
     113  
     114    mpfr_clears (x, y, z, t, y2, z2, t2, (mpfr_ptr) 0);
     115  
     116    tests_end_mpfr ();
     117    return 0;
     118  }