gsl_sys__infnan.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /* sys/infnan.c
  2. *
  3. * Copyright (C) 2001, 2004, 2007 Brian Gough
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. */
  19. #include "gsl__config.h"
  20. #include <math.h>
  21. #if HAVE_IEEEFP_H
  22. #include <ieeefp.h>
  23. #endif
  24. double gsl_nan (void);
  25. double gsl_posinf (void);
  26. double gsl_neginf (void);
  27. double gsl_fdiv (const double x, const double y);
  28. double gsl_nan (void)
  29. {
  30. return gsl_fdiv (0.0, 0.0);
  31. }
  32. double gsl_posinf (void)
  33. {
  34. return gsl_fdiv (+1.0, 0.0);
  35. }
  36. double gsl_neginf (void)
  37. {
  38. return gsl_fdiv (-1.0, 0.0);
  39. }
  40. int gsl_isnan (const double x);
  41. int gsl_isinf (const double x);
  42. int gsl_finite (const double x);
  43. #if defined(_MSC_VER) /* Microsoft Visual C++ */
  44. #include <float.h>
  45. int
  46. gsl_isnan (const double x)
  47. {
  48. return _isnan(x);
  49. }
  50. int
  51. gsl_isinf (const double x)
  52. {
  53. int fpc = _fpclass(x);
  54. if (fpc == _FPCLASS_PINF)
  55. return +1;
  56. else if (fpc == _FPCLASS_NINF)
  57. return -1;
  58. else
  59. return 0;
  60. }
  61. int
  62. gsl_finite (const double x)
  63. {
  64. return _finite(x);
  65. }
  66. #else
  67. # if HAVE_DECL_ISFINITE
  68. int
  69. gsl_finite (const double x)
  70. {
  71. return isfinite(x);
  72. }
  73. # elif HAVE_DECL_FINITE
  74. int
  75. gsl_finite (const double x)
  76. {
  77. return finite(x);
  78. }
  79. # elif HAVE_IEEE_COMPARISONS
  80. int
  81. gsl_finite (const double x)
  82. {
  83. const double y = x - x;
  84. int status = (y == y);
  85. return status;
  86. }
  87. # endif
  88. # if HAVE_DECL_ISNAN
  89. int
  90. gsl_isnan (const double x)
  91. {
  92. return isnan(x);
  93. }
  94. #elif HAVE_IEEE_COMPARISONS
  95. int
  96. gsl_isnan (const double x)
  97. {
  98. int status = (x != x);
  99. return status;
  100. }
  101. # endif
  102. # if HAVE_DECL_ISINF
  103. int
  104. gsl_isinf (const double x)
  105. {
  106. return isinf(x);
  107. }
  108. # else
  109. int
  110. gsl_isinf (const double x)
  111. {
  112. if (! gsl_finite(x) && ! gsl_isnan(x))
  113. {
  114. return (x > 0 ? +1 : -1);
  115. }
  116. else
  117. {
  118. return 0;
  119. }
  120. }
  121. # endif
  122. #endif