gmp_printf() and mpf_set_d() question
Lars Callenbach
lars.callenbach at gmx.de
Sun Nov 2 20:35:34 UTC 2014
Hello,
I do not understand the behaviour of gmp_printf() and mpf_set_d().
Sample code:
------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gmp.h>
int main(int argc, char **argv) {
double real=2.1111;
int i;
mpf_set_default_prec(80);
mpf_t mpf;
mpf_init2(mpf, 80);
mpf_set_d(mpf, real);
__mpf_struct mpfs[4];;
mpf_ptr mpfptr;
printf("Lim-Size %d Exp-Size %d\n", sizeof(mp_limb_t),
sizeof(mp_exp_t));
for(i=0; i<4; i++) {
mpf_init2(mpfs+i, 80);
printf("%d -- 1 %d %d %ld %p \n", i, mpfs[i]._mp_prec,
mpfs[i]._mp_size, mpfs[i]._mp_exp, mpfs[i]._mp_d);
}
for(i=0; i<4; i++) {
double r= 1.*sqrt(i+2) + 2.1111;
printf("%d -- 2 %d %d %ld %p \n", i, mpfs[i]._mp_prec,
mpfs[i]._mp_size, mpfs[i]._mp_exp, mpfs[i]._mp_d);
mpfptr = &(mpfs[i]);
mpf_set_d(mpfptr, r);
gmp_printf("%1.10e\n", *mpfptr);
printf("%d -- 3 %d %d %ld %p \n", i, mpfs[i]._mp_prec,
mpfs[i]._mp_size, mpfs[i]._mp_exp, mpfs[i]._mp_d);
}
return 1;
}
------------------------------------------
mpf_set_d() in the inner loop does not seem to work. The output is:
Lim-Size 8 Exp-Size 8
0 -- 1 3 0 0 0xae8040
1 -- 1 3 0 0 0xae8070
2 -- 1 3 0 0 0xae80a0
3 -- 1 3 0 0 0xae80d0
0 -- 2 3 0 0 0xae8040
6.9043377051e-310
0 -- 3 3 2 1 0xae8040
1 -- 2 3 0 0 0xae8070
6.9043377051e-310
1 -- 3 3 2 1 0xae8070
2 -- 2 3 0 0 0xae80a0
6.9043377051e-310
2 -- 3 3 2 1 0xae80a0
3 -- 2 3 0 0 0xae80d0
6.9043377051e-310
3 -- 3 3 2 1 0xae80d0
If I change gmp_printf("%1.10e\n", *mpfptr) to gmp_printf("%1.10e %.100f
\n", *mpfptr,r) I get another output result (with initialized
variables?).
Lim-Size 8 Exp-Size 8
0 -- 1 3 0 0 0x1701040
1 -- 1 3 0 0 0x1701070
2 -- 1 3 0 0 0x17010a0
3 -- 1 3 0 0 0x17010d0
0 -- 2 3 0 0 0x1701040
3.5253135624e+00
3.5253135623730953440713165036868304014205932617187500000000000000000000000000000000000000000000000000
0 -- 3 3 2 1 0x1701040
1 -- 2 3 0 0 0x1701070
3.8431508076e+00
3.8431508075688771697286938433535397052764892578125000000000000000000000000000000000000000000000000000
1 -- 3 3 2 1 0x1701070
2 -- 2 3 0 0 0x17010a0
4.1111000000e+00
4.1111000000000004206412995699793100357055664062500000000000000000000000000000000000000000000000000000
2 -- 3 3 2 1 0x17010a0
3 -- 2 3 0 0 0x17010d0
4.3471679775e+00
4.3471679774997902256927773123607039451599121093750000000000000000000000000000000000000000000000000000
3 -- 3 3 2 1 0x17010d0
How can I use arrays like "__mpf_struct mpfs[4];" in the code? Does
anyone expect this behaviour of GMP?
Best regards,
Lars
More information about the gmp-discuss
mailing list