コード例 #1
0
ファイル: test-trunc-ieee.c プロジェクト: ajnelson/gnulib
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;
}
コード例 #2
0
ファイル: test-trunc1.c プロジェクト: ajnelson/gnulib
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;
}
コード例 #3
0
ファイル: test-signbit.c プロジェクト: ajnelson/gnulib
static void
test_signbitd ()
{
  /* Finite values.  */
  ASSERT (!signbit (3.141));
  ASSERT (!signbit (3.141e30));
  ASSERT (!signbit (3.141e-30));
  ASSERT (signbit (-2.718));
  ASSERT (signbit (-2.718e30));
  ASSERT (signbit (-2.718e-30));
  /* Zeros.  */
  ASSERT (!signbit (0.0));
  if (1.0 / minus_zerod < 0)
    ASSERT (signbit (minus_zerod));
  else
    ASSERT (!signbit (minus_zerod));
  /* Infinite values.  */
  ASSERT (!signbit (Infinityd ()));
  ASSERT (signbit (- Infinityd ()));
  /* Quiet NaN.  */
  (void) signbit (zerod / zerod);
#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 = zerod / zerod;
# 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;
    (void) signbit (m.value);
    #undef NWORDS
  }
#endif
}
コード例 #4
0
ファイル: test-isnan.c プロジェクト: ajnelson/gnulib
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
}
コード例 #5
0
  ASSERT (floor (minus_zerod) == 0.0);
  /* Positive numbers.  */
  ASSERT (floor (0.3) == 0.0);
  ASSERT (floor (0.7) == 0.0);
  ASSERT (floor (1.0) == 1.0);
  ASSERT (floor (1.5) == 1.0);
  ASSERT (floor (1.999) == 1.0);
  ASSERT (floor (2.0) == 2.0);
  ASSERT (floor (65535.999) == 65535.0);
  ASSERT (floor (65536.0) == 65536.0);
  ASSERT (floor (2.341e31) == 2.341e31);
  /* Negative numbers.  */
  ASSERT (floor (-0.3) == -1.0);
  ASSERT (floor (-0.7) == -1.0);
  ASSERT (floor (-1.0) == -1.0);
  ASSERT (floor (-1.001) == -2.0);
  ASSERT (floor (-1.5) == -2.0);
  ASSERT (floor (-1.999) == -2.0);
  ASSERT (floor (-2.0) == -2.0);
  ASSERT (floor (-65535.999) == -65536.0);
  ASSERT (floor (-65536.0) == -65536.0);
  ASSERT (floor (-2.341e31) == -2.341e31);
  /* Infinite numbers.  */
  ASSERT (floor (Infinityd ()) == Infinityd ());
  ASSERT (floor (- Infinityd ()) == - Infinityd ());
  /* NaNs.  */
  ASSERT (isnand (floor (NaNd ())));

  return 0;
}
コード例 #6
0
static void
test_function (int (*my_dprintf) (int, const char *, ...))
{
  /* Here we don't test output that may be platform dependent.
     The bulk of the tests is done as part of the 'vasnprintf-posix' module.  */

  /* Test support of size specifiers as in C99.  */

  my_dprintf (fileno (stdout), "%ju %d\n", (uintmax_t) 12345671, 33, 44, 55);

  my_dprintf (fileno (stdout), "%zu %d\n", (size_t) 12345672, 33, 44, 55);

  my_dprintf (fileno (stdout), "%tu %d\n", (ptrdiff_t) 12345673, 33, 44, 55);

  /* Test the support of the 'a' and 'A' conversion specifier for hexadecimal
     output of floating-point numbers.  */

  /* Positive zero.  */
  my_dprintf (fileno (stdout), "%a %d\n", 0.0, 33, 44, 55);

  /* Positive infinity.  */
  my_dprintf (fileno (stdout), "%a %d\n", Infinityd (), 33, 44, 55);

  /* Negative infinity.  */
  my_dprintf (fileno (stdout), "%a %d\n", - Infinityd (), 33, 44, 55);

  /* FLAG_ZERO with infinite number.  */
  my_dprintf (fileno (stdout), "%010a %d\n", Infinityd (), 33, 44, 55);

  /* Test the support of the %f format directive.  */

  /* A positive number.  */
  my_dprintf (fileno (stdout), "%f %d\n", 12.75, 33, 44, 55);

  /* A larger positive number.  */
  my_dprintf (fileno (stdout), "%f %d\n", 1234567.0, 33, 44, 55);

  /* A negative number.  */
  my_dprintf (fileno (stdout), "%f %d\n", -0.03125, 33, 44, 55);

  /* Positive zero.  */
  my_dprintf (fileno (stdout), "%f %d\n", 0.0, 33, 44, 55);

  /* FLAG_ZERO.  */
  my_dprintf (fileno (stdout), "%015f %d\n", 1234.0, 33, 44, 55);

  /* Precision.  */
  my_dprintf (fileno (stdout), "%.f %d\n", 1234.0, 33, 44, 55);

  /* Precision with no rounding.  */
  my_dprintf (fileno (stdout), "%.2f %d\n", 999.95, 33, 44, 55);

  /* Precision with rounding.  */
  my_dprintf (fileno (stdout), "%.2f %d\n", 999.996, 33, 44, 55);

  /* A positive number.  */
  my_dprintf (fileno (stdout), "%Lf %d\n", 12.75L, 33, 44, 55);

  /* A larger positive number.  */
  my_dprintf (fileno (stdout), "%Lf %d\n", 1234567.0L, 33, 44, 55);

  /* A negative number.  */
  my_dprintf (fileno (stdout), "%Lf %d\n", -0.03125L, 33, 44, 55);

  /* Positive zero.  */
  my_dprintf (fileno (stdout), "%Lf %d\n", 0.0L, 33, 44, 55);

  /* FLAG_ZERO.  */
  my_dprintf (fileno (stdout), "%015Lf %d\n", 1234.0L, 33, 44, 55);

  /* Precision.  */
  my_dprintf (fileno (stdout), "%.Lf %d\n", 1234.0L, 33, 44, 55);

  /* Precision with no rounding.  */
  my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.95L, 33, 44, 55);

  /* Precision with rounding.  */
  my_dprintf (fileno (stdout), "%.2Lf %d\n", 999.996L, 33, 44, 55);

  /* Test the support of the %F format directive.  */

  /* A positive number.  */
  my_dprintf (fileno (stdout), "%F %d\n", 12.75, 33, 44, 55);

  /* A larger positive number.  */
  my_dprintf (fileno (stdout), "%F %d\n", 1234567.0, 33, 44, 55);

  /* A negative number.  */
  my_dprintf (fileno (stdout), "%F %d\n", -0.03125, 33, 44, 55);

  /* Positive zero.  */
  my_dprintf (fileno (stdout), "%F %d\n", 0.0, 33, 44, 55);

  /* FLAG_ZERO.  */
  my_dprintf (fileno (stdout), "%015F %d\n", 1234.0, 33, 44, 55);

  /* Precision.  */
  my_dprintf (fileno (stdout), "%.F %d\n", 1234.0, 33, 44, 55);

  /* Precision with no rounding.  */
  my_dprintf (fileno (stdout), "%.2F %d\n", 999.95, 33, 44, 55);

  /* Precision with rounding.  */
  my_dprintf (fileno (stdout), "%.2F %d\n", 999.996, 33, 44, 55);

  /* A positive number.  */
  my_dprintf (fileno (stdout), "%LF %d\n", 12.75L, 33, 44, 55);

  /* A larger positive number.  */
  my_dprintf (fileno (stdout), "%LF %d\n", 1234567.0L, 33, 44, 55);

  /* A negative number.  */
  my_dprintf (fileno (stdout), "%LF %d\n", -0.03125L, 33, 44, 55);

  /* Positive zero.  */
  my_dprintf (fileno (stdout), "%LF %d\n", 0.0L, 33, 44, 55);

  /* FLAG_ZERO.  */
  my_dprintf (fileno (stdout), "%015LF %d\n", 1234.0L, 33, 44, 55);

  /* Precision.  */
  my_dprintf (fileno (stdout), "%.LF %d\n", 1234.0L, 33, 44, 55);

  /* Precision with no rounding.  */
  my_dprintf (fileno (stdout), "%.2LF %d\n", 999.95L, 33, 44, 55);

  /* Precision with rounding.  */
  my_dprintf (fileno (stdout), "%.2LF %d\n", 999.996L, 33, 44, 55);

  /* Test the support of the POSIX/XSI format strings with positions.  */

  my_dprintf (fileno (stdout), "%2$d %1$d\n", 33, 55);
}
コード例 #7
0
ファイル: test-frexp.c プロジェクト: DesmondWu/gnulib
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 = Infinityd ();
    mantissa = frexp (x, &exp);
    ASSERT (mantissa == x);
  }

  { /* Negative infinity.  */
    int exp = -9999;
    double mantissa;
    x = - Infinityd ();
    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 = minus_zerod;
    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;
}