1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
|
/* ieee-utils/test.c
*
* Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <string.h>
#include <gsl/gsl_ieee_utils.h>
#include <gsl/gsl_test.h>
#if HAVE_IRIX_IEEE_INTERFACE
/* don't test denormals on IRIX */
#else
#if HAVE_IEEE_DENORMALS
#define TEST_DENORMAL 1
#endif
#endif
#ifndef FLT_MIN
#define FLT_MIN 1.17549435e-38f
#endif
#ifndef FLT_MAX
#define FLT_MAX 3.40282347e+38f
#endif
#ifndef DBL_MIN
#define DBL_MIN 2.2250738585072014e-308
#endif
#ifndef DBL_MAX
#define DBL_MAX 1.7976931348623157e+308
#endif
int
main (void)
{
float zerof = 0.0f, minus_onef = -1.0f ;
double zero = 0.0, minus_one = -1.0 ;
/* Check for +ZERO (float) */
{
float f = 0.0f;
const char mantissa[] = "00000000000000000000000";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = 0, sign is +");
gsl_test_int (r.exponent, -127, "float x = 0, exponent is -127");
gsl_test_str (r.mantissa, mantissa, "float x = 0, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = 0, type is ZERO");
}
/* Check for -ZERO (float) */
{
float f = minus_onef;
const char mantissa[] = "00000000000000000000000";
gsl_ieee_float_rep r;
while (f < 0) {
f *= 0.1f;
}
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 1, "float x = -1*0, sign is -");
gsl_test_int (r.exponent, -127, "float x = -1*0, exponent is -127");
gsl_test_str (r.mantissa, mantissa, "float x = -1*0, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "float x = -1*0, type is ZERO");
}
/* Check for a positive NORMAL number (e.g. 2.1) (float) */
{
float f = 2.1f;
const char mantissa[] = "00001100110011001100110";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = 2.1, sign is +");
gsl_test_int (r.exponent, 1, "float x = 2.1, exponent is 1");
gsl_test_str (r.mantissa, mantissa, "float x = 2.1, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 2.1, type is NORMAL");
}
/* Check for a negative NORMAL number (e.g. -1.3304...) (float) */
{
float f = -1.3303577090924210f ;
const char mantissa[] = "01010100100100100101001";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 1, "float x = -1.3304..., sign is -");
gsl_test_int (r.exponent, 0, "float x = -1.3304..., exponent is 0");
gsl_test_str (r.mantissa, mantissa, "float x = -1.3304..., mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"float x = -1.3304..., type is NORMAL");
}
/* Check for a large positive NORMAL number (e.g. 3.37e31) (float) */
{
float f = 3.37e31f;
const char mantissa[] = "10101001010110101001001";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = 3.37e31, sign is +");
gsl_test_int (r.exponent, 104, "float x = 3.37e31, exponent is 104");
gsl_test_str (r.mantissa, mantissa, "float x = 3.37e31, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = 3.37e31, type is NORMAL");
}
/* Check for a small positive NORMAL number (e.g. 3.37e-31) (float) */
{
float f = 3.37e-31f;
const char mantissa[] = "10110101011100110111011";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = 3.37e-31, sign is +");
gsl_test_int (r.exponent, -102, "float x = 3.37e-31, exponent is -102");
gsl_test_str (r.mantissa, mantissa, "float x = 3.37e-31, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"float x = 3.37e-31, type is NORMAL");
}
/* Check for FLT_MIN (smallest possible number that is not denormal) */
{
float f = FLT_MIN;
const char mantissa[] = "00000000000000000000000";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = FLT_MIN, sign is +");
gsl_test_int (r.exponent, -126, "float x = FLT_MIN, exponent is -126");
gsl_test_str (r.mantissa, mantissa, "float x = FLT_MIN, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MIN, type is NORMAL");
}
/* Check for FLT_MAX (largest possible number that is not Inf) */
{
float f = FLT_MAX;
const char mantissa[] = "11111111111111111111111";
gsl_ieee_float_rep r;
gsl_ieee_float_to_rep (&f, &r);
gsl_test_int (r.sign, 0, "float x = FLT_MAX, sign is +");
gsl_test_int (r.exponent, 127, "float x = FLT_MAX, exponent is 127");
gsl_test_str (r.mantissa, mantissa, "float x = FLT_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "float x = FLT_MAX, type is NORMAL");
}
/* Check for DENORMAL numbers (e.g. FLT_MIN/2^n) */
#ifdef TEST_DENORMAL
{
float f = FLT_MIN;
char mantissa[] = "10000000000000000000000";
int i;
gsl_ieee_float_rep r;
for (i = 0; i < 23; i++)
{
float x = f / (float)pow (2.0, 1 + (float) i);
mantissa[i] = '1';
gsl_ieee_float_to_rep (&x, &r);
gsl_test_int (r.sign, 0, "float x = FLT_MIN/2^%d, sign is +", i + 1);
gsl_test_int (r.exponent, -127,
"float x = FLT_MIN/2^%d, exponent is -127", i + 1);
gsl_test_str (r.mantissa, mantissa,
"float x = FLT_MIN/2^%d, mantissa", i + 1);
gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
"float x = FLT_MIN/2^%d, type is DENORMAL", i + 1);
mantissa[i] = '0';
}
}
#endif
/* Check for positive INFINITY (e.g. 2*FLT_MAX) */
{
float f = FLT_MAX;
const char mantissa[] = "00000000000000000000000";
gsl_ieee_float_rep r;
float x;
x = 2 * f;
gsl_ieee_float_to_rep (&x, &r);
gsl_test_int (r.sign, 0, "float x = 2*FLT_MAX, sign is +");
gsl_test_int (r.exponent, 128, "float x = 2*FLT_MAX, exponent is 128");
gsl_test_str (r.mantissa, mantissa, "float x = 2*FLT_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
}
/* Check for negative INFINITY (e.g. -2*FLT_MAX) */
{
float f = FLT_MAX;
const char mantissa[] = "00000000000000000000000";
gsl_ieee_float_rep r;
float x;
x = -2 * f;
gsl_ieee_float_to_rep (&x, &r);
gsl_test_int (r.sign, 1, "float x = -2*FLT_MAX, sign is -");
gsl_test_int (r.exponent, 128, "float x = -2*FLT_MAX, exponent is 128");
gsl_test_str (r.mantissa, mantissa, "float x = -2*FLT_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "float x = -2*FLT_MAX, type is INF");
}
/* Check for NAN (e.g. Inf - Inf) (float) */
{
gsl_ieee_float_rep r;
float x = 1.0f, y = 2.0f, z = zerof;
x = x / z;
y = y / z;
z = y - x;
gsl_ieee_float_to_rep (&z, &r);
/* We don't check the sign and we don't check the mantissa because
they could be anything for a NaN */
gsl_test_int (r.exponent, 128, "float x = NaN, exponent is 128");
gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "float x = NaN, type is NAN");
}
/* Check for +ZERO */
{
double d = 0.0;
const char mantissa[]
= "0000000000000000000000000000000000000000000000000000";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = 0, sign is +");
gsl_test_int (r.exponent, -1023, "double x = 0, exponent is -1023");
gsl_test_str (r.mantissa, mantissa, "double x = 0, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = 0, type is ZERO");
}
/* Check for -ZERO */
{
double d = minus_one;
const char mantissa[]
= "0000000000000000000000000000000000000000000000000000";
gsl_ieee_double_rep r;
while (d < 0) {
d *= 0.1;
}
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 1, "double x = -1*0, sign is -");
gsl_test_int (r.exponent, -1023, "double x = -1*0, exponent is -1023");
gsl_test_str (r.mantissa, mantissa, "double x = -1*0, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_ZERO, "double x = -1*0, type is ZERO");
}
/* Check for a positive NORMAL number (e.g. 2.1) */
{
double d = 2.1;
const char mantissa[]
= "0000110011001100110011001100110011001100110011001101";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = 2.1, sign is +");
gsl_test_int (r.exponent, 1, "double x = 2.1, exponent is 1");
gsl_test_str (r.mantissa, mantissa, "double x = 2.1, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL, "double x = 2.1, type is NORMAL");
}
/* Check for a negative NORMAL number (e.g. -1.3304...) */
{
double d = -1.3303577090924210146738460025517269968986511230468750;
const char mantissa[]
= "0101010010010010010100101010010010001000100011101110";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 1, "double x = -1.3304..., sign is -");
gsl_test_int (r.exponent, 0, "double x = -1.3304..., exponent is 0");
gsl_test_str (r.mantissa, mantissa, "double x = -1.3304..., mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"double x = -1.3304..., type is NORMAL");
}
/* Check for a large positive NORMAL number (e.g. 3.37e297) */
{
double d = 3.37e297;
const char mantissa[]
= "0100100111001001100101111001100000100110011101000100";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = 3.37e297, sign is +");
gsl_test_int (r.exponent, 988, "double x = 3.37e297, exponent is 998");
gsl_test_str (r.mantissa, mantissa, "double x = 3.37e297, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"double x = 3.37e297, type is NORMAL");
}
/* Check for a small positive NORMAL number (e.g. 3.37e-297) */
{
double d = 3.37e-297;
const char mantissa[]
= "0001101000011011101011100001110010100001001100110111";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = 3.37e-297, sign is +");
gsl_test_int (r.exponent, -985, "double x = 3.37e-297, exponent is -985");
gsl_test_str (r.mantissa, mantissa, "double x = 3.37e-297, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"double x = 3.37e-297, type is NORMAL");
}
/* Check for DBL_MIN (smallest possible number that is not denormal) */
{
double d = DBL_MIN;
const char mantissa[]
= "0000000000000000000000000000000000000000000000000000";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = DBL_MIN, sign is +");
gsl_test_int (r.exponent, -1022, "double x = DBL_MIN, exponent is -1022");
gsl_test_str (r.mantissa, mantissa, "double x = DBL_MIN, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"double x = DBL_MIN, type is NORMAL");
}
/* Check for DBL_MAX (largest possible number that is not Inf) */
{
double d = DBL_MAX;
const char mantissa[]
= "1111111111111111111111111111111111111111111111111111";
gsl_ieee_double_rep r;
gsl_ieee_double_to_rep (&d, &r);
gsl_test_int (r.sign, 0, "double x = DBL_MAX, sign is +");
gsl_test_int (r.exponent, 1023, "double x = DBL_MAX, exponent is 1023");
gsl_test_str (r.mantissa, mantissa, "double x = DBL_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_NORMAL,
"double x = DBL_MAX, type is NORMAL");
}
/* Check for DENORMAL numbers (e.g. DBL_MIN/2^n) */
#ifdef TEST_DENORMAL
{
double d = DBL_MIN;
char mantissa[]
= "1000000000000000000000000000000000000000000000000000";
int i;
gsl_ieee_double_rep r;
for (i = 0; i < 52; i++)
{
double x = d / pow (2.0, 1 + (double) i);
mantissa[i] = '1';
gsl_ieee_double_to_rep (&x, &r);
gsl_test_int (r.sign, 0, "double x = DBL_MIN/2^%d, sign is +", i + 1);
gsl_test_int (r.exponent, -1023,
"double x = DBL_MIN/2^%d, exponent", i + 1);
gsl_test_str (r.mantissa, mantissa,
"double x = DBL_MIN/2^%d, mantissa", i + 1);
gsl_test_int (r.type, GSL_IEEE_TYPE_DENORMAL,
"double x = DBL_MIN/2^%d, type is DENORMAL", i + 1);
mantissa[i] = '0';
}
}
#endif
/* Check for positive INFINITY (e.g. 2*DBL_MAX) */
{
double d = DBL_MAX;
const char mantissa[]
= "0000000000000000000000000000000000000000000000000000";
gsl_ieee_double_rep r;
double x;
x = 2.0 * d;
gsl_ieee_double_to_rep (&x, &r);
gsl_test_int (r.sign, 0, "double x = 2*DBL_MAX, sign is +");
gsl_test_int (r.exponent, 1024, "double x = 2*DBL_MAX, exponent is 1024");
gsl_test_str (r.mantissa, mantissa, "double x = 2*DBL_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_INF, "double x = 2*DBL_MAX, type is INF");
}
/* Check for negative INFINITY (e.g. -2*DBL_MAX) */
{
double d = DBL_MAX;
const char mantissa[]
= "0000000000000000000000000000000000000000000000000000";
gsl_ieee_double_rep r;
double x;
x = -2.0 * d;
gsl_ieee_double_to_rep (&x, &r);
gsl_test_int (r.sign, 1, "double x = -2*DBL_MAX, sign is -");
gsl_test_int (r.exponent, 1024, "double x = -2*DBL_MAX, exponent is 1024");
gsl_test_str (r.mantissa, mantissa, "double x = -2*DBL_MAX, mantissa");
gsl_test_int (r.type, GSL_IEEE_TYPE_INF,"double x = -2*DBL_MAX, type is INF");
}
/* Check for NAN (e.g. Inf - Inf) */
{
gsl_ieee_double_rep r;
double x = 1.0, y = 2.0, z = zero;
x = x / z;
y = y / z;
z = y - x;
gsl_ieee_double_to_rep (&z, &r);
/* We don't check the sign and we don't check the mantissa because
they could be anything for a NaN */
gsl_test_int (r.exponent, 1024, "double x = NaN, exponent is 1024");
gsl_test_int (r.type, GSL_IEEE_TYPE_NAN, "double x = NaN, type is NAN");
}
exit (gsl_test_summary ());
}
|