merge from 1.8 branch
[bpt/guile.git] / test-suite / standalone / test-round.c
1 /* Copyright (C) 2004, 2006 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
34 test_scm_c_round ()
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
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);
72
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);
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);
84 assert (scm_c_round (x) == want);
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);
89 assert (scm_c_round (x) == want);
90
91 /* 2^DBL_MANT_DIG-1
92 In the past scm_c_round had incorrectly incremented this value, due
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 */
97 assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
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 */
102 assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
103
104 /* 2^64 */
105 x = ldexp (1.0, 64);
106 assert (scm_c_round (x) == x);
107
108 /* -2^64
109 In the past scm_c_round had incorrectely gone to the next highest
110 representable value in FE_UPWARD, due to x+0.5 rounding. */
111 x = - ldexp (1.0, 64);
112 assert (scm_c_round (x) == x);
113 }
114 }
115
116 static void
117 tests (void *data, int argc, char **argv)
118 {
119 test_scm_c_round ();
120 }
121
122 int
123 main (int argc, char *argv[])
124 {
125 scm_boot_guile (argc, argv, tests, NULL);
126 return 0;
127 }