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

Annotation of OpenXM_contrib/gmp/mpfr/tests/tfactorial.c, Revision 1.1

1.1     ! ohara       1: /* Test file for mpfr_factorial.
        !             2:
        !             3: Copyright 2001 Free Software Foundation.
        !             4: Adapted from tarctan.c.
        !             5:
        !             6: This file is part of the MPFR Library.
        !             7:
        !             8: The MPFR 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 MPFR 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 MPFR 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: #include "gmp.h"
        !            26: #include "gmp-impl.h"
        !            27: #include "mpfr.h"
        !            28: #include "mpfr-impl.h"
        !            29:
        !            30: #define TEST_FUNCTION mpfr_fac_ui
        !            31:
        !            32: int
        !            33: main (int argc, char *argv[])
        !            34: {
        !            35:   unsigned int prec, err, yprec, n, k, zeros;
        !            36:   mp_rnd_t rnd;
        !            37:   mpfr_t x, y, z, t;
        !            38:   int inexact;
        !            39:
        !            40:   mpfr_init (x);
        !            41:   mpfr_init (y);
        !            42:   mpfr_init (z);
        !            43:   mpfr_init (t);
        !            44:
        !            45:   mpfr_fac_ui (y, 0, GMP_RNDN);
        !            46:
        !            47:   if (mpfr_cmp_ui (y, 1))
        !            48:     {
        !            49:       printf ("mpfr_fac_ui(0) does not give 1\n");
        !            50:       exit (1);
        !            51:     }
        !            52:
        !            53:   for (prec = 2; prec <= 100; prec++)
        !            54:     {
        !            55:       mpfr_set_prec (x, prec);
        !            56:       mpfr_set_prec (z, prec);
        !            57:       mpfr_set_prec (t, prec);
        !            58:       yprec = prec + 10;
        !            59:       mpfr_set_prec (y, yprec);
        !            60:
        !            61:       for (n=0; n<100; n++)
        !            62:        for (rnd=0; rnd<4; rnd++)
        !            63:          {
        !            64:            inexact = mpfr_fac_ui (y, n, rnd);
        !            65:            err = (rnd == GMP_RNDN) ? yprec + 1 : yprec;
        !            66:            if (mpfr_can_round (y, err, rnd, rnd, prec))
        !            67:              {
        !            68:                mpfr_set (t, y, rnd);
        !            69:                inexact = mpfr_fac_ui (z, n, rnd);
        !            70:                /* fact(n) ends with floor(n/2)+floor(n/4)+... zeros */
        !            71:                for (k=n/2, zeros=0; k; k >>= 1)
        !            72:                  zeros += k;
        !            73:                if (MPFR_EXP(y) <= prec + zeros) /* result should be exact */
        !            74:                  {
        !            75:                    if (inexact)
        !            76:                      {
        !            77:                        fprintf (stderr, "Wrong inexact flag: expected exact\n");
        !            78:                        exit (1);
        !            79:                      }
        !            80:                  }
        !            81:                else /* result is inexact */
        !            82:                  {
        !            83:                    if (!inexact)
        !            84:                      {
        !            85:                        fprintf (stderr, "Wrong inexact flag: expected inexact\n");
        !            86:                        printf ("n=%u prec=%u\n", n, prec);
        !            87:                        mpfr_print_binary(z); putchar('\n');
        !            88:                        exit (1);
        !            89:                      }
        !            90:                  }
        !            91:                if (mpfr_cmp (t, z))
        !            92:                  {
        !            93:                    printf ("results differ for x=");
        !            94:                    mpfr_out_str (stdout, 2, prec, x, GMP_RNDN);
        !            95:                    printf (" prec=%u rnd_mode=%s\n", prec,
        !            96:                            mpfr_print_rnd_mode (rnd));
        !            97:                    printf ("   got ");
        !            98:                    mpfr_out_str (stdout, 2, prec, z, GMP_RNDN);
        !            99:                    putchar ('\n');
        !           100:                    printf ("   expected ");
        !           101:                    mpfr_out_str (stdout, 2, prec, t, GMP_RNDN);
        !           102:                    putchar ('\n');
        !           103:                    printf ("   approximation was ");
        !           104:                    mpfr_print_binary (y);
        !           105:                    putchar ('\n');
        !           106:                    exit (1);
        !           107:                  }
        !           108:              }
        !           109:          }
        !           110:     }
        !           111:
        !           112:   mpfr_clear (x);
        !           113:   mpfr_clear (y);
        !           114:   mpfr_clear (z);
        !           115:   mpfr_clear (t);
        !           116:
        !           117:   return 0;
        !           118: }

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