How to show extra large value?

cylam1000
cylam1000 used Ask the Experts™
on
How to show a extra large value (e.g. 200!)? as the following program.
And how to show that value more accurately?

# include <stdio.h>

int main(void)
{
int cnt, input;
double sum=1;

printf ("Please enter an integer of n to calculate n!: ");
scanf ("%d", &input);

for (cnt=1; cnt<=input; cnt++)
sum*=cnt;

printf ("%d! = %.0f", input, sum);

return 0;
}
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
fridomCEO/Programmer

Commented:
I dont's thing you will get a large value like that with floats I expect some 1.0453e40 stuff or the like. If you really want to have such large numbers you better use specialy libraries for that e.g gmp.

#include <gmp.h>
#include <stdio.h>
#include <stdlib.h>


void fact (long from, mpz_t *result){
     mpz_t val;
     int i = from;

     mpz_init(val);
     mpz_set_si(*result, 1);
     while (i > 1){
          mpz_set_si (val, i);
          mpz_mul(*result, *result, val);
          --i;
     }
     mpz_clear(val);
}


int main (void){
     mpz_t val;
     long fact_from = 0;

     mpz_init(val);
     printf("Give me an integer : ");
     fflush(stdout);
     scanf("%d", &fact_from);
     fact (fact_from, &val);
     printf("fact(%d) = ", fact_from);
     mpz_out_str(stdout, 10, val);
     mpz_clear(val);
     return 0;
}

On Windows such a library comes along with lcc-win32

Regards
Friedrich

Author

Commented:
How if I just want to calculate display 200! ?
Do I need such a complex program provided by fridom?
If I want a program without so much library funchion, what should I do? can divided 200! into more than one part and sum up them?
Most Valuable Expert 2014
Top Expert 2015
Commented:
int cnt, input;
double sum=1;
int exp=0;
printf ("Please enter an integer of n to calculate n!: ");
scanf ("%d", &input);

for( cnt=1; cnt<=input; cnt++ ){
    sum*=cnt;
    while( sum >= 10 ){
        sum /= 10;
        exp++;
    }
}
printf ("%d! = %fe+%d\n", input, sum,exp);
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Commented:
The problem is that a standard floating point number simply cannot hold a number large enough to represent 200!. My calculator has a number range which goes up to 10^100, and the largest factorial I can calculate is 69! (1.711 x 10^98)--200! is going to be in excess of 1.797 x 10^308, which is the largest value which can be held in a "double". Without using some sort of maths library you simply won't be able to handle calculations with numbers this large, much less display the results!

Author

Commented:
Can the output be a fixed point number instand of a floating point number?

Commented:
20! is about as far as I could get with visual's largest double. Ints are not much better, even 64bit ones, even when you exploit the zeros on the end.  They are just whopping big numbers and either must be done in a bignum lib or by hand in char arrays or something.  18446744073709551616 is a 64 bit unsigned int, alas visuals are always signed.  Relatively, its not a large number...

think of it this way:
You can have 3 digits. represent 115681 with 3 digits. Can't do it...  must approximate or create a new, larger number representation...

Author

Commented:
Jonnin, what does representing 115681 with 3 digits mean?
What's "approximate or create a new, larger number representation"? and How?

Commented:
Fixed point will be even worse for representing huge numbers, and besides, you'd then be in the territory of using a specialist maths library--which is exactly what you seem to be holding out against!
Top Expert 2006

Commented:
No comment has been added lately and this question is therefore classified abandoned.

If asker wishes to close the question, then refer to
http://www.experts-exchange.com/help/closing.jsp

Otherwise, I will leave a recommendation in the Cleanup topic area that this question is:
PAQed with A grade to ozo

Please leave any comments here within the next seven days. It is assumed that any participant not responding to this request is no longer interested in its final disposition.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Sunny
EE Cleanup Volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial