int main () { /* Zero. */ ASSERT (trunc (0.0) == 0.0); ASSERT (trunc (minus_zerod) == 0.0); /* Positive numbers. */ ASSERT (trunc (0.3) == 0.0); ASSERT (trunc (0.7) == 0.0); ASSERT (trunc (1.0) == 1.0); ASSERT (trunc (1.5) == 1.0); ASSERT (trunc (1.999) == 1.0); ASSERT (trunc (2.0) == 2.0); ASSERT (trunc (65535.999) == 65535.0); ASSERT (trunc (65536.0) == 65536.0); ASSERT (trunc (2.341e31) == 2.341e31); /* Negative numbers. */ ASSERT (trunc (-0.3) == 0.0); ASSERT (trunc (-0.7) == 0.0); ASSERT (trunc (-1.0) == -1.0); ASSERT (trunc (-1.5) == -1.0); ASSERT (trunc (-1.999) == -1.0); ASSERT (trunc (-2.0) == -2.0); ASSERT (trunc (-65535.999) == -65535.0); ASSERT (trunc (-65536.0) == -65536.0); ASSERT (trunc (-2.341e31) == -2.341e31); /* Infinite numbers. */ ASSERT (trunc (Infinityd ()) == Infinityd ()); ASSERT (trunc (- Infinityd ()) == - Infinityd ()); /* NaNs. */ ASSERT (isnand (trunc (NaNd ()))); return 0; }
int main () { /* See IEEE 754, section 6.3: "the sign of the result of the round floating-point number to integral value operation is the sign of the operand. These rules shall apply even when operands or results are zero or infinite." */ /* Zero. */ ASSERT (!signbit (trunc (0.0))); ASSERT (!!signbit (trunc (minus_zerod)) == !!signbit (minus_zerod)); /* Positive numbers. */ ASSERT (!signbit (trunc (0.3))); ASSERT (!signbit (trunc (0.7))); /* Negative numbers. */ ASSERT (!!signbit (trunc (-0.3)) == !!signbit (minus_zerod)); ASSERT (!!signbit (trunc (-0.7)) == !!signbit (minus_zerod)); /* [MX] shaded specification in POSIX. */ /* NaN. */ ASSERT (isnand (trunc (NaNd ()))); /* Infinity. */ ASSERT (trunc (Infinityd ()) == Infinityd ()); ASSERT (trunc (- Infinityd ()) == - Infinityd ()); return 0; }
int main () { /* Finite values. */ ASSERT (!isnand (3.141)); ASSERT (!isnand (3.141e30)); ASSERT (!isnand (3.141e-30)); ASSERT (!isnand (-2.718)); ASSERT (!isnand (-2.718e30)); ASSERT (!isnand (-2.718e-30)); ASSERT (!isnand (0.0)); ASSERT (!isnand (-0.0)); /* Infinite values. */ ASSERT (!isnand (1.0 / 0.0)); ASSERT (!isnand (-1.0 / 0.0)); /* Quiet NaN. */ ASSERT (isnand (NaNd ())); #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT /* Signalling NaN. */ { #define NWORDS \ ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)) typedef union { double value; unsigned int word[NWORDS]; } memory_double; memory_double m; m.value = NaNd (); # if DBL_EXPBIT0_BIT > 0 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1); # else m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1); # endif m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDS / 2 ? 1 : - 1)] |= (unsigned int) 1 << DBL_EXPBIT0_BIT; ASSERT (isnand (m.value)); } #endif return 0; }
static void test_double (void) { /* Finite values. */ ASSERT (!isnan (3.141)); ASSERT (!isnan (3.141e30)); ASSERT (!isnan (3.141e-30)); ASSERT (!isnan (-2.718)); ASSERT (!isnan (-2.718e30)); ASSERT (!isnan (-2.718e-30)); ASSERT (!isnan (0.0)); ASSERT (!isnan (minus_zerod)); /* Infinite values. */ ASSERT (!isnan (Infinityd ())); ASSERT (!isnan (- Infinityd ())); /* Quiet NaN. */ ASSERT (isnan (NaNd ())); #if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT /* Signalling NaN. */ { #define NWORDSD \ ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int)) typedef union { double value; unsigned int word[NWORDSD]; } memory_double; memory_double m; m.value = NaNd (); # if DBL_EXPBIT0_BIT > 0 m.word[DBL_EXPBIT0_WORD] ^= (unsigned int) 1 << (DBL_EXPBIT0_BIT - 1); # else m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)] ^= (unsigned int) 1 << (sizeof (unsigned int) * CHAR_BIT - 1); # endif m.word[DBL_EXPBIT0_WORD + (DBL_EXPBIT0_WORD < NWORDSD / 2 ? 1 : - 1)] |= (unsigned int) 1 << DBL_EXPBIT0_BIT; ASSERT (isnan (m.value)); } #endif }
int main () { int i; /* The use of 'volatile' guarantees that excess precision bits are dropped when dealing with denormalized numbers. It is necessary on x86 systems where double-floats are not IEEE compliant by default, to avoid that the results become platform and compiler option dependent. 'volatile' is a portable alternative to gcc's -ffloat-store option. */ volatile double x; { /* NaN. */ int exp = -9999; double mantissa; x = NaNd (); mantissa = frexp (x, &exp); ASSERT (isnand (mantissa)); } { /* Positive infinity. */ int exp = -9999; double mantissa; x = 1.0 / 0.0; mantissa = frexp (x, &exp); ASSERT (mantissa == x); } { /* Negative infinity. */ int exp = -9999; double mantissa; x = -1.0 / 0.0; mantissa = frexp (x, &exp); ASSERT (mantissa == x); } { /* Positive zero. */ int exp = -9999; double mantissa; x = 0.0; mantissa = frexp (x, &exp); ASSERT (exp == 0); ASSERT (mantissa == x); ASSERT (!signbit (mantissa)); } { /* Negative zero. */ int exp = -9999; double mantissa; x = -zero; mantissa = frexp (x, &exp); ASSERT (exp == 0); ASSERT (mantissa == x); ASSERT (signbit (mantissa)); } for (i = 1, x = 1.0; i <= DBL_MAX_EXP; i++, x *= 2.0) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.5); } for (i = 1, x = 1.0; i >= DBL_MIN_EXP; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.5); } for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.5); } for (i = 1, x = -1.0; i <= DBL_MAX_EXP; i++, x *= 2.0) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == -0.5); } for (i = 1, x = -1.0; i >= DBL_MIN_EXP; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == -0.5); } for (; i >= DBL_MIN_EXP - 100 && x < 0.0; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == -0.5); } for (i = 1, x = 1.01; i <= DBL_MAX_EXP; i++, x *= 2.0) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.505); } for (i = 1, x = 1.01; i >= DBL_MIN_EXP; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.505); } for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa >= 0.5); ASSERT (mantissa < 1.0); ASSERT (mantissa == my_ldexp (x, - exp)); } for (i = 1, x = 1.73205; i <= DBL_MAX_EXP; i++, x *= 2.0) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.866025); } for (i = 1, x = 1.73205; i >= DBL_MIN_EXP; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i); ASSERT (mantissa == 0.866025); } for (; i >= DBL_MIN_EXP - 100 && x > 0.0; i--, x *= 0.5) { int exp = -9999; double mantissa = frexp (x, &exp); ASSERT (exp == i || exp == i + 1); ASSERT (mantissa >= 0.5); ASSERT (mantissa < 1.0); ASSERT (mantissa == my_ldexp (x, - exp)); } return 0; }