isnan.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* isnan()
  2. * signbit()
  3. * isfinite()
  4. *
  5. * Floating point numeric utilities
  6. *
  7. *
  8. *
  9. * SYNOPSIS:
  10. *
  11. * double ceil(), floor(), frexp(), ldexp();
  12. * int signbit(), isnan(), isfinite();
  13. * double x, y;
  14. * int expnt, n;
  15. *
  16. * y = floor(x);
  17. * y = ceil(x);
  18. * y = frexp( x, &expnt );
  19. * y = ldexp( x, n );
  20. * n = signbit(x);
  21. * n = isnan(x);
  22. * n = isfinite(x);
  23. *
  24. *
  25. *
  26. * DESCRIPTION:
  27. *
  28. * All four routines return a double precision floating point
  29. * result.
  30. *
  31. * floor() returns the largest integer less than or equal to x.
  32. * It truncates toward minus infinity.
  33. *
  34. * ceil() returns the smallest integer greater than or equal
  35. * to x. It truncates toward plus infinity.
  36. *
  37. * frexp() extracts the exponent from x. It returns an integer
  38. * power of two to expnt and the significand between 0.5 and 1
  39. * to y. Thus x = y * 2**expn.
  40. *
  41. * ldexp() multiplies x by 2**n.
  42. *
  43. * signbit(x) returns 1 if the sign bit of x is 1, else 0.
  44. *
  45. * These functions are part of the standard C run time library
  46. * for many but not all C compilers. The ones supplied are
  47. * written in C for either DEC or IEEE arithmetic. They should
  48. * be used only if your compiler library does not already have
  49. * them.
  50. *
  51. * The IEEE versions assume that denormal numbers are implemented
  52. * in the arithmetic. Some modifications will be required if
  53. * the arithmetic has abrupt rather than gradual underflow.
  54. */
  55. /*
  56. Cephes Math Library Release 2.3: March, 1995
  57. Copyright 1984, 1995 by Stephen L. Moshier
  58. */
  59. #include "mconf.h"
  60. #ifdef UNK
  61. /* ceil(), floor(), frexp(), ldexp() may need to be rewritten. */
  62. #undef UNK
  63. #if BIGENDIAN
  64. #define MIEEE 1
  65. #else
  66. #define IBMPC 1
  67. #endif
  68. #endif
  69. /* Return 1 if the sign bit of x is 1, else 0. */
  70. int signbit(x) double x;
  71. {
  72. union {
  73. double d;
  74. short s[4];
  75. int i[2];
  76. } u;
  77. u.d = x;
  78. if (sizeof(int) == 4) {
  79. #ifdef IBMPC
  80. return (u.i[1] < 0);
  81. #endif
  82. #ifdef DEC
  83. return (u.s[3] < 0);
  84. #endif
  85. #ifdef MIEEE
  86. return (u.i[0] < 0);
  87. #endif
  88. } else {
  89. #ifdef IBMPC
  90. return (u.s[3] < 0);
  91. #endif
  92. #ifdef DEC
  93. return (u.s[3] < 0);
  94. #endif
  95. #ifdef MIEEE
  96. return (u.s[0] < 0);
  97. #endif
  98. }
  99. }
  100. /* Return 1 if x is a number that is Not a Number, else return 0. */
  101. int isnan(x) double x;
  102. {
  103. #ifdef NANS
  104. union {
  105. double d;
  106. unsigned short s[4];
  107. unsigned int i[2];
  108. } u;
  109. u.d = x;
  110. if (sizeof(int) == 4) {
  111. #ifdef IBMPC
  112. if (((u.i[1] & 0x7ff00000) == 0x7ff00000) &&
  113. (((u.i[1] & 0x000fffff) != 0) || (u.i[0] != 0)))
  114. return 1;
  115. #endif
  116. #ifdef DEC
  117. if ((u.s[1] & 0x7fff) == 0) {
  118. if ((u.s[2] | u.s[1] | u.s[0]) != 0)
  119. return (1);
  120. }
  121. #endif
  122. #ifdef MIEEE
  123. if (((u.i[0] & 0x7ff00000) == 0x7ff00000) &&
  124. (((u.i[0] & 0x000fffff) != 0) || (u.i[1] != 0)))
  125. return 1;
  126. #endif
  127. return (0);
  128. } else { /* size int not 4 */
  129. #ifdef IBMPC
  130. if ((u.s[3] & 0x7ff0) == 0x7ff0) {
  131. if (((u.s[3] & 0x000f) | u.s[2] | u.s[1] | u.s[0]) != 0)
  132. return (1);
  133. }
  134. #endif
  135. #ifdef DEC
  136. if ((u.s[3] & 0x7fff) == 0) {
  137. if ((u.s[2] | u.s[1] | u.s[0]) != 0)
  138. return (1);
  139. }
  140. #endif
  141. #ifdef MIEEE
  142. if ((u.s[0] & 0x7ff0) == 0x7ff0) {
  143. if (((u.s[0] & 0x000f) | u.s[1] | u.s[2] | u.s[3]) != 0)
  144. return (1);
  145. }
  146. #endif
  147. return (0);
  148. } /* size int not 4 */
  149. #else
  150. /* No NANS. */
  151. return (0);
  152. #endif
  153. }
  154. /* Return 1 if x is not infinite and is not a NaN. */
  155. int isfinite(x) double x;
  156. {
  157. #ifdef INFINITIES
  158. union {
  159. double d;
  160. unsigned short s[4];
  161. unsigned int i[2];
  162. } u;
  163. u.d = x;
  164. if (sizeof(int) == 4) {
  165. #ifdef IBMPC
  166. if ((u.i[1] & 0x7ff00000) != 0x7ff00000)
  167. return 1;
  168. #endif
  169. #ifdef DEC
  170. if ((u.s[3] & 0x7fff) != 0)
  171. return 1;
  172. #endif
  173. #ifdef MIEEE
  174. if ((u.i[0] & 0x7ff00000) != 0x7ff00000)
  175. return 1;
  176. #endif
  177. return (0);
  178. } else {
  179. #ifdef IBMPC
  180. if ((u.s[3] & 0x7ff0) != 0x7ff0)
  181. return 1;
  182. #endif
  183. #ifdef DEC
  184. if ((u.s[3] & 0x7fff) != 0)
  185. return 1;
  186. #endif
  187. #ifdef MIEEE
  188. if ((u.s[0] & 0x7ff0) != 0x7ff0)
  189. return 1;
  190. #endif
  191. return (0);
  192. }
  193. #else
  194. /* No INFINITY. */
  195. return (1);
  196. #endif
  197. }