isnan.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <math.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)
  71. double x;
  72. {
  73. union
  74. {
  75. double d;
  76. short s[4];
  77. int i[2];
  78. } u;
  79. u.d = x;
  80. if( sizeof(int) == 4 )
  81. {
  82. #ifdef IBMPC
  83. return( u.i[1] < 0 );
  84. #endif
  85. #ifdef DEC
  86. return( u.s[3] < 0 );
  87. #endif
  88. #ifdef MIEEE
  89. return( u.i[0] < 0 );
  90. #endif
  91. }
  92. else
  93. {
  94. #ifdef IBMPC
  95. return( u.s[3] < 0 );
  96. #endif
  97. #ifdef DEC
  98. return( u.s[3] < 0 );
  99. #endif
  100. #ifdef MIEEE
  101. return( u.s[0] < 0 );
  102. #endif
  103. }
  104. }
  105. /* Return 1 if x is a number that is Not a Number, else return 0. */
  106. int isnan(x)
  107. double x;
  108. {
  109. #ifdef NANS
  110. union
  111. {
  112. double d;
  113. unsigned short s[4];
  114. unsigned int i[2];
  115. } u;
  116. u.d = x;
  117. if( sizeof(int) == 4 )
  118. {
  119. #ifdef IBMPC
  120. if( ((u.i[1] & 0x7ff00000) == 0x7ff00000)
  121. && (((u.i[1] & 0x000fffff) != 0) || (u.i[0] != 0)))
  122. return 1;
  123. #endif
  124. #ifdef DEC
  125. if( (u.s[1] & 0x7fff) == 0)
  126. {
  127. if( (u.s[2] | u.s[1] | u.s[0]) != 0 )
  128. return(1);
  129. }
  130. #endif
  131. #ifdef MIEEE
  132. if( ((u.i[0] & 0x7ff00000) == 0x7ff00000)
  133. && (((u.i[0] & 0x000fffff) != 0) || (u.i[1] != 0)))
  134. return 1;
  135. #endif
  136. return(0);
  137. }
  138. else
  139. { /* size int not 4 */
  140. #ifdef IBMPC
  141. if( (u.s[3] & 0x7ff0) == 0x7ff0)
  142. {
  143. if( ((u.s[3] & 0x000f) | u.s[2] | u.s[1] | u.s[0]) != 0 )
  144. return(1);
  145. }
  146. #endif
  147. #ifdef DEC
  148. if( (u.s[3] & 0x7fff) == 0)
  149. {
  150. if( (u.s[2] | u.s[1] | u.s[0]) != 0 )
  151. return(1);
  152. }
  153. #endif
  154. #ifdef MIEEE
  155. if( (u.s[0] & 0x7ff0) == 0x7ff0)
  156. {
  157. if( ((u.s[0] & 0x000f) | u.s[1] | u.s[2] | u.s[3]) != 0 )
  158. return(1);
  159. }
  160. #endif
  161. return(0);
  162. } /* size int not 4 */
  163. #else
  164. /* No NANS. */
  165. return(0);
  166. #endif
  167. }
  168. /* Return 1 if x is not infinite and is not a NaN. */
  169. int isfinite(x)
  170. double x;
  171. {
  172. #ifdef INFINITIES
  173. union
  174. {
  175. double d;
  176. unsigned short s[4];
  177. unsigned int i[2];
  178. } u;
  179. u.d = x;
  180. if( sizeof(int) == 4 )
  181. {
  182. #ifdef IBMPC
  183. if( (u.i[1] & 0x7ff00000) != 0x7ff00000)
  184. return 1;
  185. #endif
  186. #ifdef DEC
  187. if( (u.s[3] & 0x7fff) != 0)
  188. return 1;
  189. #endif
  190. #ifdef MIEEE
  191. if( (u.i[0] & 0x7ff00000) != 0x7ff00000)
  192. return 1;
  193. #endif
  194. return(0);
  195. }
  196. else
  197. {
  198. #ifdef IBMPC
  199. if( (u.s[3] & 0x7ff0) != 0x7ff0)
  200. return 1;
  201. #endif
  202. #ifdef DEC
  203. if( (u.s[3] & 0x7fff) != 0)
  204. return 1;
  205. #endif
  206. #ifdef MIEEE
  207. if( (u.s[0] & 0x7ff0) != 0x7ff0)
  208. return 1;
  209. #endif
  210. return(0);
  211. }
  212. #else
  213. /* No INFINITY. */
  214. return(1);
  215. #endif
  216. }