t-md_2exp.c
上传用户:qaz666999
上传日期:2022-08-06
资源大小:2570k
文件大小:6k
源码类别:

数学计算

开发平台:

Unix_Linux

  1. /* Test mpq_mul_2exp and mpq_div_2exp.
  2. Copyright 2000, 2001 Free Software Foundation, Inc.
  3. This file is part of the GNU MP Library.
  4. The GNU MP Library is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or (at your
  7. option) any later version.
  8. The GNU MP Library is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  10. or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  11. License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with the GNU MP Library.  If not, see http://www.gnu.org/licenses/.  */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "gmp.h"
  17. #include "gmp-impl.h"
  18. #include "tests.h"
  19. struct pair_t {
  20.   const char     *num;
  21.   const char     *den;
  22. };
  23. int
  24. main (void)
  25. {
  26.   static const struct {
  27.     struct pair_t  left;
  28.     unsigned long  n;
  29.     struct pair_t  right;
  30.   } data[] = {
  31.     { {"0","1"}, 0, {"0","1"} },
  32.     { {"0","1"}, 1, {"0","1"} },
  33.     { {"0","1"}, 2, {"0","1"} },
  34.     { {"1","1"}, 0, {"1","1"} },
  35.     { {"1","1"}, 1, {"2","1"} },
  36.     { {"1","1"}, 2, {"4","1"} },
  37.     { {"1","1"}, 3, {"8","1"} },
  38.     { {"1","1"}, 31, {"0x80000000","1"} },
  39.     { {"1","1"}, 32, {"0x100000000","1"} },
  40.     { {"1","1"}, 33, {"0x200000000","1"} },
  41.     { {"1","1"}, 63, {"0x8000000000000000","1"} },
  42.     { {"1","1"}, 64, {"0x10000000000000000","1"} },
  43.     { {"1","1"}, 65, {"0x20000000000000000","1"} },
  44.     { {"1","1"}, 95, {"0x800000000000000000000000","1"} },
  45.     { {"1","1"}, 96, {"0x1000000000000000000000000","1"} },
  46.     { {"1","1"}, 97, {"0x2000000000000000000000000","1"} },
  47.     { {"1","1"}, 127, {"0x80000000000000000000000000000000","1"} },
  48.     { {"1","1"}, 128, {"0x100000000000000000000000000000000","1"} },
  49.     { {"1","1"}, 129, {"0x200000000000000000000000000000000","1"} },
  50.     { {"1","2"}, 31, {"0x40000000","1"} },
  51.     { {"1","2"}, 32, {"0x80000000","1"} },
  52.     { {"1","2"}, 33, {"0x100000000","1"} },
  53.     { {"1","2"}, 63, {"0x4000000000000000","1"} },
  54.     { {"1","2"}, 64, {"0x8000000000000000","1"} },
  55.     { {"1","2"}, 65, {"0x10000000000000000","1"} },
  56.     { {"1","2"}, 95, {"0x400000000000000000000000","1"} },
  57.     { {"1","2"}, 96, {"0x800000000000000000000000","1"} },
  58.     { {"1","2"}, 97, {"0x1000000000000000000000000","1"} },
  59.     { {"1","2"}, 127, {"0x40000000000000000000000000000000","1"} },
  60.     { {"1","2"}, 128, {"0x80000000000000000000000000000000","1"} },
  61.     { {"1","2"}, 129, {"0x100000000000000000000000000000000","1"} },
  62.     { {"1","0x80000000"}, 30, {"1","2"} },
  63.     { {"1","0x80000000"}, 31, {"1","1"} },
  64.     { {"1","0x80000000"}, 32, {"2","1"} },
  65.     { {"1","0x80000000"}, 33, {"4","1"} },
  66.     { {"1","0x80000000"}, 62, {"0x80000000","1"} },
  67.     { {"1","0x80000000"}, 63, {"0x100000000","1"} },
  68.     { {"1","0x80000000"}, 64, {"0x200000000","1"} },
  69.     { {"1","0x80000000"}, 94, {"0x8000000000000000","1"} },
  70.     { {"1","0x80000000"}, 95, {"0x10000000000000000","1"} },
  71.     { {"1","0x80000000"}, 96, {"0x20000000000000000","1"} },
  72.     { {"1","0x80000000"}, 126, {"0x800000000000000000000000","1"} },
  73.     { {"1","0x80000000"}, 127, {"0x1000000000000000000000000","1"} },
  74.     { {"1","0x80000000"}, 128, {"0x2000000000000000000000000","1"} },
  75.     { {"1","0x100000000"}, 1, {"1","0x80000000"} },
  76.     { {"1","0x100000000"}, 2, {"1","0x40000000"} },
  77.     { {"1","0x100000000"}, 3, {"1","0x20000000"} },
  78.     { {"1","0x10000000000000000"}, 1, {"1","0x8000000000000000"} },
  79.     { {"1","0x10000000000000000"}, 2, {"1","0x4000000000000000"} },
  80.     { {"1","0x10000000000000000"}, 3, {"1","0x2000000000000000"} },
  81.   };
  82.   void (*fun) __GMP_PROTO ((mpq_ptr, mpq_srcptr, unsigned long));
  83.   const struct pair_t  *p_start, *p_want;
  84.   const char  *name;
  85.   mpq_t    sep, got, want;
  86.   mpq_ptr  q;
  87.   int      i, muldiv, sign, overlap;
  88.   tests_start ();
  89.   mpq_init (sep);
  90.   mpq_init (got);
  91.   mpq_init (want);
  92.   for (i = 0; i < numberof (data); i++)
  93.     {
  94.       for (muldiv = 0; muldiv < 2; muldiv++)
  95.         {
  96.           if (muldiv == 0)
  97.             {
  98.               fun = mpq_mul_2exp;
  99.               name = "mpq_mul_2exp";
  100.               p_start = &data[i].left;
  101.               p_want = &data[i].right;
  102.             }
  103.           else
  104.             {
  105.               fun = mpq_div_2exp;
  106.               name = "mpq_div_2exp";
  107.               p_start = &data[i].right;
  108.               p_want = &data[i].left;
  109.             }
  110.           for (sign = 0; sign <= 1; sign++)
  111.             {
  112.               mpz_set_str_or_abort (mpq_numref(want), p_want->num, 0);
  113.               mpz_set_str_or_abort (mpq_denref(want), p_want->den, 0);
  114.               if (sign)
  115.                 mpq_neg (want, want);
  116.               for (overlap = 0; overlap <= 1; overlap++)
  117.                 {
  118.                   q = overlap ? got : sep;
  119.                   /* initial garbage in "got" */
  120.                   mpq_set_ui (got, 123L, 456L);
  121.                   mpz_set_str_or_abort (mpq_numref(q), p_start->num, 0);
  122.                   mpz_set_str_or_abort (mpq_denref(q), p_start->den, 0);
  123.                   if (sign)
  124.                     mpq_neg (q, q);
  125.                   (*fun) (got, q, data[i].n);
  126.                   MPQ_CHECK_FORMAT (got);
  127.                   if (! mpq_equal (got, want))
  128.                     {
  129.                       printf ("%s wrong at data[%d], sign %d, overlap %dn",
  130.                               name, i, sign, overlap);
  131.                       printf ("   num "%s"n", p_start->num);
  132.                       printf ("   den "%s"n", p_start->den);
  133.                       printf ("   n   %lun", data[i].n);
  134.                       printf ("   got  ");
  135.                       mpq_out_str (stdout, 16, got);
  136.                       printf (" (hex)n");
  137.                       printf ("   want ");
  138.                       mpq_out_str (stdout, 16, want);
  139.                       printf (" (hex)n");
  140.                       abort ();
  141.                     }
  142.                 }
  143.             }
  144.         }
  145.     }
  146.   mpq_clear (sep);
  147.   mpq_clear (got);
  148.   mpq_clear (want);
  149.   tests_end ();
  150.   exit (0);
  151. }