1 /* Test of fmod*() function family.
2 Copyright (C) 2012-2017 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18 my_ldexp (DOUBLE x
, int d
)
32 const DOUBLE TWO_MANT_DIG
=
33 /* Assume MANT_DIG <= 5 * 31.
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));
42 /* Randomized tests. */
43 for (i
= 0; i
< SIZEOF (RANDOM
) / 5; i
++)
44 for (j
= 0; j
< SIZEOF (RANDOM
) / 5; j
++)
46 DOUBLE x
= L_(16.0) * RANDOM
[i
]; /* 0.0 <= x <= 16.0 */
47 DOUBLE y
= RANDOM
[j
]; /* 0.0 <= y < 1.0 */
50 DOUBLE z
= FMOD (x
, y
);
51 ASSERT (z
>= L_(0.0));
53 z
-= x
- (int) (x
/ y
) * y
;
54 ASSERT (/* The common case. */
55 (z
> - L_(16.0) / TWO_MANT_DIG
56 && z
< L_(16.0) / TWO_MANT_DIG
)
57 || /* rounding error: x / y computed too large */
58 (z
> y
- L_(16.0) / TWO_MANT_DIG
59 && z
< y
+ L_(16.0) / TWO_MANT_DIG
)
60 || /* rounding error: x / y computed too small */
61 (z
> - y
- L_(16.0) / TWO_MANT_DIG
62 && z
< - y
+ L_(16.0) / TWO_MANT_DIG
));
66 for (i
= 0; i
< SIZEOF (RANDOM
) / 5; i
++)
67 for (j
= 0; j
< SIZEOF (RANDOM
) / 5; j
++)
69 DOUBLE x
= L_(1.0e9
) * RANDOM
[i
]; /* 0.0 <= x <= 10^9 */
70 DOUBLE y
= RANDOM
[j
]; /* 0.0 <= y < 1.0 */
73 DOUBLE z
= FMOD (x
, y
);
75 ASSERT (z
>= L_(0.0));
78 /* Determine the quotient x / y in two steps, because it
80 int q1
= (int) (x
/ y
/ L_(65536.0));
81 int q2
= (int) ((x
- q1
* L_(65536.0) * y
) / y
);
82 DOUBLE q
= (DOUBLE
) q1
* L_(65536.0) + (DOUBLE
) q2
;
85 /* The absolute error of z can be up to 1e9/2^MANT_DIG.
86 The absolute error of r can also be up to 1e9/2^MANT_DIG.
87 Therefore the error of z - r can be twice as large. */
89 ASSERT (/* The common case. */
90 (z
> - L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
91 && z
< L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
)
92 || /* rounding error: x / y computed too large */
93 (z
> y
- L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
94 && z
< y
+ L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
)
95 || /* rounding error: x / y computed too small */
96 (z
> - y
- L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
97 && z
< - y
+ L_(2.0) * L_(1.0e9
) / TWO_MANT_DIG
));
102 int large_exp
= (MAX_EXP
- 1 < 1000 ? MAX_EXP
- 1 : 1000);
103 DOUBLE large
= my_ldexp (L_(1.0), large_exp
); /* = 2^large_exp */
104 for (i
= 0; i
< SIZEOF (RANDOM
) / 10; i
++)
105 for (j
= 0; j
< SIZEOF (RANDOM
) / 10; j
++)
107 DOUBLE x
= large
* RANDOM
[i
]; /* 0.0 <= x <= 2^large_exp */
108 DOUBLE y
= RANDOM
[j
]; /* 0.0 <= y < 1.0 */
111 DOUBLE z
= FMOD (x
, y
);
112 /* Regardless how large the rounding errors are, the result
113 must be >= 0, < y. */
114 ASSERT (z
>= L_(0.0));