test-round.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Copyright (C) 2004, 2006, 2008, 2009, 2011, 2014 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 License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * 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
  16. * 02110-1301 USA
  17. */
  18. #if HAVE_CONFIG_H
  19. # include <config.h>
  20. #endif
  21. #undef NDEBUG
  22. #include <assert.h>
  23. #include <math.h>
  24. #include <stdio.h>
  25. #if HAVE_FENV_H
  26. # if defined __GNUC__ && defined __GLIBC__
  27. /* In Glibc 2.17, <bits/fenv.h> defines `feraiseexcept' as an inline
  28. without declaring it first, so ignore the warning. */
  29. # pragma GCC diagnostic push
  30. # pragma GCC diagnostic ignored "-Wmissing-prototypes"
  31. # endif
  32. # include <fenv.h>
  33. # if defined __GNUC__ && defined __GLIBC__
  34. # pragma GCC diagnostic pop
  35. # endif
  36. #elif defined HAVE_MACHINE_FPU_H
  37. /* On Tru64 5.1b, the declaration of fesetround(3) is in <machine/fpu.h>.
  38. On NetBSD, this header has to be included along with <sys/types.h>. */
  39. # ifdef HAVE_SYS_TYPES_H
  40. # include <sys/types.h>
  41. # endif
  42. # include <machine/fpu.h>
  43. #endif
  44. #include <libguile.h>
  45. #define numberof(x) (sizeof (x) / sizeof ((x)[0]))
  46. static void
  47. test_scm_c_round ()
  48. {
  49. /* FE constants are defined only where supported, in particular for
  50. instance some ARM systems have been seen with only a couple of modes */
  51. static const int modes[] = {
  52. 0,
  53. #ifdef FE_TONEAREST
  54. FE_TONEAREST,
  55. #endif
  56. #ifdef FE_UPWARD
  57. FE_UPWARD,
  58. #endif
  59. #ifdef FE_DOWNWARD
  60. FE_DOWNWARD,
  61. #endif
  62. #ifdef FE_TOWARDZERO
  63. FE_TOWARDZERO,
  64. #endif
  65. };
  66. double x, want;
  67. int i;
  68. for (i = 0; i < numberof (modes); i++)
  69. {
  70. /* First iteration is the default rounding mode, ie. no call to
  71. fesetround. Subsequent iterations are the FE modes from the
  72. table. */
  73. if (i != 0)
  74. {
  75. #ifdef HAVE_FESETROUND
  76. fesetround (modes[i]);
  77. #endif
  78. }
  79. assert (scm_c_round (0.0) == 0.0);
  80. assert (scm_c_round (1.0) == 1.0);
  81. assert (scm_c_round (-1.0) == -1.0);
  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. /* 2^(DBL_MANT_DIG-1)-1+0.5 */
  90. x = ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5;
  91. want = ldexp (1.0, DBL_MANT_DIG - 1);
  92. assert (scm_c_round (x) == want);
  93. /* -(2^(DBL_MANT_DIG-1)-1+0.5) */
  94. x = - (ldexp (1.0, DBL_MANT_DIG - 1) - 1.0 + 0.5);
  95. want = - ldexp (1.0, DBL_MANT_DIG - 1);
  96. assert (scm_c_round (x) == want);
  97. /* 2^DBL_MANT_DIG-1
  98. In the past scm_c_round had incorrectly incremented this value, due
  99. to the way that x+0.5 would round upwards (in the usual default
  100. nearest-even mode on most systems). */
  101. x = ldexp (1.0, DBL_MANT_DIG) - 1.0;
  102. assert (x == floor (x)); /* should be an integer already */
  103. assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
  104. /* -(2^DBL_MANT_DIG-1) */
  105. x = - (ldexp (1.0, DBL_MANT_DIG) - 1.0);
  106. assert (x == floor (x)); /* should be an integer already */
  107. assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
  108. /* 2^64 */
  109. x = ldexp (1.0, 64);
  110. assert (scm_c_round (x) == x);
  111. /* -2^64
  112. In the past scm_c_round had incorrectely gone to the next highest
  113. representable value in FE_UPWARD, due to x+0.5 rounding. */
  114. x = - ldexp (1.0, 64);
  115. assert (scm_c_round (x) == x);
  116. }
  117. }
  118. static void
  119. tests (void *data, int argc, char **argv)
  120. {
  121. test_scm_c_round ();
  122. }
  123. int
  124. main (int argc, char *argv[])
  125. {
  126. scm_boot_guile (argc, argv, tests, NULL);
  127. return 0;
  128. }