Chapter 6 factrl.h


MPD78
10-15-2009, 10:13 AM
Hello all,

The factrl.h used for calculating the factorial, n! of a floating point number is returning a negative and incorrect value for any n greater than 12.

Here is my sample program.

#include "C:\NR Headers\nr3.h"
#include "factrl.h"


int main()
{
Int ans; // the returned factorial value
Int pause; // dummy variable to keep the output screen visible

ans = factrl(12); // Input value of 12
cout << ans << endl;
cin >> pause;

return 0;
}

The result with a value of 12 is the correct value of 479001600.However, for any value greater than 12 the program
returns the value of -2147483648. The correct value for 13! is 6227020800.

I am assuming that routine is programmed correctly because all values between 1 and 12 are returned correctly.

Any help on this would be greatly appreciated.

Thanks
Matt

MPD78
10-15-2009, 10:31 AM
Never mind all,

I simply used the wrong type def.

I had ans as Int instead of Doub.

Here is the corrected program.

#include "C:\NR Headers\nr3.h"
#include "factrl.h"


int main()
{
Doub ans; // the returned factorial value
Int pause; // dummy variable to keep the output screen visible

ans = factrl(13); // Input value of 13
cout << ans << endl;
cin >> pause;

return 0;
}

The correct result is returned.

Thanks
Matt