Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Adding two large numbers

Adding two large numbers

Scheduled Pinned Locked Moved C / C++ / MFC
questioncsshelp
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Waldermort
    wrote on last edited by
    #1

    This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

    A N T R S 6 Replies Last reply
    0
    • W Waldermort

      This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

      A Offline
      A Offline
      Ahmed Ismail Mohamed
      wrote on last edited by
      #2

      waldermort wrote:

      ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00;

      if you divede both size1 and size by a big number that make you sure that you can add and you still in the range of long. size1 = size1/n; size2 = size2/n;

      waldermort wrote:

      size1 += size2; // == 0xFFFFFE00

      adding in this line will not make a problem then finally, when you want to output the final value , make the value the program do it time the number you devide by it: ex: textbox1.text = Tostring(size2 * n) // n is the number you devided by it. I hope it work properly with you. regards;:) ** Ahmed Ismail **

      W 1 Reply Last reply
      0
      • A Ahmed Ismail Mohamed

        waldermort wrote:

        ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00;

        if you divede both size1 and size by a big number that make you sure that you can add and you still in the range of long. size1 = size1/n; size2 = size2/n;

        waldermort wrote:

        size1 += size2; // == 0xFFFFFE00

        adding in this line will not make a problem then finally, when you want to output the final value , make the value the program do it time the number you devide by it: ex: textbox1.text = Tostring(size2 * n) // n is the number you devided by it. I hope it work properly with you. regards;:) ** Ahmed Ismail **

        W Offline
        W Offline
        Waldermort
        wrote on last edited by
        #3

        That doesn't work, it produces exactly the same result as simply adding them: ULONG max1 = 0xFFFFFF00; ULONG max2 = 0xFFFFFF00; max1 = max1/4; max2 = max2/4; max2 += max1; max2 *= 4; // == 0xFFFFFE00 The only way I have found is to cast them to a larger data type and check the value using an if else block if ( ((UINT64)max1 + (UINT64)max2) > 0xFFFFFFFF ) max2 = 0xFFFFFFFF; else max2 += max1; But I'm sure there must be a more accurate way of doing it.

        1 Reply Last reply
        0
        • W Waldermort

          This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

          N Offline
          N Offline
          Nader Elshehabi
          wrote on last edited by
          #4

          Hello Well, if you have some assembly background you'd remember that there is a bit in the status register called overflow bit to check for this error. Anyway, I know how to check for overflow in Assembly and C#, but sorry, my C++ is too rusty to remember how to do it in C++:-D. You are right, there is a better way to detect overflow, yet I will provide you with a simpler way of detecting it. Usualy the overflow result is smaller than any of the two operands. So, try this:

          ULONG size1 = 0xFFFFFF00;
          ULONG size2 = 0xFFFFFF00;
          size1 += size2; // == 0xFFFFFE00

          if( size1 < size2)
          size1 = 0xFFFFFFFF;

          Remember, maximum value of each type is totally OS dependant. Keep that in mind.:) If someone got a better approach, please post. I'll try to remember how to check overflow in C++. If I got anything, I'll keep you posted.

          Regards:rose:

          W 1 Reply Last reply
          0
          • W Waldermort

            This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #5

            use the header <LIMITS.h> with this, you can know the types max and min values, and some other informations for floating points types.


            TOXCCT >>> GEII power

            [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

            1 Reply Last reply
            0
            • N Nader Elshehabi

              Hello Well, if you have some assembly background you'd remember that there is a bit in the status register called overflow bit to check for this error. Anyway, I know how to check for overflow in Assembly and C#, but sorry, my C++ is too rusty to remember how to do it in C++:-D. You are right, there is a better way to detect overflow, yet I will provide you with a simpler way of detecting it. Usualy the overflow result is smaller than any of the two operands. So, try this:

              ULONG size1 = 0xFFFFFF00;
              ULONG size2 = 0xFFFFFF00;
              size1 += size2; // == 0xFFFFFE00

              if( size1 < size2)
              size1 = 0xFFFFFFFF;

              Remember, maximum value of each type is totally OS dependant. Keep that in mind.:) If someone got a better approach, please post. I'll try to remember how to check overflow in C++. If I got anything, I'll keep you posted.

              Regards:rose:

              W Offline
              W Offline
              Waldermort
              wrote on last edited by
              #6

              That probably is a better way of doing it. Thinking about it, the output would always be smaller. If you remember the better way, please post it. I would love to know.

              1 Reply Last reply
              0
              • W Waldermort

                This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

                R Offline
                R Offline
                Rick York
                wrote on last edited by
                #7

                You could do something like this : _int64 sum = (_int64)size1 + (_int64)size2; if( sum > ULONG_MAX ) TRACE( "an overflow occurred\n" ); else size1 = (ULONG)sum;

                1 Reply Last reply
                0
                • W Waldermort

                  This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

                  S Offline
                  S Offline
                  Stephen Hewitt
                  wrote on last edited by
                  #8

                  Try this: ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; ULONG sizeR = size1 + size2; // == 0xFFFFFE00 if (sizeR < max(size1, size2)) {     // Overflow. }

                  Steve

                  1 Reply Last reply
                  0
                  • W Waldermort

                    This is the same with any data type, when 1 large number is added to another and the result is larger than the data type can hold, it wraps around. i.e. ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; size1 += size2; // == 0xFFFFFE00 The output is less than the input. I want to check for this problem and if it's there I would prefer the output to be the max value the data type can hold. What is the best way to approach this?

                    M Offline
                    M Offline
                    MayankT
                    wrote on last edited by
                    #9

                    try this: if (MAXVAL - size1 < size2) { //overflow } else { size1 += size2; } MAXVAL can be obtained from limits.h as toxcct said.

                    ---------------------- Mayank Thakore Learning C++ - since 1998 They didn't print my card right; so I resigned.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups