[BACK]Return to texceptions.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / mpfr / tests

Annotation of OpenXM_contrib/gmp/mpfr/tests/texceptions.c, Revision 1.1.1.1

1.1       ohara       1: /* Test file for exceptions.
                      2:
                      3: Copyright 2001 Free Software Foundation.
                      4:
                      5: This file is part of the MPFR Library.
                      6:
                      7: The MPFR Library is free software; you can redistribute it and/or modify
                      8: it under the terms of the GNU Lesser General Public License as published by
                      9: the Free Software Foundation; either version 2.1 of the License, or (at your
                     10: option) any later version.
                     11:
                     12: The MPFR Library is distributed in the hope that it will be useful, but
                     13: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
                     14: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
                     15: License for more details.
                     16:
                     17: You should have received a copy of the GNU Lesser General Public License
                     18: along with the MPFR Library; see the file COPYING.LIB.  If not, write to
                     19: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
                     20: MA 02111-1307, USA. */
                     21:
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include "gmp.h"
                     25: #include "mpfr.h"
                     26:
                     27: void mpfr_set_double_range _PROTO((void));
                     28:
                     29: void
                     30: mpfr_set_double_range (void)
                     31: {
                     32:   mpfr_set_default_prec (53);
                     33:
                     34:   /* in double precision format, the unbiased exponent is between 0 and
                     35:      2047, where 0 is used for subnormal numbers, and 2047 for special
                     36:      numbers (infinities, NaN), and the bias is 1023, thus "normal" numbers
                     37:      have an exponent between -1022 and 1023, corresponding to numbers
                     38:      between 2^(-1022) and previous(2^(1024)).
                     39:      (The smallest subnormal number is 0.(0^51)1*2^(-1022)= 2^(-1074).)
                     40:
                     41:      The smallest normal power of two is 1.0*2^(-1022).
                     42:      The largest normal power of two is 2^1023.
                     43:      (We have to add one for mpfr since mantissa are between 1/2 and 1.)
                     44:   */
                     45:
                     46:   mpfr_set_emin (-1021);
                     47:   mpfr_set_emax (1024);
                     48: }
                     49:
                     50: int
                     51: main (int argc, char *argv[])
                     52: {
                     53:   mpfr_t x, y;
                     54:   mp_exp_t emin, emax;
                     55:
                     56:   mpfr_init (x);
                     57:   mpfr_init (y);
                     58:
                     59:   emin = mpfr_get_emin ();
                     60:   emax = mpfr_get_emax ();
                     61:   if (emin >= emax)
                     62:     {
                     63:       fprintf (stderr, "Error: emin >= emax\n");
                     64:       exit (1);
                     65:     }
                     66:
                     67:   mpfr_set_ui (x, 1, GMP_RNDN);
                     68:   mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
                     69:   mpfr_set_double_range ();
                     70:   mpfr_check_range (x, GMP_RNDN);
                     71:   if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0))
                     72:     {
                     73:       fprintf (stderr, "Error: 2^1024 rounded to nearest should give +Inf\n");
                     74:       exit (1);
                     75:     }
                     76:
                     77:   mpfr_set_emax (1025);
                     78:   mpfr_set_ui (x, 1, GMP_RNDN);
                     79:   mpfr_mul_2exp (x, x, 1024, GMP_RNDN);
                     80:   mpfr_set_double_range ();
                     81:   mpfr_check_range (x, GMP_RNDD);
                     82:   if (!mpfr_number_p (x))
                     83:     {
                     84:       fprintf (stderr, "Error: 2^1024 rounded down should give a normal number\n");
                     85:       exit (1);
                     86:     }
                     87:
                     88:   mpfr_set_ui (x, 1, GMP_RNDN);
                     89:   mpfr_mul_2exp (x, x, 1023, GMP_RNDN);
                     90:   mpfr_add (x, x, x, GMP_RNDN);
                     91:   if (!mpfr_inf_p (x) || (mpfr_sgn(x) <= 0))
                     92:     {
                     93:       fprintf (stderr, "Error: x+x rounded to nearest for x=2^1023 should give +Inf\n");
                     94:       printf ("emax = %ld\n", mpfr_get_emax ());
                     95:       printf ("got "); mpfr_print_binary (x); putchar ('\n');
                     96:       exit (1);
                     97:     }
                     98:
                     99:   mpfr_set_ui (x, 1, GMP_RNDN);
                    100:   mpfr_mul_2exp (x, x, 1023, GMP_RNDN);
                    101:   mpfr_add (x, x, x, GMP_RNDD);
                    102:   if (!mpfr_number_p (x))
                    103:     {
                    104:       fprintf (stderr, "Error: x+x rounded down for x=2^1023 should give a normal number\n");
                    105:       exit (1);
                    106:     }
                    107:
                    108:   mpfr_set_ui (x, 1, GMP_RNDN);
                    109:   mpfr_div_2exp (x, x, 1022, GMP_RNDN);
                    110:   mpfr_set_str_raw (y, "1.1e-1022"); /* y = 3/2*x */
                    111:   mpfr_sub (y, y, x, GMP_RNDZ);
                    112:   if (mpfr_cmp_ui (y, 0))
                    113:     {
                    114:       fprintf (stderr, "Error: y-x rounded to zero should give 0 for y=3/2*2^(-1022), x=2^(-1022)\n");
                    115:       printf ("y="); mpfr_print_binary (y); putchar ('\n');
                    116:       exit (1);
                    117:     }
                    118:
                    119:   mpfr_clear (x);
                    120:   mpfr_clear (y);
                    121:
                    122:   return 0;
                    123: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>