[BACK]Return to t-get_d.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib / gmp / tests / mpq

Annotation of OpenXM_contrib/gmp/tests/mpq/t-get_d.c, Revision 1.1

1.1     ! ohara       1: /* Test mpq_get_d and mpq_set_d
        !             2:
        !             3: Copyright 1991, 1993, 1994, 1996, 2000, 2001, 2002 Free Software Foundation,
        !             4: Inc.
        !             5:
        !             6: This file is part of the GNU MP Library.
        !             7:
        !             8: The GNU MP Library is free software; you can redistribute it and/or modify
        !             9: it under the terms of the GNU Lesser General Public License as published by
        !            10: the Free Software Foundation; either version 2.1 of the License, or (at your
        !            11: option) any later version.
        !            12:
        !            13: The GNU MP Library is distributed in the hope that it will be useful, but
        !            14: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            15: or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
        !            16: License for more details.
        !            17:
        !            18: You should have received a copy of the GNU Lesser General Public License
        !            19: along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
        !            20: the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
        !            21: MA 02111-1307, USA. */
        !            22:
        !            23: #include <stdio.h>
        !            24: #include <stdlib.h>
        !            25:
        !            26: #include "gmp.h"
        !            27: #include "gmp-impl.h"
        !            28: #include "tests.h"
        !            29:
        !            30: #ifndef SIZE
        !            31: #define SIZE 8
        !            32: #endif
        !            33:
        !            34: /* VAX D floats only have an 8 bit signed exponent, so anything 2^128 or
        !            35:    bigger will overflow, that being 4 limbs. */
        !            36: #if defined (__vax__) && SIZE > 4
        !            37: #undef SIZE
        !            38: #define SIZE 4
        !            39: #define EPSIZE 3
        !            40: #else
        !            41: #define EPSIZE SIZE
        !            42: #endif
        !            43:
        !            44: void dump _PROTO ((mpq_t));
        !            45:
        !            46: int
        !            47: main (int argc, char **argv)
        !            48: {
        !            49:   mpq_t a;
        !            50:   mp_size_t size;
        !            51:   int reps = 1000;
        !            52:   int i, j;
        !            53:   double last_d, new_d;
        !            54:   mpq_t qlast_d, qnew_d;
        !            55:   mpq_t eps;
        !            56:
        !            57:   tests_start ();
        !            58:
        !            59:   if (argc == 2)
        !            60:      reps = atoi (argv[1]);
        !            61:
        !            62:   /* The idea here is to test the monotonousness of mpq_get_d by adding
        !            63:      numbers to the numerator and denominator.  */
        !            64:
        !            65:   mpq_init (a);
        !            66:   mpq_init (eps);
        !            67:   mpq_init (qlast_d);
        !            68:   mpq_init (qnew_d);
        !            69:
        !            70:   for (i = 0; i < reps; i++)
        !            71:     {
        !            72:       size = urandom () % SIZE - SIZE/2;
        !            73:       mpz_random2 (mpq_numref (a), size);
        !            74:       do
        !            75:        {
        !            76:          size = urandom () % SIZE - SIZE/2;
        !            77:          mpz_random2 (mpq_denref (a), size);
        !            78:        }
        !            79:       while (mpz_cmp_ui (mpq_denref (a), 0) == 0);
        !            80:
        !            81:       mpq_canonicalize (a);
        !            82:
        !            83:       last_d = mpq_get_d (a);
        !            84:       mpq_set_d (qlast_d, last_d);
        !            85:       for (j = 0; j < 10; j++)
        !            86:        {
        !            87:          size = urandom () % EPSIZE + 1;
        !            88:          mpz_random2 (mpq_numref (eps), size);
        !            89:          size = urandom () % EPSIZE + 1;
        !            90:          mpz_random2 (mpq_denref (eps), size);
        !            91:          mpq_canonicalize (eps);
        !            92:
        !            93:          mpq_add (a, a, eps);
        !            94:          mpq_canonicalize (a);
        !            95:          new_d = mpq_get_d (a);
        !            96:          if (last_d > new_d)
        !            97:            {
        !            98:              fprintf (stderr, "ERROR (test %d/%d): bad mpq_get_d results\n", i, j);
        !            99:              printf ("\nlast: %.16g\n      ", last_d);
        !           100:              printf (" new: %.16g\n      ", new_d); dump (a);
        !           101:              abort ();
        !           102:            }
        !           103:          mpq_set_d (qnew_d, new_d);
        !           104:          MPQ_CHECK_FORMAT (qnew_d);
        !           105:          if (mpq_cmp (qlast_d, qnew_d) > 0)
        !           106:            {
        !           107:              fprintf (stderr,
        !           108:                       "ERROR (test %d/%d): bad mpq_set_d results\n", i, j);
        !           109:              printf ("\nlast: %.16g\n      ", last_d); dump (qlast_d);
        !           110:              printf (" new: %.16g\n      ", new_d); dump (qnew_d);
        !           111:              abort ();
        !           112:            }
        !           113:          last_d = new_d;
        !           114:          mpq_set (qlast_d, qnew_d);
        !           115:        }
        !           116:     }
        !           117:
        !           118:   mpq_clear (a);
        !           119:   mpq_clear (eps);
        !           120:   mpq_clear (qlast_d);
        !           121:   mpq_clear (qnew_d);
        !           122:
        !           123:   tests_end ();
        !           124:   exit (0);
        !           125: }
        !           126:
        !           127: void
        !           128: dump (mpq_t x)
        !           129: {
        !           130:   mpz_out_str (stdout, 10, mpq_numref (x));
        !           131:   printf ("/");
        !           132:   mpz_out_str (stdout, 10, mpq_denref (x));
        !           133:   printf ("\n");
        !           134: }

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