github.com/aergoio/aergo@v1.3.1/libtool/src/gmp-6.1.2/tests/mpf/t-muldiv.c (about)

     1  /* Test mpf_mul, mpf_div, mpf_ui_div, and mpf_div_ui.
     2  
     3  Copyright 1996, 2000, 2001, 2003 Free Software Foundation, Inc.
     4  
     5  This file is part of the GNU MP Library test suite.
     6  
     7  The GNU MP Library test suite is free software; you can redistribute it
     8  and/or modify it under the terms of the GNU General Public License as
     9  published by the Free Software Foundation; either version 3 of the License,
    10  or (at your option) any later version.
    11  
    12  The GNU MP Library test suite is distributed in the hope that it will be
    13  useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    15  Public License for more details.
    16  
    17  You should have received a copy of the GNU General Public License along with
    18  the GNU MP Library test suite.  If not, see https://www.gnu.org/licenses/.  */
    19  
    20  #include <stdio.h>
    21  #include <stdlib.h>
    22  
    23  #include "gmp.h"
    24  #include "gmp-impl.h"
    25  #include "tests.h"
    26  
    27  #ifndef SIZE
    28  #define SIZE 16
    29  #endif
    30  
    31  int
    32  main (int argc, char **argv)
    33  {
    34    mp_size_t size;
    35    mp_exp_t exp;
    36    int reps = 10000;
    37    int i;
    38    mpf_t u, v, w, x;
    39    mp_size_t bprec = SIZE * GMP_LIMB_BITS;
    40    mpf_t rerr, limit_rerr;
    41    unsigned long ulimb, vlimb;
    42    int single_flag;
    43  
    44    tests_start ();
    45  
    46    if (argc > 1)
    47      {
    48        reps = strtol (argv[1], 0, 0);
    49        if (argc > 2)
    50  	bprec = strtol (argv[2], 0, 0);
    51      }
    52  
    53    mpf_set_default_prec (bprec);
    54  
    55    mpf_init (rerr);
    56    mpf_init (limit_rerr);
    57  
    58    mpf_init (u);
    59    mpf_init (v);
    60    mpf_init (w);
    61    mpf_init (x);
    62  
    63    for (i = 0; i < reps; i++)
    64      {
    65        mp_size_t res_prec;
    66  
    67        res_prec = urandom () % bprec + 1;
    68        mpf_set_prec (w, res_prec);
    69        mpf_set_prec (x, res_prec);
    70  
    71        mpf_set_ui (limit_rerr, 1);
    72        mpf_div_2exp (limit_rerr, limit_rerr, res_prec - 1);
    73  
    74        single_flag = 0;
    75  
    76        if ((urandom () & 1) != 0)
    77  	{
    78  	  size = urandom () % (2 * SIZE) - SIZE;
    79  	  exp = urandom () % SIZE;
    80  	  mpf_random2 (u, size, exp);
    81  	}
    82        else
    83  	{
    84  	  ulimb = urandom ();
    85  	  mpf_set_ui (u, ulimb);
    86  	  single_flag = 1;
    87  	}
    88  
    89        if ((urandom () & 1) != 0)
    90  	{
    91  	  size = urandom () % (2 * SIZE) - SIZE;
    92  	  exp = urandom () % SIZE;
    93  	  mpf_random2 (v, size, exp);
    94  	}
    95        else
    96  	{
    97  	  vlimb = urandom ();
    98  	  mpf_set_ui (v, vlimb);
    99  	  single_flag = 2;
   100  	}
   101  
   102        if (mpf_sgn (v) == 0)
   103  	continue;
   104  
   105        mpf_div (w, u, v);
   106        mpf_mul (x, w, v);
   107        mpf_reldiff (rerr, u, x);
   108        if (mpf_cmp (rerr, limit_rerr) > 0)
   109  	{
   110  	  printf ("ERROR in mpf_mul or mpf_div after %d tests\n", i);
   111  	  printf ("   u = "); mpf_dump (u);
   112  	  printf ("   v = "); mpf_dump (v);
   113  	  printf ("   x = "); mpf_dump (x);
   114  	  printf ("   w = "); mpf_dump (w);
   115  	  abort ();
   116  	}
   117  
   118        if (single_flag == 2)
   119  	{
   120  	  mpf_div_ui (x, u, vlimb);
   121  	  mpf_reldiff (rerr, w, x);
   122  	  if (mpf_cmp (rerr, limit_rerr) > 0)
   123  	    {
   124  	      printf ("ERROR in mpf_div or mpf_div_ui after %d tests\n", i);
   125  	      printf ("   u = "); mpf_dump (u);
   126  	      printf ("   v = "); mpf_dump (v);
   127  	      printf ("   x = "); mpf_dump (x);
   128  	      printf ("   w = "); mpf_dump (w);
   129  	      abort ();
   130  	    }
   131  	}
   132  
   133        if (single_flag == 1)
   134  	{
   135  	  mpf_ui_div (x, ulimb, v);
   136  	  mpf_reldiff (rerr, w, x);
   137  	  if (mpf_cmp (rerr, limit_rerr) > 0)
   138  	    {
   139  	      printf ("ERROR in mpf_div or mpf_ui_div after %d tests\n", i);
   140  	      printf ("   u = "); mpf_dump (u);
   141  	      printf ("   v = "); mpf_dump (v);
   142  	      printf ("   x = "); mpf_dump (x);
   143  	      printf ("   w = "); mpf_dump (w);
   144  	      abort ();
   145  	    }
   146  	}
   147      }
   148  
   149    mpf_clear (rerr);
   150    mpf_clear (limit_rerr);
   151  
   152    mpf_clear (u);
   153    mpf_clear (v);
   154    mpf_clear (w);
   155    mpf_clear (x);
   156  
   157    tests_end ();
   158    exit (0);
   159  }