Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiplying two positive Int32 returns incorrect, negative answer?

Tags:

c#

int32

I'm really stumped on this one. I'm coding in C# for Windows Phone 7.5; I am taking text from a text box, parsing it into an array, then converting each array element into an Int32 using Convert.ToInt32, then running the resulting Int32 values through a series of mathematical calculations, multiplying and adding the Int32 values to hardcoded numbers (all dependent upon what's selected in the UI).

All is well until I take the resulting calculations and multiply them together: I'm getting a negative number from multiplying two positive numbers! This is the only time I am doing any calculations with both of the numbers that originated in the method that uses the Convert.ToInt32 function. When they're added, subtracted, or even divided, the math comes out correctly. But when they're multiplied, no dice. The math is totally wrong; I double checked the math in LibreOffice Calc and it doesn't match. When stepping through the code, everything's correct until the numbers that originated in the method that uses the Convert.ToInt32 function are multiplied together. Any ideas?

like image 600
RabbitEar Avatar asked Mar 09 '13 20:03

RabbitEar


People also ask

What happens when you multiply 2 positives?

Rule 1: A positive number times a positive number equals a positive number. This is the multiplication you have been doing all along, positive numbers times positive numbers equal positive numbers.

Why is multiplying two negative numbers a positive answer?

The fact that the product of two negatives is a positive is therefore related to the fact that the inverse of the inverse of a positive number is that positive number back again.

What happens when you multiply to negatives?

Explanation: When multiplying negative numbers, we get a negative answer if there are an odd number of negative numbers being multiplied. We get a positive answer if there are an even number of negative numbers being multiplied.

Can an integer be multiplied by zero and result in negative number?

The multiplication property of zero: Regardless of what the other number is, multiplying by zero always results in an answer of zero. That zero manages to be both a non-negative and non-positive integer yet is neither negative nor positive is just one of the unique properties of the number.


1 Answers

The numbers are probably large enough to overflow Int32.MaxValue.

Consider:

var r = Int32.MaxValue / 2;     // r = 1073741823
var ans = r * 3;                // ans = -1073741827

The reason for this is that negative integers are represented with a 1 in the largest bit, so any multiplication which is larger than the maximum value will have a 1 in this position, which is interpreted as a negative value.

Overflow is a danger with all integral types (int, short, byte, sbyte, char, long, ulong, ushort, and uint). You could use a floating point type, like float or double, but this comes with other problems, like rounding and equality issues. You have to know what kind of numbers you expect, then you might use other types like long, uint, ulong or a floating point type like double or decimal. Ultimately, if you don't know what could happen, you need to write guard code on the input (e.g. wrap it in a checked { } block [thanks, @EricLippert]), and handle it when it is a value outside the range you've designed for.

EDIT: Another option in .Net 4.0+ is to use System.Numerics.BigInteger, which avoids any overflow, but can be slow.

like image 56
codekaizen Avatar answered Oct 04 '22 16:10

codekaizen