test-round.c 3.9 KB

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