Thread: Solution for truncation after 17th Digit?

  1. #1
    Registered User
    Join Date
    Apr 2015
    Posts
    1

    Lightbulb Solution for truncation after 17th Digit?

    I was trying to write a simple program on C about the classical history between the king, chessboard and rice grains. The total amount of rice grains that is needed to fill the chessboard is : 18 446 744 073 709 551 615 .
    But i'm getting : 18 446 744 073 709 552 000. on C.
    Aren't there any solution to increase 17 digits resolution? I saw some solutions which were very long and complicated where a newbie like me couldnt understand anything.
    Here is my code:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
      double i=1.0;
      while(i<=64)  
      {
        printf("%2.0lf.Casilla = %.0lf\n", i, pow(2.0,(i-1.0)));
        i++;
      }
      printf("\n\n***En Total = %.0lf Granos.\n\n",pow(2.0,64.0)-1);
      return0;
    }
    



  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    805

    Quote Originally Posted by Utku Sönmez View Post
    I was trying to write a simple program on C about the classical history between the king, chessboard and rice grains. The total amount of rice grains that is needed to fill the chessboard is : 18 446 744 073 709 551 615 .
    But i'm getting : 18 446 744 073 709 552 000. on C.
    Aren't there any solution to increase 17 digits resolution? I saw some solutions which were very long and complicated where a newbie like me couldnt understand anything.
    Here is my code:

    Code:
    #include<stdio.h>
    #include<math.h>
    
    int main(void)
    {
      double i=1.0;
      while(i<=64)  
      {
        printf("%2.0lf.Casilla = %.0lf\n", i, pow(2.0,(i-1.0)));
        i++;
      }
      printf("\n\n***En Total = %.0lf Granos.\n\n",pow(2.0,64.0)-1);
      return0;
    }
    


    Several points:
    • The index to a loop is usually an int.
    • Why are you calculating the total at the end instead of accumulating in a total variable?
    • Are there any floating point data types larger than a double?
    • man pow
    Last edited by rstanley; 04-04-2015 at 11:32 AM.

  3. #3
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    C99 introduced an 'unsigned long long integer' type.

    It has a max value (ULLONG_MAX in limits.h) of 18 446 744 073 709 551 615.
    Last edited by gemera; 04-04-2015 at 01:52 PM.

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    805
    Quote Originally Posted by gemera View Post
    C99 introduced an 'unsigned long long integer' type.

    It has a max value (ULLONG_MAX in limits.h) of 18 446 744 073 709 551 615.
    What data types are returned from the pow() functions?
    I was hinting to the OP to do the work, rather than give the answer.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    Fair dues. I wasn't addressing your post though. Just making the OP aware there is a type available for the problem. If the OP has covered functions a solution is not that difficult.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  2. Read from file - 1-digit and 2-digit numbers
    By Bonaventura in forum C Programming
    Replies: 8
    Last Post: 03-06-2010, 06:33 AM
  3. Replies: 6
    Last Post: 10-31-2008, 10:00 AM
  4. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  5. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 1
    Last Post: 09-14-2007, 03:28 AM

Tags for this Thread