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

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

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

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