root/maint/gnulib/tests/test-remainder.h

/* [previous][next][first][last][top][bottom][index][help] */

INCLUDED FROM


DEFINITIONS

This source file includes following definitions.
  1. my_ldexp
  2. test_function

   1 /* Test of remainder*() function family.
   2    Copyright (C) 2012-2021 Free Software Foundation, Inc.
   3 
   4    This program is free software: you can redistribute it and/or modify
   5    it under the terms of the GNU General Public License as published by
   6    the Free Software Foundation; either version 3 of the License, or
   7    (at your option) any later version.
   8 
   9    This program is distributed in the hope that it will be useful,
  10    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12    GNU General Public License for more details.
  13 
  14    You should have received a copy of the GNU General Public License
  15    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
  16 
  17 static DOUBLE
  18 my_ldexp (DOUBLE x, int d)
     /* [previous][next][first][last][top][bottom][index][help] */
  19 {
  20   for (; d > 0; d--)
  21     x *= L_(2.0);
  22   for (; d < 0; d++)
  23     x *= L_(0.5);
  24   return x;
  25 }
  26 
  27 static void
  28 test_function (void)
     /* [previous][next][first][last][top][bottom][index][help] */
  29 {
  30   int i;
  31   int j;
  32   const DOUBLE TWO_MANT_DIG =
  33     /* Assume MANT_DIG <= 5 * 31.
  34        Use the identity
  35          n = floor(n/5) + floor((n+1)/5) + ... + floor((n+4)/5).  */
  36     (DOUBLE) (1U << ((MANT_DIG - 1) / 5))
  37     * (DOUBLE) (1U << ((MANT_DIG - 1 + 1) / 5))
  38     * (DOUBLE) (1U << ((MANT_DIG - 1 + 2) / 5))
  39     * (DOUBLE) (1U << ((MANT_DIG - 1 + 3) / 5))
  40     * (DOUBLE) (1U << ((MANT_DIG - 1 + 4) / 5));
  41 
  42   /* Randomized tests.  */
  43   for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
  44     for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
  45       {
  46         DOUBLE x = L_(16.0) * RANDOM[i]; /* 0.0 <= x <= 16.0 */
  47         DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
  48         if (y > L_(0.0))
  49           {
  50             DOUBLE z = REMAINDER (x, y);
  51             ASSERT (z >= - L_(0.5) * y);
  52             ASSERT (z <= L_(0.5) * y);
  53             z -= x - (int) ((L_(2.0) * x + y) / (L_(2.0) * y)) * y;
  54             ASSERT (/* The common case.  */
  55                     (z > - L_(2.0) * L_(16.0) / TWO_MANT_DIG
  56                      && z < L_(2.0) * L_(16.0) / TWO_MANT_DIG)
  57                     || /* rounding error: 2x+y / 2y computed too large */
  58                        (z > y - L_(2.0) * L_(16.0) / TWO_MANT_DIG
  59                         && z < y + L_(2.0) * L_(16.0) / TWO_MANT_DIG)
  60                     || /* rounding error: 2x+y / 2y computed too small */
  61                        (z > - y - L_(2.0) * L_(16.0) / TWO_MANT_DIG
  62                         && z < - y + L_(2.0) * L_(16.0) / TWO_MANT_DIG));
  63           }
  64       }
  65 
  66   for (i = 0; i < SIZEOF (RANDOM) / 5; i++)
  67     for (j = 0; j < SIZEOF (RANDOM) / 5; j++)
  68       {
  69         DOUBLE x = L_(1.0e9) * RANDOM[i]; /* 0.0 <= x <= 10^9 */
  70         DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
  71         if (y > L_(0.0))
  72           {
  73             DOUBLE z = REMAINDER (x, y);
  74             DOUBLE r;
  75             ASSERT (z >= - L_(0.5) * y);
  76             ASSERT (z <= L_(0.5) * y);
  77             {
  78               /* Determine the quotient 2x+y / 2y in two steps, because it
  79                  may be > 2^31.  */
  80               int q1 = (int) (x / y / L_(65536.0));
  81               int q2 = (int) ((L_(2.0) * (x - q1 * L_(65536.0) * y) + y)
  82                               / (L_(2.0) * y));
  83               DOUBLE q = (DOUBLE) q1 * L_(65536.0) + (DOUBLE) q2;
  84               r = x - q * y;
  85             }
  86             /* The absolute error of z can be up to 1e9/2^MANT_DIG.
  87                The absolute error of r can also be up to 1e9/2^MANT_DIG.
  88                Therefore the error of z - r can be twice as large.  */
  89             z -= r;
  90             ASSERT (/* The common case.  */
  91                     (z > - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
  92                      && z < L_(2.0) * L_(1.0e9) / TWO_MANT_DIG)
  93                     || /* rounding error: 2x+y / 2y computed too large */
  94                        (z > y - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
  95                         && z < y + L_(2.0) * L_(1.0e9) / TWO_MANT_DIG)
  96                     || /* rounding error: 2x+y / 2y computed too small */
  97                        (z > - y - L_(2.0) * L_(1.0e9) / TWO_MANT_DIG
  98                         && z < - y + L_(2.0) * L_(1.0e9) / TWO_MANT_DIG));
  99           }
 100       }
 101 
 102   {
 103     int large_exp = (MAX_EXP - 1 < 1000 ? MAX_EXP - 1 : 1000);
 104     DOUBLE large = my_ldexp (L_(1.0), large_exp); /* = 2^large_exp */
 105     for (i = 0; i < SIZEOF (RANDOM) / 10; i++)
 106       for (j = 0; j < SIZEOF (RANDOM) / 10; j++)
 107         {
 108           DOUBLE x = large * RANDOM[i]; /* 0.0 <= x <= 2^large_exp */
 109           DOUBLE y = RANDOM[j]; /* 0.0 <= y < 1.0 */
 110           if (y > L_(0.0))
 111             {
 112               DOUBLE z = REMAINDER (x, y);
 113               /* Regardless how large the rounding errors are, the result
 114                  must be >= -y/2, <= y/2.  */
 115               ASSERT (z >= - L_(0.5) * y);
 116               ASSERT (z <= L_(0.5) * y);
 117             }
 118         }
 119   }
 120 }
 121 
 122 volatile DOUBLE x;
 123 volatile DOUBLE y;
 124 DOUBLE z;

/* [previous][next][first][last][top][bottom][index][help] */