Commit | Line | Data |
---|---|---|
370b8399 KR |
1 | /* Copyright (C) 2004 Free Software Foundation, Inc. |
2 | * | |
3 | * This library is free software; you can redistribute it and/or | |
4 | * modify it under the terms of the GNU Lesser General Public | |
5 | * License as published by the Free Software Foundation; either | |
6 | * version 2.1 of the License, or (at your option) any later version. | |
7 | * | |
8 | * This library is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
11 | * Lesser General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU Lesser General Public | |
14 | * License along with this library; if not, write to the Free Software | |
15 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
16 | */ | |
17 | ||
18 | #include "config.h" | |
19 | ||
20 | #include <assert.h> | |
21 | #include <math.h> | |
22 | #include <stdio.h> | |
23 | ||
24 | #if HAVE_FENV_H | |
25 | #include <fenv.h> | |
26 | #endif | |
27 | ||
28 | #include "libguile.h" | |
29 | ||
30 | ||
31 | #define numberof(x) (sizeof (x) / sizeof ((x)[0])) | |
32 | ||
33 | static void | |
ee3e40b7 | 34 | test_scm_c_round () |
370b8399 KR |
35 | { |
36 | /* FE constants are defined only where supported, in particular for | |
37 | instance some ARM systems have been seen with only a couple of modes */ | |
38 | static const int modes[] = { | |
39 | 0, | |
40 | #ifdef FE_TONEAREST | |
41 | FE_TONEAREST, | |
42 | #endif | |
43 | #ifdef FE_UPWARD | |
44 | FE_UPWARD, | |
45 | #endif | |
46 | #ifdef FE_DOWNWARD | |
47 | FE_DOWNWARD, | |
48 | #endif | |
49 | #ifdef FE_TOWARDZERO | |
50 | FE_TOWARDZERO, | |
51 | #endif | |
52 | }; | |
53 | ||
54 | double x, want; | |
55 | int i; | |
56 | ||
57 | for (i = 0; i < numberof (modes); i++) | |
58 | { | |
59 | /* First iteration is the default rounding mode, ie. no call to | |
60 | fesetround. Subsequent iterations are the FE modes from the | |
61 | table. */ | |
62 | if (i != 0) | |
63 | { | |
64 | #if HAVE_FESETROUND | |
65 | fesetround (modes[i]); | |
66 | #endif | |
67 | } | |
68 | ||
ee3e40b7 MV |
69 | assert (scm_c_round (0.0) == 0.0); |
70 | assert (scm_c_round (1.0) == 1.0); | |
71 | assert (scm_c_round (-1.0) == -1.0); | |
370b8399 | 72 | |
ee3e40b7 MV |
73 | assert (scm_c_round (0.5) == 0.0); |
74 | assert (scm_c_round (1.5) == 2.0); | |
75 | assert (scm_c_round (-1.5) == -2.0); | |
76 | assert (scm_c_round (2.5) == 2.0); | |
77 | assert (scm_c_round (-2.5) == -2.0); | |
78 | assert (scm_c_round (3.5) == 4.0); | |
79 | assert (scm_c_round (-3.5) == -4.0); | |
370b8399 KR |
80 | |
81 | /* 2^(DBL_MANT_DIG-1)-1+0.5 */ | |
82 | x = ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5; | |
83 | want = ldexp (1.0, DBL_MANT_DIG - 1); | |
ee3e40b7 | 84 | assert (scm_c_round (x) == want); |
370b8399 KR |
85 | |
86 | /* -(2^(DBL_MANT_DIG-1)-1+0.5) */ | |
87 | x = - (ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5); | |
88 | want = - ldexp (1.0, DBL_MANT_DIG - 1); | |
ee3e40b7 | 89 | assert (scm_c_round (x) == want); |
370b8399 KR |
90 | |
91 | /* 2^DBL_MANT_DIG-1 | |
ee3e40b7 | 92 | In the past scm_c_round had incorrectly incremented this value, due |
370b8399 KR |
93 | to the way that x+0.5 would round upwards (in the usual default |
94 | nearest-even mode on most systems). */ | |
95 | x = ldexp (1.0, DBL_MANT_DIG) - 1.0; | |
96 | assert (x == floor (x)); /* should be an integer already */ | |
ee3e40b7 | 97 | assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */ |
370b8399 KR |
98 | |
99 | /* -(2^DBL_MANT_DIG-1) */ | |
100 | x = - (ldexp (1.0, DBL_MANT_DIG) - 1.0); | |
101 | assert (x == floor (x)); /* should be an integer already */ | |
ee3e40b7 | 102 | assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */ |
370b8399 KR |
103 | |
104 | /* 2^64 */ | |
105 | x = ldexp (1.0, 64); | |
ee3e40b7 | 106 | assert (scm_c_round (x) == x); |
370b8399 KR |
107 | |
108 | /* -2^64 | |
ee3e40b7 | 109 | In the past scm_c_round had incorrectely gone to the next highest |
370b8399 KR |
110 | representable value in FE_UPWARD, due to x+0.5 rounding. */ |
111 | x = - ldexp (1.0, 64); | |
ee3e40b7 | 112 | assert (scm_c_round (x) == x); |
370b8399 KR |
113 | } |
114 | } | |
115 | ||
116 | int | |
117 | main (int argc, char *argv[]) | |
118 | { | |
119 | scm_init_guile(); | |
ee3e40b7 | 120 | test_scm_c_round (); |
370b8399 KR |
121 | return 0; |
122 | } |