GMP gives wrong (negative) results

Vincent Lefevre vincent at
Fri Mar 31 15:43:04 UTC 2017

On 2017-03-31 15:46:45 +0200, V. Nicolai Friedhoff wrote:
> 2017-03-31 12:10 GMT+02:00 Torbjörn Granlund <tg at>:
> >   For small values of x<29 the results seem to be correct, so I am sure
> > that
> >   my maths and my implementation is correct. But for larger x>29 the
> > results
> >   are wrong and even negative (the function should be positive for positive
> >   x).
> >
> > Let me rephrase that: your program is correct for some parameters,
> > therefore you know it is correct for all parameters.
> >
> > Skeptical programmers might not find that reasoning convincing.
> >
> Nope, I said that I am sure, not that I know. But I am very sure because I
> get expected results in Wolframalpha and Mathematica for x upto 120
> (compared to 30 in C++ w/ GMP), when it suddenly also goes into the
> negative numbers, even though it is a product of two positive functions. To
> fix this and get proper results for even higher x, one has to apply higher
> precision in Mathematica. But in the end I need that calculation in C++,
> not Mathematica or Wolframalpha.

So, what you probably need is to set the same working precision in
GMP/MPFR as in Wolframalpha and Mathematica (BTW, that's a reason
why I think that a default precision in mpf and MPFR is a bad idea).

Even with that, you may see differences because the internal
arithmetic is not the same.

Vincent Lefèvre <vincent at> - Web: <>
100% accessible validated (X)HTML - Blog: <>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)

More information about the gmp-discuss mailing list