123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- #if HAVE_CONFIG_H
- # include <config.h>
- #endif
- #undef NDEBUG
- #include <assert.h>
- #include <math.h>
- #include <stdio.h>
- #if HAVE_FENV_H
- # if defined __GNUC__ && defined __GLIBC__
- # pragma GCC diagnostic push
- # pragma GCC diagnostic ignored "-Wmissing-prototypes"
- # endif
- # include <fenv.h>
- # if defined __GNUC__ && defined __GLIBC__
- # pragma GCC diagnostic pop
- # endif
- #elif defined HAVE_MACHINE_FPU_H
- # ifdef HAVE_SYS_TYPES_H
- # include <sys/types.h>
- # endif
- # include <machine/fpu.h>
- #endif
- #include <libguile.h>
- #define numberof(x) (sizeof (x) / sizeof ((x)[0]))
- static void
- test_scm_c_round ()
- {
-
- static const int modes[] = {
- 0,
- #ifdef FE_TONEAREST
- FE_TONEAREST,
- #endif
- #ifdef FE_UPWARD
- FE_UPWARD,
- #endif
- #ifdef FE_DOWNWARD
- FE_DOWNWARD,
- #endif
- #ifdef FE_TOWARDZERO
- FE_TOWARDZERO,
- #endif
- };
- double x, want;
- int i;
- for (i = 0; i < numberof (modes); i++)
- {
-
- if (i != 0)
- {
- #ifdef HAVE_FESETROUND
- fesetround (modes[i]);
- #endif
- }
- assert (scm_c_round (0.0) == 0.0);
- assert (scm_c_round (1.0) == 1.0);
- assert (scm_c_round (-1.0) == -1.0);
- assert (scm_c_round (0.5) == 0.0);
- assert (scm_c_round (1.5) == 2.0);
- assert (scm_c_round (-1.5) == -2.0);
- assert (scm_c_round (2.5) == 2.0);
- assert (scm_c_round (-2.5) == -2.0);
- assert (scm_c_round (3.5) == 4.0);
- assert (scm_c_round (-3.5) == -4.0);
-
- x = ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5;
- want = ldexp (1.0, DBL_MANT_DIG - 1);
- assert (scm_c_round (x) == want);
-
- x = - (ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5);
- want = - ldexp (1.0, DBL_MANT_DIG - 1);
- assert (scm_c_round (x) == want);
-
- x = ldexp (1.0, DBL_MANT_DIG) - 1.0;
- assert (x == floor (x));
- assert (scm_c_round (x) == x);
-
- x = - (ldexp (1.0, DBL_MANT_DIG) - 1.0);
- assert (x == floor (x));
- assert (scm_c_round (x) == x);
-
- x = ldexp (1.0, 64);
- assert (scm_c_round (x) == x);
-
- x = - ldexp (1.0, 64);
- assert (scm_c_round (x) == x);
- }
- }
- static void
- tests (void *data, int argc, char **argv)
- {
- test_scm_c_round ();
- }
- int
- main (int argc, char *argv[])
- {
- scm_boot_guile (argc, argv, tests, NULL);
- return 0;
- }
|