Ok, I'm having some issues with miscalculation here and i cannot figure it out?
example:
when i do
long numberTest = 14*13*12*11*10*9*8*7*6*5*4*3*2*1;
cout << numberTest;
cin.get();
it outputs a calculation completely different from my calculator and PC calculator.
when i do anything less the 12 or so it computes and outputs fine
long numberTest = 10*9*8*7*6*5*4*3*2*1;
cout << numberTest;
cin.get();
what its going on here?
some sort of coercion converting it to the incorrect number?
wrong data type? I'm a bit lost.
Any help would be greatly appreciated
error while calculating factorials
Page 1 of 15 Replies - 726 Views - Last Post: 06 February 2011 - 02:58 PM
Replies To: error while calculating factorials
#2
Re: error while calculating factorials
Posted 06 February 2011 - 02:08 PM
The result of 14! is greater than the range of long data type.
Data type ranges
Use long long instead.
Data type ranges
Use long long instead.
#3
Re: error while calculating factorials
Posted 06 February 2011 - 02:11 PM
The value of LONG_MAX is 2147483647
The value of the the factorial is 87178291200
Can you see the problem?
The value of the the factorial is 87178291200
Can you see the problem?
#4
Re: error while calculating factorials
Posted 06 February 2011 - 02:15 PM
Try this:
These are my INT_MAX and LONG_MAX values:
2147483647
9223372036854775807
long numberTest = (long)14*13*12*11*10*9*8*7*6*5*4*3*2*1;
These are my INT_MAX and LONG_MAX values:
2147483647
9223372036854775807
#5
Re: error while calculating factorials
Posted 06 February 2011 - 02:47 PM
Ok, here's what i'm trying to do...
Compare the value of 14! to the value produced by solving Stirlings Approximation formula:
n approximately equal to e^-14 * n^n * sqrt(2(PI)n)
I understand that the value of 14! exceeds the data type I used. But it seems no matter which data type I use i cannot generate the proper result
using this formula:
14*13*12*11*10*9*8*7*6*5*4*3*2*1
I can however generate the correct answer usings stirlings formula...
Heres my code so far:
Thanks for the help so far!!!!!!!!!!!!!!!!!
Using a long long or (long) doesn't change the result. it still is incorrect.
I've tried float, double, long double, long, and long long.
Compare the value of 14! to the value produced by solving Stirlings Approximation formula:
n approximately equal to e^-14 * n^n * sqrt(2(PI)n)
I understand that the value of 14! exceeds the data type I used. But it seems no matter which data type I use i cannot generate the proper result
using this formula:
14*13*12*11*10*9*8*7*6*5*4*3*2*1
I can however generate the correct answer usings stirlings formula...
Heres my code so far:
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
const double PI = 3.14159265;
double stirlingsFormula = (1 / (pow(2.71828, 14.00))) * (pow(14.00, 14.00)) * (sqrt(2*PI*14.00));
double stirlingsFormulaExp = (exp(-14.00)) * (pow(14.00, 14.00)) * (sqrt(2*PI*14.00)); // just using exponential function here to simplify code later on
long exactFact = 14*13*12*11*10*9*8*7*6*5*4*3*2*1;
cout << stirlingsFormula << endl;
cout << stirlingsFormulaExp << endl;
cout << float(exactFact) << endl;
cout << exactFact;
cin.get();
}
as you see the two are very different. I can work out the correct answer with paper and pencil and on a calculator.. but something i just cannot seem to figure out how to store 14! properly, which data type to use to display it either as a decimal value, integer or in scientific notation!
Thanks for the help so far!!!!!!!!!!!!!!!!!
Using a long long or (long) doesn't change the result. it still is incorrect.
I've tried float, double, long double, long, and long long.
This post has been edited by jmoore_20200: 06 February 2011 - 02:50 PM
#6
Re: error while calculating factorials
Posted 06 February 2011 - 02:58 PM
Here's what I'm talking about:
and my output:
#include <iostream>
using namespace std;
long factorial(long);
long recurse_fact(long);
int main (int argc, char * const argv[]) {
long N = 15;
cout << N<<"! = " << factorial(N) << endl;
cout << N<<"! = " << recurse_fact(N) << endl;
long numberTest = (long)14*13*12*11*10*9*8*7*6*5*4*3*2*1;
cout << numberTest << endl;
cout << INT_MAX << " " << LONG_MAX << endl;
return 0;
}
long factorial(long x)
{
long fact = x;
for (long i = x-1; i > 1; i--) {
fact *= i;
}
return fact;
}
long recurse_fact(long x)
{
if (x > 2) {
return x * recurse_fact(x-1);
} else {
return 2;
}
}
and my output:
15! = 1307674368000 15! = 1307674368000 87178291200 2147483647 9223372036854775807
Page 1 of 1

New Topic/Question
Reply



MultiQuote



|