Hi all,

I have written a simple function to compute factorial in C++.
It computes OK until 31! which is 738197504. Now for factorial 32 it gives me a negative result and for 34 it gives me 0.
I know that on my machine LONG_MAX is 2147483647.
So in my recursive algorithm when I try to compute 32! it would cross the max value. That is OK but then I should get an error message and not an incorrect value.
Here comes the code

"
const long factorial(const long n)
{
long fn = 1;
    if (n>1) fn = (n*factorial(n-1));
return fn;
"

ANY IDEA???

>>I should get an error message and not an incorrect value.

No. Its your fault, not the compiler's. A compiler can only produce erros/warnings on the code it sees at compile time. It knows nothing about runtime errors.

Ancient Dragon thanks for the clarification.
But I still have a problem that needs to be resolved. The code above could have been used in a much bigger program that computes something else after long mathematical manipulations.
HOW CAN I SOLVE THE PROBLEM THAT ONE OF MY CALCULATIONS MIGHT CROSS THE LIMIT OF A SPECIFIC TYPE'S MAX OR MIN VALUE.
I CANNOT KNOW THAT IT IS GOING TO CROSS THAT VALUE SINCE I HAVE NOT COMPUTED IT YET
.
So how the hell can I know that after I computed 31! now I will get a wrong result if I multiply it by 32?
or for that matter after any interation in mathematics.
I DO NOT CARE WHO IS TO BLAME! I CARE ABOUT THE SOLUTION!!!

Using a 64-bit integer instead of 32-bit integer will help some. But here is no way to resolve the problem using native c or c++ data types. There are a few large number libraries around, such as this c++ class It uses a vector of digits in char form then manipulates the individual digits much like you would when using pencil & paper.

Thank you Ancient Dragon,

But that is very depressing! What it means is that I can write the best of programs even use large number libraries but even those large number libraries have a MAX value.
In a complex mathematical function calculation one or more parts might cross that line and I will have no way of knowing that???
What a life!!
I WOULD APPRECIATE A DIFFEREMT ADVICE ON HOW TO PROCEED WHEN A MATHEMATICAL FUNCTION IS BEING COMPUTED!

Maybe you misunderstood the link I posted -- it has no limit other than computer memory. The author's example includes calculating 100!

I know this isn't a solution but it should help. If you know that when using your function trying to calculate a factorial higher than 31 will be incorrect, then you can use an if statement to check the number supplied to the function to see if it is higher than 31. If it is then simply return some number that would mean an error condition. Then in the function that calls your factorial function you can check the return and make sure there wasn't an error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.