I made a program that computes a factorial of a number.

#include<iostream>
#include<fstream>
#include<cmath>
#include<iomanip>
#include<string>
using namespace std;
//unsigned __int64 Fact(unsigned __int64 x );


int main(){


unsigned __int64 num = 100;
unsigned long double fact(1);

for(unsigned __int64 i=1;i<=num;i++)
{
	fact *=i;
	cout<<setprecision(50)<<"i is : "<<i<<"	fact is : "<<fact<<endl;
	
}

return 0 ;
}

The problem is I can't convert 100! into all of its significants digits. The output is 9.3326215443944102 e+157;
How could I get all the number to show. "setprecision" dosen't work here.

any help ???

Recommended Answers

All 5 Replies

cout<<fixed << setprecision(50)<<"i is : "<<i< ...

Don't be supprised when it displays this:

i is : 100 fact is : 933262154439440900000000000000000000000000000000000000
00000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000.00000000000000000000000000000000000000000000000000

Your recent threads all have the same underlying problem. You need more than 64 bits of precision and you are only using a data type that can store 64 bits. You either need a data type that holds more or you need to convert the problem somehow so that you don't need to store more than 64 bits. But if you use a 64 bit unsigned integer and go above 2^64, it can't hold it. If you use the same 64 bits as a double, I think it may be able to hold it, but you pay a round off error price and you won't be able to get the actual digits. So you either have to do something clever mathematically, use one of the arbitrary precision data types out there already, or write your own.

cout<<fixed << setprecision(50)<<"i is : "<<i< ...

Don't be supprised when it displays this:

I see but this is not precise. The output screen displays

9.33......000;

but i am confused because, I know really that 100! has a lot more significant digits and not all that zeros. I think when I declared
unsigned long double fact. The computer could not hold all the real values of 100! so it probably truncacated untill what it could hold and thus set the rest to zero.

My question is that how can I get all the number of 100! ( ex. when i use my computer to calculate 100! it gives me 9.332621544....267e+157) because of this i know there are more number to 100! than what my programs gives me. So any hints or help on how i could get
my program from outputing this: 93326215443944090000000000000000000000000000...
into something loke 9.332621544394409....12457.000 ? ??

Read Vernon's previous post.

Rashakil did suggest GNU MP in one of your other posts.
Your own small basic version wouldn't be hard either.

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.