fltestl.c 3.9 KB

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