bug-nexttoward.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. #include <fenv.h>
  2. #include <math.h>
  3. #include <float.h>
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. #include <math-tests.h>
  7. #if !defined(FE_OVERFLOW) && !defined(FE_UNDERFLOW)
  8. /* If there's no support for the exceptions this test is checking,
  9. then just return success and allow the test to be compiled. */
  10. # define fetestexcept(e) 1
  11. #endif
  12. float zero = 0.0;
  13. float inf = INFINITY;
  14. int
  15. main (void)
  16. {
  17. int result = 0;
  18. long double tl = (long double) FLT_MAX + 0x1.0p128L;
  19. float fi = INFINITY;
  20. float m = FLT_MAX;
  21. feclearexcept (FE_ALL_EXCEPT);
  22. if (nexttowardf (m, tl) != fi)
  23. {
  24. puts ("nexttowardf+ failed");
  25. ++result;
  26. }
  27. if (EXCEPTION_TESTS (float) && fetestexcept (FE_OVERFLOW) == 0)
  28. {
  29. puts ("nexttowardf+ did not overflow");
  30. ++result;
  31. }
  32. feclearexcept (FE_ALL_EXCEPT);
  33. if (nexttowardf (-m, -tl) != -fi)
  34. {
  35. puts ("nexttowardf- failed");
  36. ++result;
  37. }
  38. if (EXCEPTION_TESTS (float) && fetestexcept (FE_OVERFLOW) == 0)
  39. {
  40. puts ("nexttowardf- did not overflow");
  41. ++result;
  42. }
  43. fi = 0;
  44. m = FLT_MIN;
  45. feclearexcept (FE_ALL_EXCEPT);
  46. fi = nexttowardf (m, fi);
  47. if (fi < 0 || fi >= FLT_MIN)
  48. {
  49. puts ("nexttowardf+ failed");
  50. ++result;
  51. }
  52. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  53. {
  54. puts ("nexttowardf+ did not underflow");
  55. ++result;
  56. }
  57. fi = 0;
  58. feclearexcept (FE_ALL_EXCEPT);
  59. fi = nexttowardf (-m, -fi);
  60. if (fi > 0 || fi <= -FLT_MIN)
  61. {
  62. puts ("nexttowardf- failed");
  63. ++result;
  64. }
  65. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  66. {
  67. puts ("nexttowardf- did not underflow");
  68. ++result;
  69. }
  70. fi = -INFINITY;
  71. feclearexcept (FE_ALL_EXCEPT);
  72. m = nexttowardf (zero, inf);
  73. if (m < 0.0 || m >= FLT_MIN)
  74. {
  75. puts ("nexttowardf+ failed");
  76. ++result;
  77. }
  78. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  79. {
  80. puts ("nexttowardf+ did not underflow");
  81. ++result;
  82. }
  83. feclearexcept (FE_ALL_EXCEPT);
  84. if (nexttowardf (m, fi) != 0.0)
  85. {
  86. puts ("nexttowardf+ failed");
  87. ++result;
  88. }
  89. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  90. {
  91. puts ("nexttowardf+ did not underflow");
  92. ++result;
  93. }
  94. feclearexcept (FE_ALL_EXCEPT);
  95. m = nexttowardf (copysignf (zero, -1.0), -inf);
  96. if (m > 0.0 || m <= -FLT_MIN)
  97. {
  98. puts ("nexttowardf- failed");
  99. ++result;
  100. }
  101. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  102. {
  103. puts ("nexttowardf- did not underflow");
  104. ++result;
  105. }
  106. feclearexcept (FE_ALL_EXCEPT);
  107. if (nexttowardf (m, -fi) != 0.0)
  108. {
  109. puts ("nexttowardf- failed");
  110. ++result;
  111. }
  112. if (EXCEPTION_TESTS (float) && fetestexcept (FE_UNDERFLOW) == 0)
  113. {
  114. puts ("nexttowardf- did not underflow");
  115. ++result;
  116. }
  117. tl = (long double) DBL_MAX + 1.0e305L;
  118. double di = INFINITY;
  119. double dm = DBL_MAX;
  120. feclearexcept (FE_ALL_EXCEPT);
  121. if (nexttoward (dm, tl) != di)
  122. {
  123. puts ("nexttoward+ failed");
  124. ++result;
  125. }
  126. if (EXCEPTION_TESTS (double) && fetestexcept (FE_OVERFLOW) == 0)
  127. {
  128. puts ("nexttoward+ did not overflow");
  129. ++result;
  130. }
  131. feclearexcept (FE_ALL_EXCEPT);
  132. if (nexttoward (-dm, -tl) != -di)
  133. {
  134. puts ("nexttoward- failed");
  135. ++result;
  136. }
  137. if (EXCEPTION_TESTS (double) && fetestexcept (FE_OVERFLOW) == 0)
  138. {
  139. puts ("nexttoward- did not overflow");
  140. ++result;
  141. }
  142. di = 0;
  143. dm = DBL_MIN;
  144. feclearexcept (FE_ALL_EXCEPT);
  145. di = nexttoward (dm, di);
  146. if (di < 0 || di >= DBL_MIN)
  147. {
  148. puts ("nexttoward+ failed");
  149. ++result;
  150. }
  151. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  152. {
  153. puts ("nexttoward+ did not underflow");
  154. ++result;
  155. }
  156. di = 0;
  157. feclearexcept (FE_ALL_EXCEPT);
  158. di = nexttoward (-dm, -di);
  159. if (di > 0 || di <= -DBL_MIN)
  160. {
  161. puts ("nexttoward- failed");
  162. ++result;
  163. }
  164. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  165. {
  166. puts ("nexttoward- did not underflow");
  167. ++result;
  168. }
  169. di = -INFINITY;
  170. feclearexcept (FE_ALL_EXCEPT);
  171. dm = nexttoward (zero, inf);
  172. if (dm < 0.0 || dm >= DBL_MIN)
  173. {
  174. puts ("nexttoward+ failed");
  175. ++result;
  176. }
  177. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  178. {
  179. puts ("nexttoward+ did not underflow");
  180. ++result;
  181. }
  182. feclearexcept (FE_ALL_EXCEPT);
  183. if (nexttoward (dm, di) != 0.0)
  184. {
  185. puts ("nexttoward+ failed");
  186. ++result;
  187. }
  188. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  189. {
  190. puts ("nexttoward+ did not underflow");
  191. ++result;
  192. }
  193. feclearexcept (FE_ALL_EXCEPT);
  194. dm = nexttoward (copysign (zero, -1.0), -inf);
  195. if (dm > 0.0 || dm <= -DBL_MIN)
  196. {
  197. puts ("nexttoward- failed");
  198. ++result;
  199. }
  200. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  201. {
  202. puts ("nexttoward- did not underflow");
  203. ++result;
  204. }
  205. feclearexcept (FE_ALL_EXCEPT);
  206. if (nexttoward (dm, -di) != 0.0)
  207. {
  208. puts ("nexttoward- failed");
  209. ++result;
  210. }
  211. if (EXCEPTION_TESTS (double) && fetestexcept (FE_UNDERFLOW) == 0)
  212. {
  213. puts ("nexttoward- did not underflow");
  214. ++result;
  215. }
  216. long double li = INFINITY;
  217. long double lm = LDBL_MAX;
  218. feclearexcept (FE_ALL_EXCEPT);
  219. if (nexttowardl (lm, li) != li)
  220. {
  221. puts ("nexttowardl+ failed");
  222. ++result;
  223. }
  224. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_OVERFLOW) == 0)
  225. {
  226. puts ("nexttowardl+ did not overflow");
  227. ++result;
  228. }
  229. feclearexcept (FE_ALL_EXCEPT);
  230. if (nexttowardl (-lm, -li) != -li)
  231. {
  232. puts ("nexttowardl failed");
  233. ++result;
  234. }
  235. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_OVERFLOW) == 0)
  236. {
  237. puts ("nexttowardl- did not overflow");
  238. ++result;
  239. }
  240. li = 0;
  241. lm = LDBL_MIN;
  242. feclearexcept (FE_ALL_EXCEPT);
  243. li = nexttowardl (lm, li);
  244. if (li < 0 || li >= LDBL_MIN)
  245. {
  246. puts ("nexttowardl+ failed");
  247. ++result;
  248. }
  249. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  250. {
  251. puts ("nexttowardl+ did not underflow");
  252. ++result;
  253. }
  254. li = 0;
  255. feclearexcept (FE_ALL_EXCEPT);
  256. li = nexttowardl (-lm, -li);
  257. if (li > 0 || li <= -LDBL_MIN)
  258. {
  259. puts ("nexttowardl- failed");
  260. ++result;
  261. }
  262. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  263. {
  264. puts ("nexttowardl- did not underflow");
  265. ++result;
  266. }
  267. li = -INFINITY;
  268. feclearexcept (FE_ALL_EXCEPT);
  269. lm = nexttowardl (zero, inf);
  270. if (lm < 0.0 || lm >= LDBL_MIN)
  271. {
  272. puts ("nexttowardl+ failed");
  273. ++result;
  274. }
  275. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  276. {
  277. puts ("nexttowardl+ did not underflow");
  278. ++result;
  279. }
  280. feclearexcept (FE_ALL_EXCEPT);
  281. if (nexttowardl (lm, li) != 0.0)
  282. {
  283. puts ("nexttowardl+ failed");
  284. ++result;
  285. }
  286. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  287. {
  288. puts ("nexttowardl+ did not underflow");
  289. ++result;
  290. }
  291. feclearexcept (FE_ALL_EXCEPT);
  292. lm = nexttowardl (copysign (zero, -1.0), -inf);
  293. if (lm > 0.0 || lm <= -LDBL_MIN)
  294. {
  295. puts ("nexttowardl- failed");
  296. ++result;
  297. }
  298. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  299. {
  300. puts ("nexttowardl- did not underflow");
  301. ++result;
  302. }
  303. feclearexcept (FE_ALL_EXCEPT);
  304. if (nexttowardl (lm, -li) != 0.0)
  305. {
  306. puts ("nexttowardl- failed");
  307. ++result;
  308. }
  309. if (EXCEPTION_TESTS (long double) && fetestexcept (FE_UNDERFLOW) == 0)
  310. {
  311. puts ("nexttowardl- did not underflow");
  312. ++result;
  313. }
  314. return result;
  315. }