
1: #include <fenv.h> 2: #include <math.h> 3: #include <float.h> 4: #include <stdlib.h> 5: #include <stdio.h> 6: 7: float zero = 0.0; 8: float inf = INFINITY; 9: 10: int 11: main (void) 12: { 13: int result = 0; 14: 15: long double tl = (long double) FLT_MAX + 0x1.0p128L; 16: float fi = INFINITY; 17: float m = FLT_MAX; 18: feclearexcept (FE_ALL_EXCEPT); 19: if (nexttowardf (m, tl) != fi) 20: { 21: puts ("nexttowardf+ failed"); 22: ++result; 23: } 24: if (fetestexcept (FE_OVERFLOW) == 0) 25: { 26: puts ("nexttowardf+ did not overflow"); 27: ++result; 28: } 29: feclearexcept (FE_ALL_EXCEPT); 30: if (nexttowardf (-m, -tl) != -fi) 31: { 32: puts ("nexttowardf- failed"); 33: ++result; 34: } 35: if (fetestexcept (FE_OVERFLOW) == 0) 36: { 37: puts ("nexttowardf- did not overflow"); 38: ++result; 39: } 40: 41: fi = 0; 42: m = FLT_MIN; 43: feclearexcept (FE_ALL_EXCEPT); 44: fi = nexttowardf (m, fi); 45: if (fi < 0 || fi >= FLT_MIN) 46: { 47: puts ("nexttowardf+ failed"); 48: ++result; 49: } 50: if (fetestexcept (FE_UNDERFLOW) == 0) 51: { 52: puts ("nexttowardf+ did not underflow"); 53: ++result; 54: } 55: fi = 0; 56: feclearexcept (FE_ALL_EXCEPT); 57: fi = nexttowardf (-m, -fi); 58: if (fi > 0 || fi <= -FLT_MIN) 59: { 60: puts ("nexttowardf- failed"); 61: ++result; 62: } 63: if (fetestexcept (FE_UNDERFLOW) == 0) 64: { 65: puts ("nexttowardf- did not underflow"); 66: ++result; 67: } 68: fi = -INFINITY; 69: feclearexcept (FE_ALL_EXCEPT); 70: m = nexttowardf (zero, inf); 71: if (m < 0.0 || m >= FLT_MIN) 72: { 73: puts ("nexttowardf+ failed"); 74: ++result; 75: } 76: if (fetestexcept (FE_UNDERFLOW) == 0) 77: { 78: puts ("nexttowardf+ did not underflow"); 79: ++result; 80: } 81: feclearexcept (FE_ALL_EXCEPT); 82: if (nexttowardf (m, fi) != 0.0) 83: { 84: puts ("nexttowardf+ failed"); 85: ++result; 86: } 87: if (fetestexcept (FE_UNDERFLOW) == 0) 88: { 89: puts ("nexttowardf+ did not underflow"); 90: ++result; 91: } 92: feclearexcept (FE_ALL_EXCEPT); 93: m = nexttowardf (copysignf (zero, -1.0), -inf); 94: if (m > 0.0 || m <= -FLT_MIN) 95: { 96: puts ("nexttowardf- failed"); 97: ++result; 98: } 99: if (fetestexcept (FE_UNDERFLOW) == 0) 100: { 101: puts ("nexttowardf- did not underflow"); 102: ++result; 103: } 104: feclearexcept (FE_ALL_EXCEPT); 105: if (nexttowardf (m, -fi) != 0.0) 106: { 107: puts ("nexttowardf- failed"); 108: ++result; 109: } 110: if (fetestexcept (FE_UNDERFLOW) == 0) 111: { 112: puts ("nexttowardf- did not underflow"); 113: ++result; 114: } 115: 116: tl = (long double) DBL_MAX + 1.0e305L; 117: double di = INFINITY; 118: double dm = DBL_MAX; 119: feclearexcept (FE_ALL_EXCEPT); 120: if (nexttoward (dm, tl) != di) 121: { 122: puts ("nexttoward+ failed"); 123: ++result; 124: } 125: if (fetestexcept (FE_OVERFLOW) == 0) 126: { 127: puts ("nexttoward+ did not overflow"); 128: ++result; 129: } 130: feclearexcept (FE_ALL_EXCEPT); 131: if (nexttoward (-dm, -tl) != -di) 132: { 133: puts ("nexttoward- failed"); 134: ++result; 135: } 136: if (fetestexcept (FE_OVERFLOW) == 0) 137: { 138: puts ("nexttoward- did not overflow"); 139: ++result; 140: } 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 (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 (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 (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 (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 (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 (fetestexcept (FE_UNDERFLOW) == 0) 212: { 213: puts ("nexttoward- did not underflow"); 214: ++result; 215: } 216: 217: #ifndef NO_LONG_DOUBLE 218: long double li = INFINITY; 219: long double lm = LDBL_MAX; 220: feclearexcept (FE_ALL_EXCEPT); 221: if (nexttowardl (lm, li) != li) 222: { 223: puts ("nexttowardl+ failed"); 224: ++result; 225: } 226: if (fetestexcept (FE_OVERFLOW) == 0) 227: { 228: puts ("nexttowardl+ did not overflow"); 229: ++result; 230: } 231: feclearexcept (FE_ALL_EXCEPT); 232: if (nexttowardl (-lm, -li) != -li) 233: { 234: puts ("nexttowardl failed"); 235: ++result; 236: } 237: if (fetestexcept (FE_OVERFLOW) == 0) 238: { 239: puts ("nexttowardl- did not overflow"); 240: ++result; 241: } 242: 243: li = 0; 244: lm = LDBL_MIN; 245: feclearexcept (FE_ALL_EXCEPT); 246: li = nexttowardl (lm, li); 247: if (li < 0 || li >= LDBL_MIN) 248: { 249: puts ("nexttowardl+ failed"); 250: ++result; 251: } 252: if (fetestexcept (FE_UNDERFLOW) == 0) 253: { 254: puts ("nexttowardl+ did not underflow"); 255: ++result; 256: } 257: li = 0; 258: feclearexcept (FE_ALL_EXCEPT); 259: li = nexttowardl (-lm, -li); 260: if (li > 0 || li <= -LDBL_MIN) 261: { 262: puts ("nexttowardl- failed"); 263: ++result; 264: } 265: if (fetestexcept (FE_UNDERFLOW) == 0) 266: { 267: puts ("nexttowardl- did not underflow"); 268: ++result; 269: } 270: li = -INFINITY; 271: feclearexcept (FE_ALL_EXCEPT); 272: lm = nexttowardl (zero, inf); 273: if (lm < 0.0 || lm >= LDBL_MIN) 274: { 275: puts ("nexttowardl+ failed"); 276: ++result; 277: } 278: if (fetestexcept (FE_UNDERFLOW) == 0) 279: { 280: puts ("nexttowardl+ did not underflow"); 281: ++result; 282: } 283: feclearexcept (FE_ALL_EXCEPT); 284: if (nexttowardl (lm, li) != 0.0) 285: { 286: puts ("nexttowardl+ failed"); 287: ++result; 288: } 289: if (fetestexcept (FE_UNDERFLOW) == 0) 290: { 291: puts ("nexttowardl+ did not underflow"); 292: ++result; 293: } 294: feclearexcept (FE_ALL_EXCEPT); 295: lm = nexttowardl (copysign (zero, -1.0), -inf); 296: if (lm > 0.0 || lm <= -LDBL_MIN) 297: { 298: puts ("nexttowardl- failed"); 299: ++result; 300: } 301: if (fetestexcept (FE_UNDERFLOW) == 0) 302: { 303: puts ("nexttowardl- did not underflow"); 304: ++result; 305: } 306: feclearexcept (FE_ALL_EXCEPT); 307: if (nexttowardl (lm, -li) != 0.0) 308: { 309: puts ("nexttowardl- failed"); 310: ++result; 311: } 312: if (fetestexcept (FE_UNDERFLOW) == 0) 313: { 314: puts ("nexttowardl- did not underflow"); 315: ++result; 316: } 317: #endif 318: 319: return result; 320: }