Thread: Big Floating Number, 100! Not right.

  1. #1
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31

    Big Floating Number, 100! Not right.

    Hello there Forum!

    So, I'm new at programming C so my understanding about it is really rather limited. But I managed to put together a simple program to output factorials up to 100. It works fine until n!=18 but afterwards, when the numbers really are atronomical, only the few first digits of the output factorial are correct. The rest is replaced by 0's.

    So, to 100! the output should be a 157 digit number with zeros for the last 24 digits, however, instead, the program returns a 157 digit number with zeros for the last 130 digits. Only the first 27 digits are correct.

    What am I doing wrong?

    Code:
    #include<stdio.h>
    int main(void){
        int i;
        double n;
        n=1;
        for(i=1;i<=100;i++){
                            n*=i;
                            printf("%.0f\n",n);}                      
        system("pause");
        return(0);
    }
    And also, I also made a program to output Euler's number, but I can only get it right up to 18 decimal places. How can I print about e to 100 decimal places?

    Code:
    #include<stdio.h>
    int main(void){
        int i;
        float n,e,t;
        n=1;
        e=1;
        for(i=1;i<=30;i++){
                            n*=i;
                            t=n;
                            e+=1/t;
                            printf("%.20f\n",e);}                      
        system("pause");
        return(0);
    }
    Last edited by KAUFMANN; 01-19-2011 at 08:14 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    That seems about right.

    For each decimal digit, you need just over 3 bits in your number to represent it accurately. For a float, this equates to about 6 decimal digits. For doubles, you get about 15 decimal digits. Beyond that, anything you see is pure noise.

    Floating point numbers are a fixed size (say 4 or 8 bytes). To achieve this, they sacrifice some precision (lots of accurate digits) for range (like being able to hold the most significant digits of 100!).

    Now, if you want to store ALL the digits with total accuracy, you're going to have to use a bignum library like The GNU MP Bignum Library
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by Salem View Post
    That seems about right.

    For each decimal digit, you need just over 3 bits in your number to represent it accurately. For a float, this equates to about 6 decimal digits. For doubles, you get about 15 decimal digits. Beyond that, anything you see is pure noise.

    Floating point numbers are a fixed size (say 4 or 8 bytes). To achieve this, they sacrifice some precision (lots of accurate digits) for range (like being able to hold the most significant digits of 100!).

    Now, if you want to store ALL the digits with total accuracy, you're going to have to use a bignum library like The GNU MP Bignum Library
    So I have to go to this Bignum Library, download a Header file and just include it on the code?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Yes, something like that.

    Though it is not the only one, and it is always possible to write your own (if that is the purpose of the exercise).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User KAUFMANN's Avatar
    Join Date
    Jan 2011
    Location
    Coimbra, Portugal
    Posts
    31
    Quote Originally Posted by Salem View Post
    Yes, something like that.

    Though it is not the only one, and it is always possible to write your own (if that is the purpose of the exercise).
    You think I might be able to generate the first 1 000 000 decimal digits of Euler's number using that library and C?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    "There is no practical limit to the precision except the ones implied by the available memory in the machine GMP runs on. GMP has a rich set of functions, and the functions have a regular interface. "
    1M digits would need about 350K of memory for each number.
    It might take a while, but it can certainly store them on a decent modern machine.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    31
    Quote Originally Posted by KAUFMANN View Post
    So I have to go to this Bignum Library, download a Header file and just include it on the code?

    Just curious... That's way to many digits for human consumption.
    Have you tried using the %e format?

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    36
    If you're running on Windows (Visual C++), you can access GMP through MPIR; I've written a "HowTo" at How to Install and Run GMP on Windows Using MPIR - Exploring Binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. big number
    By rodrigorules in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2010, 01:17 AM
  2. help! calculate arbitrary number of floating points
    By cakestler in forum C Programming
    Replies: 5
    Last Post: 02-26-2009, 02:47 PM
  3. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  4. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  5. Replies: 2
    Last Post: 02-08-2009, 09:26 PM