Unexpected output of a simple GMP program
Marc Glisse
marc.glisse at inria.fr
Sat Jul 27 18:58:02 CEST 2013
On Sat, 27 Jul 2013, Jiri Simsa wrote:
> Hello,
>
> I am trying to use GMP in my project and to familiarize myself with GMP, I
For any new project that requires bigfloats, it is better to use mpfr (it
uses gmp internally).
> wrote some simple programs. For one of them, I am seeing unexpected
> behavior and I am not sure what am I doing wrong.
>
> Here is the example in question:
> #include <gmp.h>
> int main() {
> mpf_t x;
> mpf_init(x);
> mpf_set_si(x, 1);
> gmp_printf("x = %F\n", x);
The examples in the manual use %Ff.
> mpf_set_ui(x, 1);
> gmp_printf("x = %F\n", x);
> mpf_set_d(x, 1.0);
> gmp_printf("x = %F\n", x);
> mpf_clear(x);
> return 0;
> }
>
> I compile this program with:
> g++ -o test test.c -lgmp
>
> When I run it, I see:
> x = 0.000000
> x = 0.000000
> x = 1.000001
>
> While I would expect to see:
> x = 1.000000
> x = 1.000000
> x = 1.000000
>
> Could someone please let me know why the actual output differs from my
> expectation? Thanks.
--
Marc Glisse
More information about the gmp-discuss
mailing list