test-round.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright 2004,2006,2008-2009,2011,2014,2018
  2. Free Software Foundation, Inc.
  3. This file is part of Guile.
  4. Guile is free software: you can redistribute it and/or modify it
  5. under the terms of the GNU Lesser General Public License as published
  6. by the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. Guile is distributed in the hope that it will be useful, but WITHOUT
  9. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with Guile. If not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #if HAVE_CONFIG_H
  16. # include <config.h>
  17. #endif
  18. #undef NDEBUG
  19. #include <assert.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #if HAVE_FENV_H
  23. # if defined __GNUC__ && defined __GLIBC__
  24. /* In Glibc 2.17, <bits/fenv.h> defines `feraiseexcept' as an inline
  25. without declaring it first, so ignore the warning. */
  26. # pragma GCC diagnostic push
  27. # pragma GCC diagnostic ignored "-Wmissing-prototypes"
  28. # endif
  29. # include <fenv.h>
  30. # if defined __GNUC__ && defined __GLIBC__
  31. # pragma GCC diagnostic pop
  32. # endif
  33. #elif defined HAVE_MACHINE_FPU_H
  34. /* On Tru64 5.1b, the declaration of fesetround(3) is in <machine/fpu.h>.
  35. On NetBSD, this header has to be included along with <sys/types.h>. */
  36. # ifdef HAVE_SYS_TYPES_H
  37. # include <sys/types.h>
  38. # endif
  39. # include <machine/fpu.h>
  40. #endif
  41. #include <libguile.h>
  42. #define numberof(x) (sizeof (x) / sizeof ((x)[0]))
  43. static void
  44. test_scm_c_round ()
  45. {
  46. /* FE constants are defined only where supported, in particular for
  47. instance some ARM systems have been seen with only a couple of modes */
  48. static const int modes[] = {
  49. 0,
  50. #ifdef FE_TONEAREST
  51. FE_TONEAREST,
  52. #endif
  53. #ifdef FE_UPWARD
  54. FE_UPWARD,
  55. #endif
  56. #ifdef FE_DOWNWARD
  57. FE_DOWNWARD,
  58. #endif
  59. #ifdef FE_TOWARDZERO
  60. FE_TOWARDZERO,
  61. #endif
  62. };
  63. double x, want;
  64. int i;
  65. for (i = 0; i < numberof (modes); i++)
  66. {
  67. /* First iteration is the default rounding mode, ie. no call to
  68. fesetround. Subsequent iterations are the FE modes from the
  69. table. */
  70. if (i != 0)
  71. {
  72. #ifdef HAVE_FESETROUND
  73. fesetround (modes[i]);
  74. #endif
  75. }
  76. assert (scm_c_round (0.0) == 0.0);
  77. assert (scm_c_round (1.0) == 1.0);
  78. assert (scm_c_round (-1.0) == -1.0);
  79. assert (scm_c_round (0.5) == 0.0);
  80. assert (scm_c_round (1.5) == 2.0);
  81. assert (scm_c_round (-1.5) == -2.0);
  82. assert (scm_c_round (2.5) == 2.0);
  83. assert (scm_c_round (-2.5) == -2.0);
  84. assert (scm_c_round (3.5) == 4.0);
  85. assert (scm_c_round (-3.5) == -4.0);
  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. /* -(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. /* 2^DBL_MANT_DIG-1
  95. In the past scm_c_round had incorrectly incremented this value, due
  96. to the way that x+0.5 would round upwards (in the usual default
  97. nearest-even mode on most systems). */
  98. x = ldexp (1.0, DBL_MANT_DIG) - 1.0;
  99. assert (x == floor (x)); /* should be an integer already */
  100. assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
  101. /* -(2^DBL_MANT_DIG-1) */
  102. x = - (ldexp (1.0, DBL_MANT_DIG) - 1.0);
  103. assert (x == floor (x)); /* should be an integer already */
  104. assert (scm_c_round (x) == x); /* scm_c_round should return it unchanged */
  105. /* 2^64 */
  106. x = ldexp (1.0, 64);
  107. assert (scm_c_round (x) == x);
  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. static void
  116. tests (void *data, int argc, char **argv)
  117. {
  118. test_scm_c_round ();
  119. }
  120. int
  121. main (int argc, char *argv[])
  122. {
  123. scm_boot_guile (argc, argv, tests, NULL);
  124. return 0;
  125. }