|
Packit |
67cb25 |
/* ieee-utils/test.c
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
67cb25 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
67cb25 |
* the Free Software Foundation; either version 3 of the License, or (at
|
|
Packit |
67cb25 |
* your option) any later version.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* This program is distributed in the hope that it will be useful, but
|
|
Packit |
67cb25 |
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
67cb25 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit |
67cb25 |
* General Public License for more details.
|
|
Packit |
67cb25 |
*
|
|
Packit |
67cb25 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
67cb25 |
* along with this program; if not, write to the Free Software
|
|
Packit |
67cb25 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
Packit |
67cb25 |
*/
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#include <config.h>
|
|
Packit |
67cb25 |
#include <stdlib.h>
|
|
Packit |
67cb25 |
#include <math.h>
|
|
Packit |
67cb25 |
#include <float.h>
|
|
Packit |
67cb25 |
#include <string.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_ieee_utils.h>
|
|
Packit |
67cb25 |
#include <gsl/gsl_test.h>
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#if HAVE_IRIX_IEEE_INTERFACE
|
|
Packit |
67cb25 |
/* don't test denormals on IRIX */
|
|
Packit |
67cb25 |
#else
|
|
Packit |
67cb25 |
#if HAVE_IEEE_DENORMALS
|
|
Packit |
67cb25 |
#define TEST_DENORMAL 1
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifndef FLT_MIN
|
|
Packit |
67cb25 |
#define FLT_MIN 1.17549435e-38f
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifndef FLT_MAX
|
|
Packit |
67cb25 |
#define FLT_MAX 3.40282347e+38f
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifndef DBL_MIN
|
|
Packit |
67cb25 |
#define DBL_MIN 2.2250738585072014e-308
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifndef DBL_MAX
|
|
Packit |
67cb25 |
#define DBL_MAX 1.7976931348623157e+308
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int
|
|
Packit |
67cb25 |
main (void)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float zerof = 0.0f, minus_onef = -1.0f ;
|
|
Packit |
67cb25 |
double zero = 0.0, minus_one = -1.0 ;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for +ZERO (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = 0.0f;
|
|
Packit |
67cb25 |
const char mantissa[] = "00000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = 0, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -127, "float x = 0, exponent is -127");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = 0, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = 0, type is ZERO");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for -ZERO (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = minus_onef;
|
|
Packit |
67cb25 |
const char mantissa[] = "00000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
while (f < 0) {
|
|
Packit |
67cb25 |
f *= 0.1f;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "float x = -1*0, sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -127, "float x = -1*0, exponent is -127");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = -1*0, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = -1*0, type is ZERO");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a positive NORMAL number (e.g. 2.1) (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = 2.1f;
|
|
Packit |
67cb25 |
const char mantissa[] = "00001100110011001100110";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = 2.1, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1, "float x = 2.1, exponent is 1");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = 2.1, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 2.1, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a negative NORMAL number (e.g. -1.3304...) (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = -1.3303577090924210f ;
|
|
Packit |
67cb25 |
const char mantissa[] = "01010100100100100101001";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "float x = -1.3304..., sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 0, "float x = -1.3304..., exponent is 0");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = -1.3304..., mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"float x = -1.3304..., type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a large positive NORMAL number (e.g. 3.37e31) (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = 3.37e31f;
|
|
Packit |
67cb25 |
const char mantissa[] = "10101001010110101001001";
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = 3.37e31, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 104, "float x = 3.37e31, exponent is 104");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = 3.37e31, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 3.37e31, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a small positive NORMAL number (e.g. 3.37e-31) (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = 3.37e-31f;
|
|
Packit |
67cb25 |
const char mantissa[] = "10110101011100110111011";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = 3.37e-31, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -102, "float x = 3.37e-31, exponent is -102");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = 3.37e-31, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"float x = 3.37e-31, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for FLT_MIN (smallest possible number that is not denormal) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = FLT_MIN;
|
|
Packit |
67cb25 |
const char mantissa[] = "00000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = FLT_MIN, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -126, "float x = FLT_MIN, exponent is -126");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = FLT_MIN, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MIN, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for FLT_MAX (largest possible number that is not Inf) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = FLT_MAX;
|
|
Packit |
67cb25 |
const char mantissa[] = "11111111111111111111111";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&f, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = FLT_MAX, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 127, "float x = FLT_MAX, exponent is 127");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = FLT_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MAX, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for DENORMAL numbers (e.g. FLT_MIN/2^n) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef TEST_DENORMAL
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = FLT_MIN;
|
|
Packit |
67cb25 |
char mantissa[] = "10000000000000000000000";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
int i;
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < 23; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float x = f / (float)pow (2.0, 1 + (float) i);
|
|
Packit |
67cb25 |
mantissa[i] = '1';
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = FLT_MIN/2^%d, sign is +", i + 1);
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -127,
|
|
Packit |
67cb25 |
"float x = FLT_MIN/2^%d, exponent is -127", i + 1);
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa,
|
|
Packit |
67cb25 |
"float x = FLT_MIN/2^%d, mantissa", i + 1);
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
|
|
Packit |
67cb25 |
"float x = FLT_MIN/2^%d, type is DENORMAL", i + 1);
|
|
Packit |
67cb25 |
mantissa[i] = '0';
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for positive INFINITY (e.g. 2*FLT_MAX) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = FLT_MAX;
|
|
Packit |
67cb25 |
const char mantissa[] = "00000000000000000000000";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
float x;
|
|
Packit |
67cb25 |
x = 2 * f;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "float x = 2*FLT_MAX, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 128, "float x = 2*FLT_MAX, exponent is 128");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = 2*FLT_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for negative INFINITY (e.g. -2*FLT_MAX) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
float f = FLT_MAX;
|
|
Packit |
67cb25 |
const char mantissa[] = "00000000000000000000000";
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
float x;
|
|
Packit |
67cb25 |
x = -2 * f;
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "float x = -2*FLT_MAX, sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 128, "float x = -2*FLT_MAX, exponent is 128");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "float x = -2*FLT_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for NAN (e.g. Inf - Inf) (float) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_ieee_float_rep r;
|
|
Packit |
67cb25 |
float x = 1.0f, y = 2.0f, z = zerof;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
x = x / z;
|
|
Packit |
67cb25 |
y = y / z;
|
|
Packit |
67cb25 |
z = y - x;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_float_to_rep (&z, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* We don't check the sign and we don't check the mantissa because
|
|
Packit |
67cb25 |
they could be anything for a NaN */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 128, "float x = NaN, exponent is 128");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "float x = NaN, type is NAN");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for +ZERO */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = 0.0;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = 0, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -1023, "double x = 0, exponent is -1023");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = 0, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = 0, type is ZERO");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for -ZERO */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = minus_one;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
while (d < 0) {
|
|
Packit |
67cb25 |
d *= 0.1;
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "double x = -1*0, sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -1023, "double x = -1*0, exponent is -1023");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = -1*0, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = -1*0, type is ZERO");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a positive NORMAL number (e.g. 2.1) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = 2.1;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000110011001100110011001100110011001100110011001101";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = 2.1, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1, "double x = 2.1, exponent is 1");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = 2.1, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 2.1, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a negative NORMAL number (e.g. -1.3304...) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = -1.3303577090924210146738460025517269968986511230468750;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0101010010010010010100101010010010001000100011101110";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "double x = -1.3304..., sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 0, "double x = -1.3304..., exponent is 0");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = -1.3304..., mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"double x = -1.3304..., type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a large positive NORMAL number (e.g. 3.37e297) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = 3.37e297;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0100100111001001100101111001100000100110011101000100";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = 3.37e297, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 988, "double x = 3.37e297, exponent is 998");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = 3.37e297, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"double x = 3.37e297, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for a small positive NORMAL number (e.g. 3.37e-297) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = 3.37e-297;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0001101000011011101011100001110010100001001100110111";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = 3.37e-297, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -985, "double x = 3.37e-297, exponent is -985");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = 3.37e-297, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"double x = 3.37e-297, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for DBL_MIN (smallest possible number that is not denormal) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = DBL_MIN;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = DBL_MIN, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -1022, "double x = DBL_MIN, exponent is -1022");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = DBL_MIN, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"double x = DBL_MIN, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for DBL_MAX (largest possible number that is not Inf) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = DBL_MAX;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "1111111111111111111111111111111111111111111111111111";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&d, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = DBL_MAX, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1023, "double x = DBL_MAX, exponent is 1023");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = DBL_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
|
|
Packit |
67cb25 |
"double x = DBL_MAX, type is NORMAL");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for DENORMAL numbers (e.g. DBL_MIN/2^n) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
#ifdef TEST_DENORMAL
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = DBL_MIN;
|
|
Packit |
67cb25 |
char mantissa[]
|
|
Packit |
67cb25 |
= "1000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
int i;
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
for (i = 0; i < 52; i++)
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double x = d / pow (2.0, 1 + (double) i);
|
|
Packit |
67cb25 |
mantissa[i] = '1';
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = DBL_MIN/2^%d, sign is +", i + 1);
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, -1023,
|
|
Packit |
67cb25 |
"double x = DBL_MIN/2^%d, exponent", i + 1);
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa,
|
|
Packit |
67cb25 |
"double x = DBL_MIN/2^%d, mantissa", i + 1);
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
|
|
Packit |
67cb25 |
"double x = DBL_MIN/2^%d, type is DENORMAL", i + 1);
|
|
Packit |
67cb25 |
mantissa[i] = '0';
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
#endif
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for positive INFINITY (e.g. 2*DBL_MAX) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = DBL_MAX;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double x;
|
|
Packit |
67cb25 |
x = 2.0 * d;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 0, "double x = 2*DBL_MAX, sign is +");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1024, "double x = 2*DBL_MAX, exponent is 1024");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = 2*DBL_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "double x = 2*DBL_MAX, type is INF");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for negative INFINITY (e.g. -2*DBL_MAX) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
double d = DBL_MAX;
|
|
Packit |
67cb25 |
const char mantissa[]
|
|
Packit |
67cb25 |
= "0000000000000000000000000000000000000000000000000000";
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
double x;
|
|
Packit |
67cb25 |
x = -2.0 * d;
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&x, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.sign, 1, "double x = -2*DBL_MAX, sign is -");
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1024, "double x = -2*DBL_MAX, exponent is 1024");
|
|
Packit |
67cb25 |
gsl_test_str (r.mantissa, mantissa, "double x = -2*DBL_MAX, mantissa");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_INF,"double x = -2*DBL_MAX, type is INF");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* Check for NAN (e.g. Inf - Inf) */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
{
|
|
Packit |
67cb25 |
gsl_ieee_double_rep r;
|
|
Packit |
67cb25 |
double x = 1.0, y = 2.0, z = zero;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
x = x / z;
|
|
Packit |
67cb25 |
y = y / z;
|
|
Packit |
67cb25 |
z = y - x;
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_ieee_double_to_rep (&z, &r);
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
/* We don't check the sign and we don't check the mantissa because
|
|
Packit |
67cb25 |
they could be anything for a NaN */
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
gsl_test_int (r.exponent, 1024, "double x = NaN, exponent is 1024");
|
|
Packit |
67cb25 |
gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "double x = NaN, type is NAN");
|
|
Packit |
67cb25 |
}
|
|
Packit |
67cb25 |
|
|
Packit |
67cb25 |
exit (gsl_test_summary ());
|
|
Packit |
67cb25 |
}
|