ライブラリを説明する例を探していmpfr
ますが、役立つものが見つかりません。
最大 100 桁の浮動小数点数を保持できる 2 つの変数を作成したいと考えています。
これらの変数は、文字列を使用して初期化する必要があります。
次に、それらを追加して結果を画面に出力したいと思います。
私はこのコードを見つけました:
#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 である必要があります。