Adding two large numbers
-
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? -
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?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 **
-
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 **
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 blockif ( ((UINT64)max1 + (UINT64)max2) > 0xFFFFFFFF ) max2 = 0xFFFFFFFF; else max2 += max1;
But I'm sure there must be a more accurate way of doing it. -
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?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; // == 0xFFFFFE00if( 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:
-
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?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! ]
-
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; // == 0xFFFFFE00if( 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:
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.
-
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? -
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?Try this:
ULONG size1 = 0xFFFFFF00; ULONG size2 = 0xFFFFFF00; ULONG sizeR = size1 + size2; // == 0xFFFFFE00 if (sizeR < max(size1, size2)) { // Overflow. }
Steve
-
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?