# factorial problem

• 07-24-2003
ygfperson
factorial problem
I can't upload the program to show you what I mean, but the piece of code looks like this:
Code:

```inline const int Factorial::operate(const int x) const {   if (!x) return 1;   return x * operate(x-1); }```
My program parses the expression so that the '!' would signal a factorial. That number is converted to a double, converted back to an integer, inserted into this function, and the output is converted back to a double, then printed out. (Ignore the lack of error-handling code... it will be there soon.)

Positive, integral factorials act the way they should up to 12!. At 13! and upwards, it just fails. (The result is usually around e+09, and it sometimes decreases.) At 34! and upwards, it becomes 0. (Which isn't unusual, given my program's handling of infinity.)

If I have the answer to 12! and multiply it by 13, the answer is correct. I'm assuming that there's a conversion error between a double and an int when the int gets big.

Is that possible? Or am I approaching int_max?
• 07-24-2003
quzah
Code:

`if (!x) return 1;`
What are you doing exactly here? Are you overloading the ! operator? If not, !x means "NOT X". Thus, this equates to "if (NOT X is anything other than zero) then return 1".

So in effect, anything non-zero that you pass to the function will end up returning 1.

Quzah.
• 07-24-2003
ygfperson
Quote:

Originally posted by quzah
Code:

`if (!x) return 1;`
What are you doing exactly here? Are you overloading the ! operator? If not, !x means "NOT X". Thus, this equates to "if (NOT X is anything other than zero) then return 1".

So in effect, anything non-zero that you pass to the function will end up returning 1.

Quzah.

x is an int here. And your logic is flawed. The bitwise not operator is '~'. The '!' returns true if the argument is false, and false if the argument is true.

Quote:

> Or am I approaching int_max?
Sailed straight past it
11! is the max you can store in a signed 32 bit integer.
No, it's 12!. 13! is around 6 billion, and 12! is 400 million.
Guess this means I should change the return to a double... and probably the input too, so I don't have twelve or more int to double cast operations.
• 07-24-2003
quzah
Quote:

Originally posted by ygfperson
x is an int here. And your logic is flawed. The bitwise not operator is '~'. The '!' returns true if the argument is false, and false if the argument is true.

Please know what you're talking about. If not, prove me wrong. I said nothing about bitwise. The ! is a logical not operator.

Watch, and be amazed:
Code:

```#include <iostream> using namespace std; int main( void ) {     int x = 5;     cout << "x is " << x << endl;     cout << "!x is " << (!x) << endl;     x = 0;     cout << "x is " << x << endl;     cout << "!x is " << (!x) << endl;     cout << "Thank you, move along." << endl;     return 0; }```
Enjoy.

Quzah.
• 07-24-2003
Cat
His termination condition is correct:

if (!x) return 1;

returns 1 when x is zero. For nonzero x, (x) evaluates to true, so (!x) evaluates as false.

There is no reason to keep things internally as integers, although you need to know that doubles cannot exactly represent all integer values.

BTW, it makes no sense to return "const int" because you're returning by copy anyway.
• 07-24-2003
quzah
Quote:

Originally posted by Cat
His termination condition is correct:

if (!x) return 1;

returns 1 when x is zero. For nonzero x, (x) evaluates to true, so (!x) evaluates as false.

:expletive: I can't believe I got that backwards. It's been a long day. That's my current excuse anyway... :D

Quzah.
• 07-24-2003
Sebastiani
:eek: hee hee. :eek:

That's alright Quzah, as far as I can tell, that's the first mistake you've ever made. :D Now you know what it's like to be me. ;)
• 07-24-2003
golfinguy4
A) I'd create a BigNum/BigFloat class to go along with this project. It'd make things very nice.

B) That recursive method has a couple of flaws. If it is a big number, it might lead to a stack overflow. Also, what about if the user asks for the factorial of a negative number? In your case, you have an eventual stack crash caused by infinite recursion.
• 07-25-2003
ygfperson
Quote:

Originally posted by golfinguy4
A) I'd create a BigNum/BigFloat class to go along with this project. It'd make things very nice.

B) That recursive method has a couple of flaws. If it is a big number, it might lead to a stack overflow. Also, what about if the user asks for the factorial of a negative number? In your case, you have an eventual stack crash caused by infinite recursion.

The calling function for the factorial restricts factorials to positive numbers (or 0). I've changed both types to a const double to take care of number problems. I don't think I'll be factorializing any number big enough to cause a stack problem. I guess it doesn't hurt to make precautions, though.

re: the bignum/bigfloat idea, I hadn't really thought about it. It seems like a good idea once I get things settled.

Quote:

:expletive: I can't believe I got that backwards. It's been a long day. That's my current excuse anyway.. :D
lol... god knows how many times I've said that.