0

我正在寻找一些说明mpfr图书馆的例子,但我找不到任何有用的东西。

我想创建两个最多可以容纳 100 位浮点数的变量。

这些变量必须使用字符串进行初始化。

然后我想添加它们并将结果打印到屏幕上。

我找到了这段代码:

#include <stdio.h>

#include <gmp.h>
#include <mpfr.h>

int main(void)
{
   unsigned int i;
   mpfr_t s, t, u;

   mpfr_init2(t, 200);
   mpfr_set_d(t, 1.0, GMP_RNDD);
   mpfr_init2(s, 200);
   mpfr_set_d(s, 1.0, GMP_RNDD);
   mpfr_init2(u, 200);
   for (i = 1; i <= 100; i++)
   {
       mpfr_mul_ui(t, t, i, GMP_RNDU);
       mpfr_set_d(u, 1.0, GMP_RNDD);
       mpfr_div(u, u, t, GMP_RNDD);
       mpfr_add(s, s, u, GMP_RNDD);
   }
   printf("Sum is ");
   mpfr_out_str(stdout, 10, 0, s, GMP_RNDD);
   putchar('\n');
   mpfr_clear(s);
   mpfr_clear(t);
   mpfr_clear(u);
   return 0;
 }

然后我尝试了以下代码:

  #include <stdio.h>

  #include <gmp.h>
  #include <mpfr.h>

  int main(void)
  {
  unsigned int i;
  mpfr_t s, t, u;
  //add numbers 1.456774343454354355435354325213 and 
  //14.456774343454354355435354325213
  mpfr_init2(t, 200);
  mpfr_set_d(t, 1.456774343454354355435354325213, GMP_RNDD);
  mpfr_init2(s, 200);
  mpfr_set_d(s, 14.456774343454354355435354325213, GMP_RNDD);
  mpfr_init2(u, 200);
  mpfr_add(u, s, t, GMP_RNDD);
  printf("Sum is ");
  mpfr_out_str(stdout, 10, 0, u, GMP_RNDD);
  putchar('\n');
  mpfr_clear(s);
  mpfr_clear(t);
  mpfr_clear(u);
  return 0;
 }

结果

Sum is 1.5913548686908708384990518425183836370706558227539062500000000e1

这是错误的,它应该至少是 2

4

0 回答 0