So I'm trying to write a program that will estimate the value of an integral using Ramanujan's Series equation, but I seem to be getting stuck on something and I'm not sure what it is. I've created the the factorial function to look for the factorial of a number and the term function which is the last summation of the equation.
(His equation can be found here https://en.wikipedia.org/wiki/Logarithmic_integral_function
it is the last equation under Series Representation. And FYI, instead of finding the sum to infinity, it will be 5 instead. And the value of x will be 1000. L(i) should be about 319, but I keep getting nan. **I am not prompting for inputs because I am inputing a text file using the command line**
In function factorial the variable factor is not initialised. The compiler told me that - you should set the warning levels high enough so you get those messages too. It's a golden rule to always initialise your variables to something - even when you are using it on the next line. Also, consider using a debugger, it would have shown that problem almost straight away.
Also, you should avoid using doubles in for loop conditions, they are not represented exactly.
With C++ the declaration of variable can often be delayed until one has a sensible value to assign to it.
TheIdeasMan wrote:
Also, consider using a debugger, it would have shown that problem almost straight away.
You haven't done that have you? It's a really good idea to learn that, it will save you days of staring at code. Hopefully there is a GUI one in your IDE :+) They are easy to use.
It's also a good idea to prompt for input. You std::cin the x variable twice.
The STL has a std::tgamma function if you are allowed to use it.
So I cin>> x >> iter inside the loop for it to be able to read those numbers as pairs. Also the number variable is the number of cases that the loop is running so in case that number would be 7.
And I'm using xcode on my Mac and I'm not quite sure how to use the debugger on it yet!!
The li(1000) = 319 approx is for 5 iterations as the OP stated.
FWIW I found it useful to break the calculations up into easily testable parts and functions. I haven't checked type conflicts/truncations fully but I know this program is very sensitive to the number of iterations. The floor function isn't needed - small free bonus. :)
Thanks! I'll try to play around with it and let you guys know how it goes!.
But on the side question, why do you need a neg and sigma function? it is possible to keep it to just factorial and term function since it's on the requirement to keep it to just those two?
From my point of view there were no restrictions on how many and what type of functions there were other than having a main() function of course.
The ones I have chosen are for convenience. So you can have 0 or any number of other functions depending on what restrictions you are under and what is convenient, especially for developing, testing and refining the code.
The main reason you're having trouble is your factor in factorial is always zero and there is only one summation (Greek sigma in the Wikipedia equation) not two in your L calculation.