1 /* Copyright (C) 2004 Free Software Foundation, Inc.
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.
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.
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
31 #define numberof(x) (sizeof (x) / sizeof ((x)[0]))
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
[] = {
57 for (i
= 0; i
< numberof (modes
); i
++)
59 /* First iteration is the default rounding mode, ie. no call to
60 fesetround. Subsequent iterations are the FE modes from the
65 fesetround (modes
[i
]);
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);
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);
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
);
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
);
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 */
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 */
106 assert (scm_c_round (x
) == x
);
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
);
117 main (int argc
, char *argv
[])