void test_nexttoward() { static_assert((std::is_same<decltype(nexttoward((double)0, (long double)0)), double>::value), ""); static_assert((std::is_same<decltype(nexttowardf(0, (long double)0)), float>::value), ""); static_assert((std::is_same<decltype(nexttowardl(0, (long double)0)), long double>::value), ""); assert(nexttoward(0, 1) == hexfloat<double>(0x1, 0, -1074)); }
float nexttowardf (float x, long double y) { return (float) nexttoward( (double)x, (double)y ); }
int main(int argc, char *argv[]) { static const int ex_under = FE_UNDERFLOW | FE_INEXACT; /* shorthand */ static const int ex_over = FE_OVERFLOW | FE_INEXACT; long double ldbl_small, ldbl_eps, ldbl_max; printf("1..5\n"); #ifdef __i386__ fpsetprec(FP_PE); #endif /* * We can't use a compile-time constant here because gcc on * FreeBSD/i386 assumes long doubles are truncated to the * double format. */ ldbl_small = ldexpl(1.0, LDBL_MIN_EXP - LDBL_MANT_DIG); ldbl_eps = LDBL_EPSILON; ldbl_max = ldexpl(1.0 - ldbl_eps / 2, LDBL_MAX_EXP); /* * Special cases involving zeroes. */ #define ztest(prec) \ test##prec(copysign##prec(1.0, nextafter##prec(0.0, -0.0)), -1.0, 0); \ test##prec(copysign##prec(1.0, nextafter##prec(-0.0, 0.0)), 1.0, 0); \ test##prec(copysign##prec(1.0, nexttoward##prec(0.0, -0.0)), -1.0, 0);\ test##prec(copysign##prec(1.0, nexttoward##prec(-0.0, 0.0)), 1.0, 0) ztest(); ztest(f); ztest(l); #undef ztest #define stest(next, eps, prec) \ test##prec(next(-0.0, 42.0), eps, ex_under); \ test##prec(next(0.0, -42.0), -eps, ex_under); \ test##prec(next(0.0, INFINITY), eps, ex_under); \ test##prec(next(-0.0, -INFINITY), -eps, ex_under) stest(nextafter, 0x1p-1074, ); stest(nextafterf, 0x1p-149f, f); stest(nextafterl, ldbl_small, l); stest(nexttoward, 0x1p-1074, ); stest(nexttowardf, 0x1p-149f, f); stest(nexttowardl, ldbl_small, l); #undef stest printf("ok 1 - next\n"); /* * `x == y' and NaN tests */ testall(42.0, 42.0, 42.0, 0); testall(-42.0, -42.0, -42.0, 0); testall(INFINITY, INFINITY, INFINITY, 0); testall(-INFINITY, -INFINITY, -INFINITY, 0); testall(NAN, 42.0, NAN, 0); testall(42.0, NAN, NAN, 0); testall(NAN, NAN, NAN, 0); printf("ok 2 - next\n"); /* * Tests where x is an ordinary normalized number */ testboth(1.0, 2.0, 1.0 + DBL_EPSILON, 0, ); testboth(1.0, -INFINITY, 1.0 - DBL_EPSILON/2, 0, ); testboth(1.0, 2.0, 1.0 + FLT_EPSILON, 0, f); testboth(1.0, -INFINITY, 1.0 - FLT_EPSILON/2, 0, f); testboth(1.0, 2.0, 1.0 + ldbl_eps, 0, l); testboth(1.0, -INFINITY, 1.0 - ldbl_eps/2, 0, l); testboth(-1.0, 2.0, -1.0 + DBL_EPSILON/2, 0, ); testboth(-1.0, -INFINITY, -1.0 - DBL_EPSILON, 0, ); testboth(-1.0, 2.0, -1.0 + FLT_EPSILON/2, 0, f); testboth(-1.0, -INFINITY, -1.0 - FLT_EPSILON, 0, f); testboth(-1.0, 2.0, -1.0 + ldbl_eps/2, 0, l); testboth(-1.0, -INFINITY, -1.0 - ldbl_eps, 0, l); /* Cases where nextafter(...) != nexttoward(...) */ test(nexttoward(1.0, 1.0 + ldbl_eps), 1.0 + DBL_EPSILON, 0); testf(nexttowardf(1.0, 1.0 + ldbl_eps), 1.0 + FLT_EPSILON, 0); testl(nexttowardl(1.0, 1.0 + ldbl_eps), 1.0 + ldbl_eps, 0); printf("ok 3 - next\n"); /* * Tests at word boundaries, normalization boundaries, etc. */ testboth(0x1.87654ffffffffp+0, INFINITY, 0x1.87655p+0, 0, ); testboth(0x1.87655p+0, -INFINITY, 0x1.87654ffffffffp+0, 0, ); testboth(0x1.fffffffffffffp+0, INFINITY, 0x1p1, 0, ); testboth(0x1p1, -INFINITY, 0x1.fffffffffffffp+0, 0, ); testboth(0x0.fffffffffffffp-1022, INFINITY, 0x1p-1022, 0, ); testboth(0x1p-1022, -INFINITY, 0x0.fffffffffffffp-1022, ex_under, ); testboth(0x1.fffffep0f, INFINITY, 0x1p1, 0, f); testboth(0x1p1, -INFINITY, 0x1.fffffep0f, 0, f); testboth(0x0.fffffep-126f, INFINITY, 0x1p-126f, 0, f); testboth(0x1p-126f, -INFINITY, 0x0.fffffep-126f, ex_under, f); #if LDBL_MANT_DIG == 53 testboth(0x1.87654ffffffffp+0L, INFINITY, 0x1.87655p+0L, 0, l); testboth(0x1.87655p+0L, -INFINITY, 0x1.87654ffffffffp+0L, 0, l); testboth(0x1.fffffffffffffp+0L, INFINITY, 0x1p1L, 0, l); testboth(0x1p1L, -INFINITY, 0x1.fffffffffffffp+0L, 0, l); testboth(0x0.fffffffffffffp-1022L, INFINITY, 0x1p-1022L, 0, l); testboth(0x1p-1022L, -INFINITY, 0x0.fffffffffffffp-1022L, ex_under, l); #elif LDBL_MANT_DIG == 64 && !defined(__i386) testboth(0x1.87654321fffffffep+0L, INFINITY, 0x1.87654322p+0L, 0, l); testboth(0x1.87654322p+0L, -INFINITY, 0x1.87654321fffffffep+0L, 0, l); testboth(0x1.fffffffffffffffep0L, INFINITY, 0x1p1L, 0, l); testboth(0x1p1L, -INFINITY, 0x1.fffffffffffffffep0L, 0, l); testboth(0x0.fffffffffffffffep-16382L, INFINITY, 0x1p-16382L, 0, l); testboth(0x1p-16382L, -INFINITY, 0x0.fffffffffffffffep-16382L, ex_under, l); #elif LDBL_MANT_DIG == 113 testboth(0x1.876543210987ffffffffffffffffp+0L, INFINITY, 0x1.876543210988p+0, 0, l); testboth(0x1.876543210988p+0L, -INFINITY, 0x1.876543210987ffffffffffffffffp+0L, 0, l); testboth(0x1.ffffffffffffffffffffffffffffp0L, INFINITY, 0x1p1L, 0, l); testboth(0x1p1L, -INFINITY, 0x1.ffffffffffffffffffffffffffffp0L, 0, l); testboth(0x0.ffffffffffffffffffffffffffffp-16382L, INFINITY, 0x1p-16382L, 0, l); testboth(0x1p-16382L, -INFINITY, 0x0.ffffffffffffffffffffffffffffp-16382L, ex_under, l); #endif printf("ok 4 - next\n"); /* * Overflow tests */ test(idd(nextafter(DBL_MAX, INFINITY)), INFINITY, ex_over); test(idd(nextafter(INFINITY, 0.0)), DBL_MAX, 0); test(idd(nexttoward(DBL_MAX, DBL_MAX * 2.0L)), INFINITY, ex_over); #if LDBL_MANT_DIG > 53 test(idd(nexttoward(INFINITY, DBL_MAX * 2.0L)), DBL_MAX, 0); #endif testf(idf(nextafterf(FLT_MAX, INFINITY)), INFINITY, ex_over); testf(idf(nextafterf(INFINITY, 0.0)), FLT_MAX, 0); testf(idf(nexttowardf(FLT_MAX, FLT_MAX * 2.0)), INFINITY, ex_over); testf(idf(nexttowardf(INFINITY, FLT_MAX * 2.0)), FLT_MAX, 0); testboth(ldbl_max, INFINITY, INFINITY, ex_over, l); testboth(INFINITY, 0.0, ldbl_max, 0, l); printf("ok 5 - next\n"); return (0); }
static void F(compile_test) (void) { TYPE a, b, c = 1.0; complex TYPE d; int i; int saved_count; long int j; long long int k; a = cos (cos (x)); b = acos (acos (a)); a = sin (sin (x)); b = asin (asin (a)); a = tan (tan (x)); b = atan (atan (a)); c = atan2 (atan2 (a, c), atan2 (b, x)); a = cosh (cosh (x)); b = acosh (acosh (a)); a = sinh (sinh (x)); b = asinh (asinh (a)); a = tanh (tanh (x)); b = atanh (atanh (a)); a = exp (exp (x)); b = log (log (a)); a = log10 (log10 (x)); b = ldexp (ldexp (a, 1), 5); a = frexp (frexp (x, &i), &i); b = expm1 (expm1 (a)); a = log1p (log1p (x)); b = logb (logb (a)); a = exp2 (exp2 (x)); b = log2 (log2 (a)); a = pow (pow (x, a), pow (c, b)); b = sqrt (sqrt (a)); a = hypot (hypot (x, b), hypot (c, a)); b = cbrt (cbrt (a)); a = ceil (ceil (x)); b = fabs (fabs (a)); a = floor (floor (x)); b = fmod (fmod (a, b), fmod (c, x)); a = nearbyint (nearbyint (x)); b = round (round (a)); a = trunc (trunc (x)); b = remquo (remquo (a, b, &i), remquo (c, x, &i), &i); j = lrint (x) + lround (a); k = llrint (b) + llround (c); a = erf (erf (x)); b = erfc (erfc (a)); a = tgamma (tgamma (x)); b = lgamma (lgamma (a)); a = rint (rint (x)); b = nextafter (nextafter (a, b), nextafter (c, x)); a = nextdown (nextdown (a)); b = nexttoward (nexttoward (x, a), c); a = nextup (nextup (a)); b = remainder (remainder (a, b), remainder (c, x)); a = scalb (scalb (x, a), (TYPE) (6)); k = scalbn (a, 7) + scalbln (c, 10l); i = ilogb (x); j = llogb (x); a = fdim (fdim (x, a), fdim (c, b)); b = fmax (fmax (a, x), fmax (c, b)); a = fmin (fmin (x, a), fmin (c, b)); b = fma (sin (a), sin (x), sin (c)); a = totalorder (totalorder (x, b), totalorder (c, x)); b = totalordermag (totalordermag (x, a), totalordermag (c, x)); #ifdef TEST_INT a = atan2 (i, b); b = remquo (i, a, &i); c = fma (i, b, i); a = pow (i, c); #endif x = a + b + c + i + j + k; saved_count = count; if (ccount != 0) ccount = -10000; d = cos (cos (z)); z = acos (acos (d)); d = sin (sin (z)); z = asin (asin (d)); d = tan (tan (z)); z = atan (atan (d)); d = cosh (cosh (z)); z = acosh (acosh (d)); d = sinh (sinh (z)); z = asinh (asinh (d)); d = tanh (tanh (z)); z = atanh (atanh (d)); d = exp (exp (z)); z = log (log (d)); d = sqrt (sqrt (z)); z = conj (conj (d)); d = fabs (conj (a)); z = pow (pow (a, d), pow (b, z)); d = cproj (cproj (z)); z += fabs (cproj (a)); a = carg (carg (z)); b = creal (creal (d)); c = cimag (cimag (z)); x += a + b + c + i + j + k; z += d; if (saved_count != count) count = -10000; if (0) { a = cos (y); a = acos (y); a = sin (y); a = asin (y); a = tan (y); a = atan (y); a = atan2 (y, y); a = cosh (y); a = acosh (y); a = sinh (y); a = asinh (y); a = tanh (y); a = atanh (y); a = exp (y); a = log (y); a = log10 (y); a = ldexp (y, 5); a = frexp (y, &i); a = expm1 (y); a = log1p (y); a = logb (y); a = exp2 (y); a = log2 (y); a = pow (y, y); a = sqrt (y); a = hypot (y, y); a = cbrt (y); a = ceil (y); a = fabs (y); a = floor (y); a = fmod (y, y); a = nearbyint (y); a = round (y); a = trunc (y); a = remquo (y, y, &i); j = lrint (y) + lround (y); k = llrint (y) + llround (y); a = erf (y); a = erfc (y); a = tgamma (y); a = lgamma (y); a = rint (y); a = nextafter (y, y); a = nexttoward (y, y); a = remainder (y, y); a = scalb (y, (const TYPE) (6)); k = scalbn (y, 7) + scalbln (y, 10l); i = ilogb (y); j = llogb (y); a = fdim (y, y); a = fmax (y, y); a = fmin (y, y); a = fma (y, y, y); a = totalorder (y, y); a = totalordermag (y, y); #ifdef TEST_INT a = atan2 (i, y); a = remquo (i, y, &i); a = fma (i, y, i); a = pow (i, y); #endif d = cos ((const complex TYPE) z); d = acos ((const complex TYPE) z); d = sin ((const complex TYPE) z); d = asin ((const complex TYPE) z); d = tan ((const complex TYPE) z); d = atan ((const complex TYPE) z); d = cosh ((const complex TYPE) z); d = acosh ((const complex TYPE) z); d = sinh ((const complex TYPE) z); d = asinh ((const complex TYPE) z); d = tanh ((const complex TYPE) z); d = atanh ((const complex TYPE) z); d = exp ((const complex TYPE) z); d = log ((const complex TYPE) z); d = sqrt ((const complex TYPE) z); d = pow ((const complex TYPE) z, (const complex TYPE) z); d = fabs ((const complex TYPE) z); d = carg ((const complex TYPE) z); d = creal ((const complex TYPE) z); d = cimag ((const complex TYPE) z); d = conj ((const complex TYPE) z); d = cproj ((const complex TYPE) z); } }
CAMLprim value math_nexttoward(value x, value y) { CAMLparam2(x, y); CAMLreturn(caml_copy_double(nexttoward(Double_val(x), Double_val(y)))); }
void math (double d, int *ex, double *dp) { acos (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 8 } */ acosh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 10 } */ asin (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 12 } */ asinh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 14 } */ atan (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 16 } */ atanh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 18 } */ atan2 (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 20 } */ cbrt (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 22 } */ ceil (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 24 } */ copysign (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 26 } */ cos (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 28 } */ cosh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 30 } */ erf (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 32 } */ erfc (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 34 } */ exp (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 36 } */ exp2 (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 38 } */ expm1 (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 40 } */ fabs (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 42 } */ fdim (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 44 } */ floor (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 46 } */ fma (d, d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 48 } */ fmax (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 50 } */ fmin (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 52 } */ fmod (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 54 } */ frexp (d, ex); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 56 } */ hypot (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 58 } */ /* We don't generate the warning for ilogb. */ ilogb (d); ldexp (d, *ex); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 62 } */ lgamma (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 64 } */ llrint (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 66 } */ llround (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 68 } */ log (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 70 } */ log10 (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 72 } */ log1p (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 74 } */ log2 (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 76 } */ logb (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 78 } */ lrint (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 80 } */ lround (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 82 } */ modf (d, dp); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 84 } */ nan (""); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 86 } */ nearbyint (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 88 } */ nextafter (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 90 } */ nexttoward (d, 20.0L); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 92 } */ pow (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 94 } */ remainder (d, d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 96 } */ remquo (d, d, ex); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 98 } */ rint (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 100 } */ round (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 102 } */ scalbln (d, 100L); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 104 } */ scalbn (d, 100); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 106 } */ sin (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 108 } */ sinh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 110 } */ sincos (d, dp, dp); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 112 } */ sqrt (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 114 } */ tan (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 116 } */ tanh (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 118 } */ tgamma (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 120 } */ trunc (d); /* { dg-warning "incompatible implicit" } */ /* { dg-message "include ..math.h.." "" { target *-*-* } 122 } */ }
int main() { /* # Math errors # Errors The following errors exist for the standard library functions. # Domain error Value outside of function domain. E.g.: `sqrt(-1.0)`. TODO undefined or implementation defined? Detection: if `math_errhandling & MATH_ERRNO != 0`, `errno = ERANGE` and a "divide-by-zero" floating point exception is raised. # Pole error Function has a pole at a point. Ex: `log(0.0)`, `tgamma(-1.0)`. Return value: HUGE_VAL. Detection if `math_errhandling & MATH_ERRNO != 0`, `errno = ERANGE` and a "divide-by-zero" floating point exception is raised. # Range errors Occur if the result is too large or too small to fint into the return type. There are two types of range errors overflow and underflow. In both cases, if `math_errhandling & MATH_ERRNO != 0`, `errno = ERANGE` and a "divide-by-zero" floating point exception is raised. # Overflow For exapmle, around poles, functions can have arbitrarily large values, so it is possible that for a given input close enough to the pole that the output is too large to reprent. Return value: HUGE_VAL, HUGE_VALF, or HUGE_VALL, acording to function's return type. # Underflow The output is too small to represent Return value: an implementation-defined value whose magnitude is no greater than the smallest normalized positive number in the specified type; */ printf("math_errhandling & MATH_ERRNO = %d\n", math_errhandling & MATH_ERRNO); /* # fabs Floating point abs. Integer version is in stdlib */ { /* Absolute values, float version: */ assert(fabsl(-1.1) == 1.1); } #if __STDC_VERSION__ >= 199901L /* # fminl # fmaxl Max and min for floats. */ { assert(fminl(0.1, 0.2) == 0.1); assert(fmaxl(0.1, 0.2) == 0.2); } #endif /* # Rounding */ { /* # floor # ceil */ { assert(fabs(floor(0.5) - 0.0) < ERR); assert(fabs(ceil(0.5) - 1.0) < ERR); } /* # trunc Never raises any errors because the new result always fits in the data type (magnitide decresases). */ { assert(fabs(trunc(1.5) - 1.0) < ERR); assert(fabs(trunc(-1.5) - -1.0) < ERR); } /* # round Away from 0 on mid case. */ { assert(fabs(round( 1.25) - 1.0) < ERR); assert(fabs(round( 1.5 ) - 2.0) < ERR); assert(fabs(round( 1.75) - 2.0) < ERR); assert(fabs(round(-1.5 ) - -2.0) < ERR); } /* # modf Decompose into fraction and integer parts. */ { double d; assert(fabs(modf(12.34, &d) - 0.34) < ERR); assert(fabs(d - 12.0 ) < ERR); } /* # frexp Decompose into: - integer exponent - normalized mantissa, a fraction between 0.5 (inclusive) and 1.0 (exclusive) Guaranteed to power of 2 representation, even though this is not true for floating point? E.g. FLT_RADIX macros indicate it. */ { int i; /* 1.5 = 0.75 * 2^1 */ assert(frexp(1.5, &i) == 0.75); assert(i == 1); /* 3.0 = 0.75 * 2^2 */ assert(frexp(3.0, &i) == 0.75); assert(i == 2); } } /* # fma Fused multiple add or floating point multiply and add. Does addition and multiplication on one operation, with a single rounding, reduncing rounding errors. Has hardware implementations on certain platforms. */ { assert(fabs(fma(2.0, 3.0, 4.0) - (2.0 * 3.0 + 4.0)) < ERR); } /* # Exponential functions */ { /* # exp */ { assert(fabs(exp(1.0) - 2.71) < 0.01); } /* # ln See log. # log Calculates the ln. */ { assert(fabs(log(exp(1.0)) - 1.0) < ERR); assert(fabs(log2(8.0) - 3.0) < ERR); assert(fabs(log10(100.0) - 2.0) < ERR); } /* # sqrt Range is positive or zero. Negatives are a range error. To get complex on negative values, use `csqrt`. */ { assert(fabs(sqrt(4.0) - 2.0) < ERR); /* GCC 4.7 -O3 is smart enough to see that this is bad: */ { float f = -4.0; /*printf("sqrt(-4.0) = %f\n", sqrt(f));*/ } { float f; volatile float g; f = -4.0; errno = 0; g = sqrt(f); if (math_errhandling & MATH_ERRNO) assert(errno == EDOM); printf("sqrt(-4.0) = %f\n", f); } } #if __STDC_VERSION__ >= 199901L /* # hypot Hypotenuse: sqrt(x^2 + y^2) */ { assert(fabs(hypot(3.0, 4.0) - 5.0) < ERR); } #endif #if __STDC_VERSION__ >= 199901L /* # cbrt CuBe RooT */ { assert(fabs(cbrt(8.0 ) - 2.0) < ERR); assert(fabs(cbrt(-8.0) - -2.0) < ERR); } #endif /* # pow */ { assert(fabs(pow(2.0, 3.0) - 8.0 ) < ERR); } } /* # trig */ { float f = sin(0.2); assert(fabs(sin(0.0) - 0.0) < ERR); assert(fabs(cos(0.0) - 1.0) < ERR); /* # PI There is no predefined macro for PI. TODO0 why not? so convenient... This is a standard way to get PI. The only problem is a possible slight calculation overhead. But don't worry much about it. For example in gcc 4.7, even with `gcc -O0` trigonometric functions are calculated at compile time and stored in the program text. */ { assert(fabs(acos(-1.0) - 3.14) < 0.01); } } /* # erf: TODO0 understand */ /* # factorial There seems to be no integer factorial function, but `gamma(n+1)` coincides with the factorials of `n` on the positive integers, and may be faster to compute via analytic approximations that can be done to gamma and/or via a hardware implementation, so just use gamma. # gamma Wiki link: <http://en.wikipedia.org/wiki/Gamma_function> Extension of the factorials to the real numbers because: - on the positive integergs: gamma(n+1) == n! - on the positive reals: gamma(x+1) == x * gamma(x) Has a holomorphic continuation to all imaginary plane, with poles on 0 and negative integers. Implemented in C as `tgamma`. # tgamma True Gamma function. TODO0 Why True? Computes the gamma function. ANSI C says that it gives either domain or pole error on the negative integers. # lgamma lgamma = ln tgamma */ { assert(fabs(tgamma(5.0) - 4*3*2) < ERR); assert(fabs(tgamma(3.5) - 2.5*tgamma(2.5)) < ERR); errno = 0; volatile double d = tgamma(-1.0); if (math_errhandling & MATH_ERRNO) { if (errno == ERANGE) assert(d == HUGE_VAL); else assert(errno == EDOM); } assert(fabs(lgamma(3.5) - log(tgamma(3.5))) < ERR); } /* Floating point manipulation functions. */ { /* # ldexp ldexp(x, y) = x * (2^y) */ { assert(fabs(ldexp(1.5, 2.0) - 6.0) < ERR); } /* # nextafter Return the next representable value in a direction. If both arguments equal, return them. # nexttowards TODO diff from nextafter */ { printf("nexttowards(0.0, 1.0) = %a\n", nexttoward(0.0, 1.0)); assert(nexttoward(0.0, 0.0) == 0.0); printf("nextafter (0.0, 1.0) = %a\n", nextafter(0.0, 1.0)); } } /* # Division by 0 Time to have some fun and do naughty things. The outcome of a division by zero depends on wether it is an integer of floating operation. # isfinite # isinf # isnan # isnormal TODO # fpclassify FP_INFINITE, FP_NAN, FP_NORMAL, FP_SUBNORMAL, FP_ZERO */ { /* # floating point exception In x86, the following generates a floating point exception, which is handled by a floating point exception handler function. In Linux the default handler is implemented by the OS and sends a signal to our application, which if we don't catch will kill us. */ if (0) { /* gcc 4.7 is smart enough to warn on literal division by 0: */ { /*int i = 1 / 0;*/ } /* gcc 4.7 is not smart enough to warn here: */ { volatile int i = 0; printf("int 1/0 = %d\n", 1 / i); /* On gcc 4.7 with `-O3` this may not generate an exception, */ /* as the compiler replaces 0 / X by 0. */ printf("int 0/0 = %d\n", 0 / i); } } /* # HUGE_VAL Returned on overflow. TODO example. Can equal `INFINITY`. */ { /* double */ printf("HUGE_VAL = %f\n", HUGE_VAL); /* float */ printf("HUGE_VALF = %f\n", HUGE_VALF); /* long double */ printf("HUGE_VALL = %Lf\n", HUGE_VALL); } /* # INFINITY Result of operations such as: 1.0 / 0.0 Type: float. There are two infinities: positive and negative. It is possible that `INFINITY == HUGE_VALF`. */ { /* [-]inf or [-]infinity implementation defined. */ printf("INFINITY = %f\n", INFINITY); printf("-INFINITY = %f\n", -INFINITY); volatile float f = 0; assert(1 / f == INFINITY); assert(isinf(INFINITY)); assert(!isnan(INFINITY)); assert(INFINITY + INFINITY == INFINITY); assert(INFINITY + 1.0 == INFINITY); assert(INFINITY - 1.0 == INFINITY); assert(isnan(INFINITY - INFINITY)); assert(INFINITY * INFINITY == INFINITY); assert(INFINITY * -INFINITY == -INFINITY); assert(INFINITY * 2.0 == INFINITY); assert(INFINITY * -1.0 == -INFINITY); assert(isnan(INFINITY * 0.0)); assert(1.0 / INFINITY == 0.0); assert(isnan(INFINITY / INFINITY)); /* Comparisons with INFINITY all work as expected. */ assert(INFINITY == INFINITY); assert(INFINITY != - INFINITY); assert(-INFINITY < - 1e100); assert(1e100 < INFINITY); } /* # NAN Not a number. Result of operations such as: 0.0 / 0.0 INFINITY - INFINITY INFINITY * 0.o INFINITY / INFINITY And any operation involving NAN. The sign of NAN has no meaning. */ { /* [-]NAN or [-]NAN<more characters> implementation defined. */ printf("NAN = %f\n", NAN); printf("-NAN = %f\n", -NAN); /* TODO why do both fail? */ /*assert(0 / f == -NAN);*/ /*assert(0 / f == NAN);*/ volatile float f = 0; assert(isnan(0 / f)); assert(isnan(NAN)); assert(!isinf(NAN)); assert(isnan(NAN)); assert(isnan(NAN + 1.0)); assert(isnan(NAN + INFINITY)); assert(isnan(NAN + NAN)); assert(isnan(NAN - 1.0)); assert(isnan(NAN * 2.0)); assert(isnan(NAN / 1.0)); assert(isnan(INFINITY - INFINITY)); assert(isnan(INFINITY * 0.0)); assert(isnan(INFINITY / INFINITY)); /* NAN is not ordered. any compairison to it yields false! This is logical since 0 is neither smaller, larger or equal to NAN. */ { assert(!(0.0 < NAN)); assert(!(0.0 > NAN)); assert(!(0.0 == NAN)); } } #if __STDC_VERSION__ >= 199901L /* # isunordered Macro. isunordered(x, y) Equals: isnan(x) || isnan(y) Likely exists because it is possible to optimize it in x86: http://stackoverflow.com/questions/26053934/is-it-feasible-to-add-this-optimization-to-gcc?rq=1 */ { assert(!isunordered(1.0, 1.0)); assert(isunordered(NAN, 1.0)); assert(isunordered(1.0, NAN)); assert(isunordered(NAN, NAN)); } #endif } /* # FLT_EVAL_METHOD # float_t # double_t */ { printf("FLT_EVAL_METHOD = %d\n", FLT_EVAL_METHOD); } return EXIT_SUCCESS; }
TEST(math, nexttoward) { ASSERT_DOUBLE_EQ(0.0, nexttoward(0.0, 0.0L)); ASSERT_DOUBLE_EQ(4.9406564584124654e-324, nexttoward(0.0, 1.0L)); ASSERT_DOUBLE_EQ(-4.9406564584124654e-324, nexttoward(0.0, -1.0L)); }
long double nexttowardl (long double x, long double y) { return (long double) nexttoward( (double)x, (double)y ); }
static int do_test (void) { int result = 0; #ifndef NO_LONG_DOUBLE { long double x = 0x100000001ll + (long double) 0.5; long double q; long double r; r = modfl (x, &q); if (q != (long double) 0x100000001ll || r != 0.5) { printf ("modfl (%Lg, ...) failed\n", x); result = 1; } } { long double x; long double m; long double r; int e; int i; # if LDBL_MANT_DIG == 64 m = 0xf.fffffffffffffffp-4L; # elif LDBL_MANT_DIG == 106 /* This has to match the mantissa of LDBL_MAX which actually does have a missing bit in the middle. */ m = 0x1.fffffffffffff7ffffffffffff8p-1L; # elif LDBL_MANT_DIG == 113 m = 0x1.ffffffffffffffffffffffffffffp-1L; # else # error "Please adjust" # endif for (i = LDBL_MAX_EXP, x = LDBL_MAX; i >= LDBL_MIN_EXP; --i, x /= 2.0L) { printf ("2^%d: ", i); r = frexpl (x, &e); if (r != m) { printf ("mantissa incorrect: %.20La\n", r); result = 1; continue; } if (e != i) { printf ("exponent wrong %d (%.20Lg)\n", e, x); result = 1; continue; } puts ("ok"); } for (i = LDBL_MIN_EXP, x = LDBL_MIN; i >= LDBL_MIN_EXP - LDBL_MANT_DIG + 1; --i, x /= 2.0L) { printf ("2^%d: ", i); r = frexpl (x, &e); if (r != 0.5L) { printf ("mantissa incorrect: %.20La\n", r); result = 1; continue; } if (e != i) { printf ("exponent wrong %d (%.20Lg)\n", e, x); result = 1; continue; } puts ("ok"); } } # if 0 { int e; long double r = frexpl (LDBL_MIN * LDBL_EPSILON, &e); if (r != 0.5) { printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): mantissa wrong: %Lg\n", r); result = 1; } else if (e != -16444) { printf ("frexpl (LDBL_MIN * LDBL_EPSILON, ...): exponent wrong: %d\n", e); result = 1; } } # endif #endif { double x = 0x100000001ll + (double) 0.5; double q; double r; r = modf (x, &q); if (q != (double) 0x100000001ll || r != 0.5) { printf ("modf (%g, ...) failed\n", x); result = 1; } } { union ieee754_float v1; union ieee754_float v2; float f; v1.f = f = FLT_MIN; if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (FLT_MIN) failed: %d\n", fpclassify (f)); result = 1; } f = nextafterf (f, FLT_MIN / 2.0f); if (fpclassify (f) != FP_SUBNORMAL) { printf ("fpclassify (FLT_MIN-epsilon) failed: %d\n", fpclassify (f)); result = 1; } v2.f = f = nextafterf (f, FLT_MIN); if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (FLT_MIN-epsilon+epsilon) failed: %d\n", fpclassify (f)); result = 1; } if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("FLT_MIN: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("FLT_MIN: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("FLT_MIN: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.f = f = -FLT_MIN; if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (-FLT_MIN) failed: %d\n", fpclassify (f)); result = 1; } f = nextafterf (f, -FLT_MIN / 2.0f); if (fpclassify (f) != FP_SUBNORMAL) { printf ("fpclassify (-FLT_MIN-epsilon) failed: %d\n", fpclassify (f)); result = 1; } v2.f = f = nextafterf (f, -FLT_MIN); if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (-FLT_MIN-epsilon+epsilon) failed: %d\n", fpclassify (f)); result = 1; } if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("-FLT_MIN: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-FLT_MIN: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-FLT_MIN: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } f = FLT_MAX; if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (FLT_MAX) failed: %d\n", fpclassify (f)); result = 1; } f = nextafterf (f, INFINITY); if (fpclassify (f) != FP_INFINITE) { printf ("fpclassify (FLT_MAX+epsilon) failed: %d\n", fpclassify (f)); result = 1; } f = -FLT_MAX; if (fpclassify (f) != FP_NORMAL) { printf ("fpclassify (-FLT_MAX) failed: %d\n", fpclassify (f)); result = 1; } f = nextafterf (f, -INFINITY); if (fpclassify (f) != FP_INFINITE) { printf ("fpclassify (-FLT_MAX-epsilon) failed: %d\n", fpclassify (f)); result = 1; } v1.f = f = 0.0625; f = nextafterf (f, 0.0); v2.f = f = nextafterf (f, 1.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("0.0625f down: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0625f down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("0.0625f down: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.f = f = 0.0625; f = nextafterf (f, 1.0); v2.f = f = nextafterf (f, 0.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("0.0625f up: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0625f up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("0.0625f up: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.f = f = -0.0625; f = nextafterf (f, 0.0); v2.f = f = nextafterf (f, -1.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("-0.0625f up: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-0.0625f up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-0.0625f up: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.f = f = -0.0625; f = nextafterf (f, -1.0); v2.f = f = nextafterf (f, 0.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("-0.0625f down: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-0.0625f down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-0.0625f down: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.f = f = 0.0f; f = nextafterf (f, 1.0); v2.f = nextafterf (f, -1.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("0.0f up: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0f up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (0 != v2.ieee.negative) { printf ("0.0f up: negative differs: 0 vs %d\n", v2.ieee.negative); result = 1; } v1.f = f = 0.0f; f = nextafterf (f, -1.0); v2.f = nextafterf (f, 1.0); if (v1.ieee.mantissa != v2.ieee.mantissa) { printf ("0.0f down: mantissa differs: %8x vs %8x\n", v1.ieee.mantissa, v2.ieee.mantissa); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0f down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (1 != v2.ieee.negative) { printf ("0.0f down: negative differs: 1 vs %d\n", v2.ieee.negative); result = 1; } if (nextafterf (0.0f, INFINITY) != nextafterf (0.0f, 1.0f) || nextafterf (-0.0f, INFINITY) != nextafterf (-0.0f, 1.0f) || nextafterf (0.0f, -INFINITY) != nextafterf (0.0f, -1.0f) || nextafterf (-0.0f, -INFINITY) != nextafterf (-0.0f, -1.0f)) { printf ("nextafterf (+-0, +-Inf) != nextafterf (+-0, +-1)\n"); result = 1; } if (nexttowardf (0.0f, INFINITY) != nexttowardf (0.0f, 1.0f) || nexttowardf (-0.0f, INFINITY) != nexttowardf (-0.0f, 1.0f) || nexttowardf (0.0f, -INFINITY) != nexttowardf (0.0f, -1.0f) || nexttowardf (-0.0f, -INFINITY) != nexttowardf (-0.0f, -1.0f)) { printf ("nexttowardf (+-0, +-Inf) != nexttowardf (+-0, +-1)\n"); result = 1; } } { union ieee754_double v1; union ieee754_double v2; double d; v1.d = d = DBL_MIN; if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (DBL_MIN) failed: %d\n", fpclassify (d)); result = 1; } d = nextafter (d, DBL_MIN / 2.0); if (fpclassify (d) != FP_SUBNORMAL) { printf ("fpclassify (DBL_MIN-epsilon) failed: %d\n", fpclassify (d)); result = 1; } v2.d = d = nextafter (d, DBL_MIN); if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (DBL_MIN-epsilon+epsilon) failed: %d\n", fpclassify (d)); result = 1; } if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("DBL_MIN: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("DBL_MIN: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("DBL_MIN: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("DBL_MIN: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.d = d = -DBL_MIN; if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (-DBL_MIN) failed: %d\n", fpclassify (d)); result = 1; } d = nextafter (d, -DBL_MIN / 2.0); if (fpclassify (d) != FP_SUBNORMAL) { printf ("fpclassify (-DBL_MIN-epsilon) failed: %d\n", fpclassify (d)); result = 1; } v2.d = d = nextafter (d, -DBL_MIN); if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (-DBL_MIN-epsilon+epsilon) failed: %d\n", fpclassify (d)); result = 1; } if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("-DBL_MIN: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("-DBL_MIN: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-DBL_MIN: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-DBL_MIN: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } d = DBL_MAX; if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (DBL_MAX) failed: %d\n", fpclassify (d)); result = 1; } d = nextafter (d, INFINITY); if (fpclassify (d) != FP_INFINITE) { printf ("fpclassify (DBL_MAX+epsilon) failed: %d\n", fpclassify (d)); result = 1; } d = -DBL_MAX; if (fpclassify (d) != FP_NORMAL) { printf ("fpclassify (-DBL_MAX) failed: %d\n", fpclassify (d)); result = 1; } d = nextafter (d, -INFINITY); if (fpclassify (d) != FP_INFINITE) { printf ("fpclassify (-DBL_MAX-epsilon) failed: %d\n", fpclassify (d)); result = 1; } v1.d = d = 0.0625; d = nextafter (d, 0.0); v2.d = d = nextafter (d, 1.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("0.0625 down: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("0.0625 down: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0625 down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("0.0625 down: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.d = d = 0.0625; d = nextafter (d, 1.0); v2.d = d = nextafter (d, 0.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("0.0625 up: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("0.0625 up: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0625 up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("0.0625 up: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.d = d = -0.0625; d = nextafter (d, 0.0); v2.d = d = nextafter (d, -1.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("-0.0625 up: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("-0.0625 up: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-0.0625 up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-0.0625 up: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.d = d = -0.0625; d = nextafter (d, -1.0); v2.d = d = nextafter (d, 0.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("-0.0625 down: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("-0.0625 down: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("-0.0625 down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (v1.ieee.negative != v2.ieee.negative) { printf ("-0.0625 down: negative differs: %d vs %d\n", v1.ieee.negative, v2.ieee.negative); result = 1; } v1.d = d = 0.0; d = nextafter (d, 1.0); v2.d = nextafter (d, -1.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("0.0 up: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("0.0 up: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0 up: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (0 != v2.ieee.negative) { printf ("0.0 up: negative differs: 0 vs %d\n", v2.ieee.negative); result = 1; } v1.d = d = 0.0; d = nextafter (d, -1.0); v2.d = nextafter (d, 1.0); if (v1.ieee.mantissa0 != v2.ieee.mantissa0) { printf ("0.0 down: mantissa0 differs: %8x vs %8x\n", v1.ieee.mantissa0, v2.ieee.mantissa0); result = 1; } if (v1.ieee.mantissa1 != v2.ieee.mantissa1) { printf ("0.0 down: mantissa1 differs: %8x vs %8x\n", v1.ieee.mantissa1, v2.ieee.mantissa1); result = 1; } if (v1.ieee.exponent != v2.ieee.exponent) { printf ("0.0 down: exponent differs: %4x vs %4x\n", v1.ieee.exponent, v2.ieee.exponent); result = 1; } if (1 != v2.ieee.negative) { printf ("0.0 down: negative differs: 1 vs %d\n", v2.ieee.negative); result = 1; } if (nextafter (0.0, INFINITY) != nextafter (0.0, 1.0) || nextafter (-0.0, INFINITY) != nextafter (-0.0, 1.0) || nextafter (0.0, -INFINITY) != nextafter (0.0, -1.0) || nextafter (-0.0, -INFINITY) != nextafter (-0.0, -1.0)) { printf ("nextafter (+-0, +-Inf) != nextafter (+-0, +-1)\n"); result = 1; } if (nexttoward (0.0, INFINITY) != nexttoward (0.0, 1.0) || nexttoward (-0.0, INFINITY) != nexttoward (-0.0, 1.0) || nexttoward (0.0, -INFINITY) != nexttoward (0.0, -1.0) || nexttoward (-0.0, -INFINITY) != nexttoward (-0.0, -1.0)) { printf ("nexttoward (+-0, +-Inf) != nexttoward (+-0, +-1)\n"); result = 1; } } #ifndef NO_LONG_DOUBLE { long double v1, v2; v1 = LDBL_MIN; if (fpclassify (v1) != FP_NORMAL) { printf ("fpclassify (LDBL_MIN) failed: %d (%La)\n", fpclassify (v1), v1); result = 1; } v2 = nextafterl (v1, LDBL_MIN / 2.0); if (fpclassify (v2) != FP_SUBNORMAL) { printf ("fpclassify (LDBL_MIN-epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } v2 = nextafterl (v2, LDBL_MIN); if (fpclassify (v2) != FP_NORMAL) { printf ("fpclassify (LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } if (v1 != v2) { printf ("LDBL_MIN-epsilon+epsilon != LDBL_MIN: %La vs %La\n", v2, v1); result = 1; } v1 = -LDBL_MIN; if (fpclassify (v1) != FP_NORMAL) { printf ("fpclassify (-LDBL_MIN) failed: %d (%La)\n", fpclassify (v1), v1); result = 1; } v2 = nextafterl (v1, -LDBL_MIN / 2.0); if (fpclassify (v2) != FP_SUBNORMAL) { printf ("fpclassify (-LDBL_MIN-epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } v2 = nextafterl (v2, -LDBL_MIN); if (fpclassify (v2) != FP_NORMAL) { printf ("fpclassify (-LDBL_MIN-epsilon+epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } if (v1 != v2) { printf ("-LDBL_MIN-epsilon+epsilon != -LDBL_MIN: %La vs %La\n", v2, v1); result = 1; } v1 = LDBL_MAX; if (fpclassify (v1) != FP_NORMAL) { printf ("fpclassify (LDBL_MAX) failed: %d (%La)\n", fpclassify (v1), v1); result = 1; } v2 = nextafterl (v1, INFINITY); if (fpclassify (v2) != FP_INFINITE) { printf ("fpclassify (LDBL_MAX+epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } v1 = -LDBL_MAX; if (fpclassify (v1) != FP_NORMAL) { printf ("fpclassify (-LDBL_MAX) failed: %d (%La)\n", fpclassify (v1), v1); result = 1; } v2 = nextafterl (v1, -INFINITY); if (fpclassify (v2) != FP_INFINITE) { printf ("fpclassify (-LDBL_MAX-epsilon) failed: %d (%La)\n", fpclassify (v2), v2); result = 1; } v1 = 0.0625; v2 = nextafterl (v1, 0.0); v2 = nextafterl (v2, 1.0); if (v1 != v2) { printf ("0.0625L-epsilon+epsilon != 0.0625L: %La vs %La\n", v2, v1); result = 1; } v1 = 0.0625; v2 = nextafterl (v1, 1.0); v2 = nextafterl (v2, 0.0); if (v1 != v2) { printf ("0.0625L+epsilon-epsilon != 0.0625L: %La vs %La\n", v2, v1); result = 1; } v1 = -0.0625; v2 = nextafterl (v1, 0.0); v2 = nextafterl (v2, -1.0); if (v1 != v2) { printf ("-0.0625L+epsilon-epsilon != -0.0625L: %La vs %La\n", v2, v1); result = 1; } v1 = -0.0625; v2 = nextafterl (v1, -1.0); v2 = nextafterl (v2, 0.0); if (v1 != v2) { printf ("-0.0625L-epsilon+epsilon != -0.0625L: %La vs %La\n", v2, v1); result = 1; } v1 = 0.0; v2 = nextafterl (v1, 1.0); v2 = nextafterl (v2, -1.0); if (v1 != v2) { printf ("0.0+epsilon-epsilon != 0.0L: %La vs %La\n", v2, v1); result = 1; } if (signbit (v2)) { printf ("0.0+epsilon-epsilon is negative\n"); result = 1; } v1 = 0.0; v2 = nextafterl (v1, -1.0); v2 = nextafterl (v2, 1.0); if (v1 != v2) { printf ("0.0-epsilon+epsilon != 0.0L: %La vs %La\n", v2, v1); result = 1; } if (!signbit (v2)) { printf ("0.0-epsilon+epsilon is positive\n"); result = 1; } if (nextafterl (0.0, INFINITY) != nextafterl (0.0, 1.0) || nextafterl (-0.0, INFINITY) != nextafterl (-0.0, 1.0) || nextafterl (0.0, -INFINITY) != nextafterl (0.0, -1.0) || nextafterl (-0.0, -INFINITY) != nextafterl (-0.0, -1.0)) { printf ("nextafterl (+-0, +-Inf) != nextafterl (+-0, +-1)\n"); result = 1; } if (nexttowardl (0.0L, INFINITY) != nexttowardl (0.0L, 1.0L) || nexttowardl (-0.0L, INFINITY) != nexttowardl (-0.0L, 1.0L) || nexttowardl (0.0L, -INFINITY) != nexttowardl (0.0L, -1.0L) || nexttowardl (-0.0L, -INFINITY) != nexttowardl (-0.0L, -1.0L)) { printf ("nexttowardl (+-0, +-Inf) != nexttowardl (+-0, +-1)\n"); result = 1; } } #endif if (! isnormal (FLT_MIN)) { puts ("isnormal (FLT_MIN) failed"); result = 1; } if (! isnormal (DBL_MIN)) { puts ("isnormal (DBL_MIN) failed"); result = 1; } #ifndef NO_LONG_DOUBLE if (! isnormal (LDBL_MIN)) { puts ("isnormal (LDBL_MIN) failed"); result = 1; } #endif #if defined (__i386__) || defined (__x86_64__) /* This is a test for the strange long doubles in x86 FPUs. */ { union { char b[10]; long double d; } u = { .b = { 0, 0, 0, 0, 0, 0, 0, 0x80, 0, 0 } }; if (fpclassify (u.d) != FP_NORMAL) { printf ("fpclassify (0x00008000000000000000) failed: %d (%Lg)\n", fpclassify (u.d), u.d); result = 1; } } /* Special qNaNs in x86 long double. Test for scalbl. */ { union { char b[10]; long double d; } u = { .b = { 0, 1, 0, 0, 0, 0, 0, 0xc0, 0xff, 0x7f } }; long double r; r = scalbl (u.d, 0.0); if (!isnan (r)) { puts ("scalbl (qNaN, 0) does not return NaN"); result = 1; } else if (memcmp (&r, &u.d, sizeof (double)) != 0) { puts ("scalbl (qNaN, 0) does not return the same NaN"); result = 1; } } #endif #ifndef NO_LONG_DOUBLE { long double r; feclearexcept (FE_ALL_EXCEPT); r = scalbl (LDBL_MIN, 2147483647); if (! isinf (r)) { puts ("scalbl (LDBL_MIN, 2147483647) does not return Inf"); result = 1; } else if (signbit (r) != 0) { puts ("scalbl (LDBL_MIN, 2147483647) returns -Inf"); result = 1; } else if (fetestexcept (FE_UNDERFLOW)) { puts ("scalbl (LDBL_MIN, 2147483647) raises underflow exception"); result = 1; } feclearexcept (FE_ALL_EXCEPT); r = scalbl (LDBL_MAX, -2147483647); if (r != 0.0) { puts ("scalbl (LDBL_MAX, -2147483647) does not return 0"); result = 1; } else if (signbit (r) != 0) { puts ("scalbl (LDBL_MAX, -2147483647) returns -Inf"); result = 1; } else if (fetestexcept (FE_OVERFLOW)) { puts ("scalbl (LDBL_MAX, -2147483647) raises overflow exception"); result = 1; } } #endif /* The tests here are very similar to tests earlier in this file, the important difference is just that there are no intervening union variables that cause some GCC versions to hide possible bugs in nextafter* implementation. */ if (nextafterf (nextafterf (FLT_MIN, FLT_MIN / 2.0), FLT_MIN) != FLT_MIN) { puts ("nextafterf FLT_MIN test failed"); result = 1; } if (nextafterf (nextafterf (-FLT_MIN, -FLT_MIN / 2.0), -FLT_MIN) != -FLT_MIN) { puts ("nextafterf -FLT_MIN test failed"); result = 1; } if (nextafter (nextafter (DBL_MIN, DBL_MIN / 2.0), DBL_MIN) != DBL_MIN) { puts ("nextafter DBL_MIN test failed"); result = 1; } if (nextafter (nextafter (-DBL_MIN, -DBL_MIN / 2.0), -DBL_MIN) != -DBL_MIN) { puts ("nextafter -DBL_MIN test failed"); result = 1; } #ifndef NO_LONG_DOUBLE if (nextafterl (nextafterl (LDBL_MIN, LDBL_MIN / 2.0), LDBL_MIN) != LDBL_MIN) { puts ("nextafterl LDBL_MIN test failed"); result = 1; } if (nextafterl (nextafterl (-LDBL_MIN, -LDBL_MIN / 2.0), -LDBL_MIN) != -LDBL_MIN) { puts ("nextafterl -LDBL_MIN test failed"); result = 1; } #endif volatile float f1 = FLT_MAX; volatile float f2 = FLT_MAX / 2; (void) &f1; (void) &f2; feclearexcept (FE_ALL_EXCEPT); f2 += f1; #if defined(FE_OVERFLOW) && defined(FE_INEXACT) int fe = fetestexcept (FE_ALL_EXCEPT); if (EXCEPTION_TESTS (float) && fe != (FE_OVERFLOW | FE_INEXACT)) { printf ("float overflow test failed: %x\n", fe); result = 1; } #endif volatile double d1 = DBL_MAX; volatile double d2 = DBL_MAX / 2; (void) &d1; (void) &d2; feclearexcept (FE_ALL_EXCEPT); d2 += d1; #if defined(FE_OVERFLOW) && defined(FE_INEXACT) fe = fetestexcept (FE_ALL_EXCEPT); if (EXCEPTION_TESTS (double) && fe != (FE_OVERFLOW | FE_INEXACT)) { printf ("double overflow test failed: %x\n", fe); result = 1; } #endif #ifndef NO_LONG_DOUBLE volatile long double ld1 = LDBL_MAX; volatile long double ld2 = LDBL_MAX / 2; (void) &ld1; (void) &ld2; feclearexcept (FE_ALL_EXCEPT); ld2 += ld1; # if defined(FE_OVERFLOW) && defined(FE_INEXACT) fe = fetestexcept (FE_ALL_EXCEPT); if (EXCEPTION_TESTS (long double) && fe != (FE_OVERFLOW | FE_INEXACT)) { printf ("long double overflow test failed: %x\n", fe); result = 1; } # endif #endif #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG == 113 volatile long double ld3 = 0x1.0000000000010000000100000001p+1; volatile long double ld4 = 0x1.0000000000000000000000000001p+1; (void) &ld3; (void) &ld4; ld3 -= ld4; if (ld3 != 0x1.0p-47) { printf ("long double subtraction test failed %.28La\n", ld3); result = 1; } #endif /* Skip testing IBM long double format, for 2 reasons: 1) it only supports FE_TONEAREST 2) nextafter (0.0, 1.0) == nextafterl (0.0L, 1.0L), so nextafter (0.0, 1.0) / 16.0L will be 0.0L. */ #if !defined NO_LONG_DOUBLE && LDBL_MANT_DIG >= DBL_MANT_DIG + 4 \ && LDBL_MANT_DIG != 106 int oldmode = fegetround (); int j; for (j = 0; j < 4; j++) { int mode; int i; int k = 0; const char *mstr; switch (j) { #ifdef FE_TONEAREST case 0: mode = FE_TONEAREST; mstr = "nearest"; k = 8; break; #endif #ifdef FE_DOWNWARD case 1: mode = FE_DOWNWARD; mstr = "-inf"; break; #endif #ifdef FE_UPWARD case 2: mode = FE_UPWARD; mstr = "+inf"; k = 15; break; #endif #ifdef FE_TOWARDZERO case 3: mode = FE_TOWARDZERO; mstr = "0"; break; #endif default: continue; } volatile long double ld5 = nextafter (0.0, 1.0) / 16.0L; volatile double d5; (void) &ld5; for (i = 0; i <= 32; i++) { if (fesetround (mode)) { printf ("failed to set rounding mode to %s\n", mstr); if (ROUNDING_TESTS (long double, mode) && ROUNDING_TESTS (double, mode)) result = 1; else puts ("ignoring this failure"); break; } d5 = ld5 * i; (void) &d5; fesetround (oldmode); if (d5 != ((j == 0 && i == 8) ? 0 : (i + k) / 16) * nextafter (0.0, 1.0)) { printf ("%La incorrectly rounded to %s as %a\n", ld5 * i, mstr, d5); if (ROUNDING_TESTS (long double, mode) && ROUNDING_TESTS (double, mode)) result = 1; else puts ("ignoring this failure"); }