Finding maximum value of a data type

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Clearner321
    New Member
    • Sep 2015
    • 22

    Finding maximum value of a data type

    I was trying the program to calculate the factorial of a number. The program is to identify the value of number at the which the factorial crosses the range of the data type. How should i do this? Please help me for the logic. I want to accommodate all kinds of data types.

    Thanks.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    In C just look in <limits.h>.

    In C++ look in <limits>. Learn how to use he numeric_limit class template.

    Comment

    • Clearner321
      New Member
      • Sep 2015
      • 22

      #3
      I made the following attempt, but if fails
      Code:
      #include<stdio.h>
      #include<limits.h>
      int main(void)
      {
         int number=0;
         unsigned short factorial=1;
         printf("Enter the number to calculate factorial\n");
         scanf("%d",&number);
         printf("us= %d\n",sizeof(unsigned short));
         while(number>1)
         {
             factorial *= number;
             if(factorial > USHRT_MAX)
             {
               break;
             }
             number -= 1;
             printf("%d\n",factorial);
         }
         return 0;
      }
      And the output looks something like this
      Code:
      Enter the number to calculate factorial
      9
      us= 2
      9
      72
      504
      3024
      15120
      60480
      50368
      35200
      Process returned 0 (0x0)   execution time : 1.874 s
      Press any key to continue.
      After 60480 the value of factorial crosses the range. How to identify this?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This code:

        Code:
        while(number>1)
           {
               factorial *= number;
               if(factorial > USHRT_MAX)
               {
                 break;
               }
        etc...
        doesn't work because factorial has changed before it is tested to see if the max is exceeded.

        You have to test before factorial is changed. Therefore, check before you multiply. The room left is USHRT_MAX - factorial. Next check to see how many factorial fit in the room by doing (USHRT_MAX - factorial)/factorial. If the number of factorials left is greater than the number you are going to multiply factorial by, then the new amount will fit:

        Code:
            int count = 1;
            while(count <= number)
           {
               if (USHRT_MAX - factorial)/ factorial) > count)
               {
                  factorial *= count;
               }
               else
               {
                   break;
               }
                      ++count;
               printf("%d\n",factorial);
           }
        Also see that I use the loop counter to do the multiply rather than counting down.
        Last edited by weaknessforcats; Oct 2 '15, 06:03 AM. Reason: made it so it would compile + changed loop control

        Comment

        Working...