]> git.donarmstrong.com Git - lilypond.git/blob - guile18/test-suite/standalone/test-round.c
New upstream version 2.19.65
[lilypond.git] / guile18 / test-suite / standalone / test-round.c
1 /* Copyright (C) 2004, 2006, 2008, 2009 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 #if HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21
22 #include <assert.h>
23 #include <math.h>
24 #include <stdio.h>
25
26 #if HAVE_FENV_H
27 #include <fenv.h>
28 #elif defined HAVE_MACHINE_FPU_H
29 /* On Tru64 5.1b, the declaration of fesetround(3) is in <machine/fpu.h>.
30    On NetBSD, this header has to be included along with <sys/types.h>.  */
31 # ifdef HAVE_SYS_TYPES_H
32 #  include <sys/types.h>
33 # endif
34 # include <machine/fpu.h>
35 #endif
36
37 #include <libguile.h>
38
39
40 #define numberof(x)  (sizeof (x) / sizeof ((x)[0]))
41
42 static void
43 test_scm_c_round ()
44 {
45   /* FE constants are defined only where supported, in particular for
46      instance some ARM systems have been seen with only a couple of modes */
47   static const int modes[] = {
48     0,
49 #ifdef FE_TONEAREST
50     FE_TONEAREST,
51 #endif
52 #ifdef FE_UPWARD
53     FE_UPWARD,
54 #endif
55 #ifdef FE_DOWNWARD
56     FE_DOWNWARD,
57 #endif
58 #ifdef FE_TOWARDZERO
59     FE_TOWARDZERO,
60 #endif
61   };
62
63   double  x, want;
64   int  i;
65
66   for (i = 0; i < numberof (modes); i++)
67     {
68       /* First iteration is the default rounding mode, ie. no call to
69          fesetround.  Subsequent iterations are the FE modes from the
70          table.  */
71       if (i != 0)
72         {
73 #if HAVE_FESETROUND
74           fesetround (modes[i]);
75 #endif
76         }
77
78       assert (scm_c_round (0.0) == 0.0);
79       assert (scm_c_round (1.0) == 1.0);
80       assert (scm_c_round (-1.0) == -1.0);
81
82       assert (scm_c_round (0.5) == 0.0);
83       assert (scm_c_round (1.5) == 2.0);
84       assert (scm_c_round (-1.5) == -2.0);
85       assert (scm_c_round (2.5) == 2.0);
86       assert (scm_c_round (-2.5) == -2.0);
87       assert (scm_c_round (3.5) == 4.0);
88       assert (scm_c_round (-3.5) == -4.0);
89
90       /* 2^(DBL_MANT_DIG-1)-1+0.5 */
91       x = ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5;
92       want = ldexp (1.0, DBL_MANT_DIG - 1);
93       assert (scm_c_round (x) == want);
94
95       /* -(2^(DBL_MANT_DIG-1)-1+0.5) */
96       x = - (ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5);
97       want = - ldexp (1.0, DBL_MANT_DIG - 1);
98       assert (scm_c_round (x) == want);
99
100       /* 2^DBL_MANT_DIG-1
101          In the past scm_c_round had incorrectly incremented this value, due
102          to the way that x+0.5 would round upwards (in the usual default
103          nearest-even mode on most systems).  */
104       x = ldexp (1.0, DBL_MANT_DIG) - 1.0;
105       assert (x == floor (x));      /* should be an integer already */
106       assert (scm_c_round (x) == x);  /* scm_c_round should return it unchanged */
107
108       /* -(2^DBL_MANT_DIG-1) */
109       x = - (ldexp (1.0, DBL_MANT_DIG) - 1.0);
110       assert (x == floor (x));      /* should be an integer already */
111       assert (scm_c_round (x) == x);  /* scm_c_round should return it unchanged */
112
113       /* 2^64 */
114       x = ldexp (1.0, 64);
115       assert (scm_c_round (x) == x);
116
117       /* -2^64
118          In the past scm_c_round had incorrectely gone to the next highest
119          representable value in FE_UPWARD, due to x+0.5 rounding.  */
120       x = - ldexp (1.0, 64);
121       assert (scm_c_round (x) == x);
122     }
123 }
124
125 static void
126 tests (void *data, int argc, char **argv)
127 {
128   test_scm_c_round ();
129 }
130
131 int
132 main (int argc, char *argv[])
133 {
134   scm_boot_guile (argc, argv, tests, NULL);
135   return 0;
136 }