fltest3.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* fltest.c
  2. * Test program for floor(), frexp(), ldexp()
  3. */
  4. /*
  5. Cephes Math Library Release 2.1: December, 1988
  6. Copyright 1984, 1987, 1988 by Stephen L. Moshier
  7. Direct inquiries to 30 Frost Street, Cambridge, MA 02140
  8. */
  9. #include <math.h>
  10. /*extern double MACHEP;*/
  11. #define MACHEP 2.3e-16
  12. #define UTH -1023
  13. main()
  14. {
  15. double x, y, y0, z, f, x00, y00;
  16. int i, j, k, e, e0;
  17. int errfr, errld, errfl, underexp, err, errth, e00;
  18. double frexp(), ldexp(), floor();
  19. /*
  20. if( 1 )
  21. goto flrtst;
  22. */
  23. printf( "Testing frexp() and ldexp().\n" );
  24. errfr = 0;
  25. errld = 0;
  26. underexp = 0;
  27. f = 1.0;
  28. x00 = 2.0;
  29. y00 = 0.5;
  30. e00 = 2;
  31. for( j=0; j<20; j++ )
  32. {
  33. if( j == 10 )
  34. {
  35. f = 1.0;
  36. x00 = 2.0;
  37. e00 = 1;
  38. /* Find 2**(2**10) / 2 */
  39. #ifdef DEC
  40. for( i=0; i<5; i++ )
  41. #else
  42. for( i=0; i<9; i++ )
  43. #endif
  44. {
  45. x00 *= x00;
  46. e00 += e00;
  47. }
  48. y00 = x00/2.0;
  49. x00 = x00 * y00;
  50. e00 += e00;
  51. y00 = 0.5;
  52. }
  53. x = x00 * f;
  54. y0 = y00 * f;
  55. e0 = e00;
  56. for( i=0; i<2200; i++ )
  57. {
  58. x /= 2.0;
  59. e0 -= 1;
  60. if( x == 0.0 )
  61. {
  62. if( f == 1.0 )
  63. underexp = e0;
  64. y0 = 0.0;
  65. e0 = 0;
  66. }
  67. y = frexp( x, &e );
  68. if( (e0 < -1023) && (e != e0) )
  69. {
  70. if( e == (e0 - 1) )
  71. {
  72. e += 1;
  73. y /= 2.0;
  74. }
  75. if( e == (e0 + 1) )
  76. {
  77. e -= 1;
  78. y *= 2.0;
  79. }
  80. }
  81. err = y - y0;
  82. if( y0 != 0.0 )
  83. err /= y0;
  84. if( err < 0.0 )
  85. err = -err;
  86. if( e0 > -1023 )
  87. errth = 0.0;
  88. else
  89. {/* Denormal numbers may have rounding errors */
  90. if( e0 == -1023 )
  91. {
  92. errth = 2.0 * MACHEP;
  93. }
  94. else
  95. {
  96. errth *= 2.0;
  97. }
  98. }
  99. if( (x != 0.0) && ((err > errth) || (e != e0)) )
  100. {
  101. printf( "Test %d: ", j+1 );
  102. printf( " frexp( %.15e) =?= %.15e * 2**%d;", x, y, e );
  103. printf( " should be %.15e * 2**%d\n", y0, e0 );
  104. errfr += 1;
  105. }
  106. y = ldexp( x, 1-e0 );
  107. err = y - 1.0;
  108. if( err < 0.0 )
  109. err = -err;
  110. if( (err > errth) && ((x == 0.0) && (y != 0.0)) )
  111. {
  112. printf( "Test %d: ", j+1 );
  113. printf( "ldexp( %.15e, %d ) =?= %.15e;", x, 1-e0, y );
  114. if( x != 0.0 )
  115. printf( " should be %.15e\n", f );
  116. else
  117. printf( " should be %.15e\n", 0.0 );
  118. errld += 1;
  119. }
  120. if( x == 0.0 )
  121. {
  122. break;
  123. }
  124. }
  125. f = f * 1.08005973889;
  126. }
  127. if( (errld == 0) && (errfr == 0) )
  128. {
  129. printf( "No errors found.\n" );
  130. }
  131. flrtst:
  132. printf( "Testing floor().\n" );
  133. errfl = 0;
  134. f = 1.0/MACHEP;
  135. x00 = 1.0;
  136. for( j=0; j<57; j++ )
  137. {
  138. x = x00 - 1.0;
  139. for( i=0; i<128; i++ )
  140. {
  141. y = floor(x);
  142. if( y != x )
  143. {
  144. flierr( x, y, j );
  145. errfl += 1;
  146. }
  147. /* Warning! the if() statement is compiler dependent,
  148. * since x-0.49 may be held in extra precision accumulator
  149. * so would never compare equal to x! The subroutine call
  150. * y = floor() forces z to be stored as a double and reloaded
  151. * for the if() statement.
  152. */
  153. z = x - 0.49;
  154. y = floor(z);
  155. if( z == x )
  156. break;
  157. if( y != (x - 1.0) )
  158. {
  159. flierr( z, y, j );
  160. errfl += 1;
  161. }
  162. z = x + 0.49;
  163. y = floor(z);
  164. if( z != x )
  165. {
  166. if( y != x )
  167. {
  168. flierr( z, y, j );
  169. errfl += 1;
  170. }
  171. }
  172. x = -x;
  173. y = floor(x);
  174. if( z != x )
  175. {
  176. if( y != x )
  177. {
  178. flierr( x, y, j );
  179. errfl += 1;
  180. }
  181. }
  182. z = x + 0.49;
  183. y = floor(z);
  184. if( z != x )
  185. {
  186. if( y != x )
  187. {
  188. flierr( z, y, j );
  189. errfl += 1;
  190. }
  191. }
  192. z = x - 0.49;
  193. y = floor(z);
  194. if( z != x )
  195. {
  196. if( y != (x - 1.0) )
  197. {
  198. flierr( z, y, j );
  199. errfl += 1;
  200. }
  201. }
  202. x = -x;
  203. x += 1.0;
  204. }
  205. x00 = x00 + x00;
  206. }
  207. y = floor(0.0);
  208. if( y != 0.0 )
  209. {
  210. flierr( 0.0, y, 57 );
  211. errfl += 1;
  212. }
  213. y = floor(-0.0);
  214. if( y != 0.0 )
  215. {
  216. flierr( -0.0, y, 58 );
  217. errfl += 1;
  218. }
  219. y = floor(-1.0);
  220. if( y != -1.0 )
  221. {
  222. flierr( -1.0, y, 59 );
  223. errfl += 1;
  224. }
  225. y = floor(-0.1);
  226. if( y != -1.0 )
  227. {
  228. flierr( -0.1, y, 60 );
  229. errfl += 1;
  230. }
  231. if( errfl == 0 )
  232. printf( "No errors found in floor().\n" );
  233. }
  234. flierr( x, y, k )
  235. double x, y;
  236. int k;
  237. {
  238. printf( "Test %d: ", k+1 );
  239. printf( "floor(%.15e) =?= %.15e\n", x, y );
  240. }