Code for Chudnovsky Algorithm [or How to Translate an Equation into C++ Code]

Pages: 12
Mar 5, 2011 at 3:31am
*shrugs*

Well, I've tacked .0 onto the ends of all of the integers in the line, and I'm now down to one error:
[path to project]/calculatePi.cpp:38: error: call of overloaded 'pow(double, long int&)' is ambiguous
/Developer/SDKs/MacOSX10.6.sdk/usr/include/architecture/i386/math.h:343: note: candidates are: double pow(double, double)
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/cmath:373: note:                 long double std::pow(long double, int)
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/cmath:369: note:                 float std::pow(float, int)
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/cmath:365: note:                 double std::pow(double, int)
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/cmath:361: note:                 long double std::pow(long double, long double)
/Developer/SDKs/MacOSX10.6.sdk/usr/include/c++/4.2.1/cmath:357: note:                 float std::pow(float, float)
, which I've isolated to pow (-1.0, k). I guess I could replace that with something that returns -1 if k is odd, and 1 if it's even, but I'm feeling a bit lazy right now :P
Last edited on Mar 5, 2011 at 3:34am
Mar 5, 2011 at 3:35am
A wild guess is that the second argument of this function is the result from computing factorial. If this is the case, cast the result from factorial to double.
Mar 5, 2011 at 3:37am
k, as in for (long k = 0 ; k < loops ; k ++ ); k, as in 1 / π = 12 [k = 0, Σ, ∞]

[I used k instead of i because k was in the original formula.]
Last edited on Mar 5, 2011 at 3:38am
Mar 5, 2011 at 3:46am
Well, cast k to double or float, like pow (-1.0, (double)k).
Mar 5, 2011 at 5:20am
Okay. Now I have two errors:[output]In file included from [path to project]/calculatePi.cpp:1:
[path to project]/header.h:10: error: 'string' has not been declared
[path to project]/calculatePi.cpp: In function 'int negOneToTheXthPower(long int)':
[path to project]/calculatePi.cpp:51: error: invalid conversion from 'const char*' to 'int'
Last edited on Mar 5, 2011 at 5:22am
Mar 5, 2011 at 6:38am
The string type is defined in header <string>. You need to include it in the beginning of header.h. The second error sounds obscure. I can't understand where in negOneToTheXthPower is char* needed.
Mar 5, 2011 at 2:15pm
( (k%2==0)? 1: -1 ) pow is overkill.
-1^k
if k is even -> 1
if k is odd -> -1


Okay, I fixed that error by replacing “1.5” with ‘3 / 2 ’
Integer division returns an integer. In this case 3/2=1

Where are you using a string?

Side note: the numbers in C have finite precision.
Last edited on Mar 5, 2011 at 2:17pm
Mar 5, 2011 at 6:56pm
@ne555

simeonz said that the caret is used for bit fiddling, not powers.

Okay, I've changed it back to ‘1.5’ .

Never mind; I forgot to type using namespace std ; is all. Now there's just the
Undefined symbols:
  "factorial(int)", referenced from:
      calculatePi(long) in calculatePi.o
      calculatePi(long) in calculatePi.o
      calculatePi(long) in calculatePi.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
linking error.

I know; I'm planning to change all the doubles to this data type that supposedly has infinite precision (enough precision, anyway) . There's a bunch of problems with that that I'll need some help with though, so I'm just using doubles for now.
Mar 5, 2011 at 7:01pm
Oh, and there's the control-reaches-end-of-non-void-function warning at the end of negOneToTheXthPower (long x)*; how do I tell Xcode to ignore that? I think it's #pragma warning disable and then something, but I can't remember.…


* all of the return statements are in if statements, and if for some reason x is not odd or even there's an else statement that ensures that the script will never reach the final “}” , so that's not a problem
Last edited on Mar 5, 2011 at 7:04pm
Mar 6, 2011 at 8:45am
Finally figured it out: factorial.cpp is in a totally different project. I had added a reference to it, not copied it into this project's folder. No wonder #include "factorial.cpp" wasn't working. Xcode couldn't figure out what I was talking about. Anyway, I included it using the full path and hit Build again, which got me protests about my calls to factorial (long x) . Apparently they were too ambiguous. I fixed them by chopping off the “.0”s in the parameters and hit Build, which got me a duplicate symbol error about factorial (long x) . I tried to fix that by removing the import for factorial.cpp , but that got me a symbol not found error. I think this is the last error. I'll try to figure it out, but again, if anyone can help, I'd really appreciate it.
Mar 6, 2011 at 12:45pm
You should not include implementation files, only header files. Including implementation files (.cpp extension) with the include directive causes redefinitions. I guess this is what the compiler meant by "duplicate symbols".
Mar 6, 2011 at 5:09pm
Okay, so what do I do? If I remove that include, I get a symbol not found error.
Mar 6, 2011 at 5:58pm
I don't know exactly why your IDE is not compiling factorial.cpp and linking against the resulting code. You need to create and include "factorial.h", a header file that declares the factorial function without defining it. But I really don't know what you need to do to include factorial.cpp in your project. As a last resort, if you are in a hurry, copy-paste the contents of factorial.cpp in main.cpp. Investigate the problem later.
Mar 7, 2011 at 5:34am
I found the problem. I had declared factorial as int factorial (int num) in the header file and long factorial (long num) in the implementation file. *facepalms*
Topic archived. No new replies allowed.
Pages: 12