GMP gives wrong (negative) results
V. Nicolai Friedhoff
vnf at gmx.de
Fri Mar 31 13:46:45 UTC 2017
2017-03-31 12:10 GMT+02:00 Torbjörn Granlund <tg at gmplib.org>:
> For small values of x<29 the results seem to be correct, so I am sure
> my maths and my implementation is correct. But for larger x>29 the
> are wrong and even negative (the function should be positive for positive
> 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.
> What is wrong when GMP gives me negative results for a function that
> be positive once some numbers become large?
> That can have many causes, with varying degree of likelihood:
> 1. The user's program is buggy
> 2. There is a bug in GMP
> 3. There is a bug in the compiler used for compiling GMP
> 4. There is a bug in the compiler used for compiling the user code
> 6. There is a bug in some other explicitly or implicitly used library
> 7. There are hardware problems
> 8. This list lacks one or more items
> If I were to debug a float application with apparent stability issues,
> I'd start with plain old 'double' variables.
1. That would be my favorite scenario.
After some memory checking with valgrind I was able to get rid of a handful
of wrongly positioned initializations and clears. Now valgrind does not
find any memory leaks.
But one thing confuses me: To please valgrind and get rid of memory errors,
I have to skip a variable initialization that i would assume necessary,
because the variable is not otherwise initialized.
2.-4. Could I find out if any of this is the case?
6. I only include gmp.h and gmpfrxx.h, nothing else.
7. Its a pretty new workstation, so I doubt that :P
Do you mean regular doubles, without GMP? I tried that and the program
fails much earlier in that case, like at x=5, giving me again large
negative results. The problem I think is that some positive terms in the
sum are too small or large, so they get neglected, while some other
negative terms are valid. This adds up to a negative end result, far from
what the sum should actually be. I would interpret this collective behavior
as a suggestion that my implementation and maths are correct, but somehow
all the platforms get the same issue at largeish numbers (x=5 for c++
doubles, x=30 for GMP, x=120 for Mathematica) with that series.
Thanks for the answer!
2017-03-31 12:33 GMT+02:00 James Wanless <james at grok.ltd.uk>:
> Try a compiler that predates Clang - you might have more luck
Hm, what do you mean? Compile it with Clang (I tried that, same result) or
a compiler that was released before Clang got released?
More information about the gmp-discuss