phedon rousou
phedon rousou

Reputation: 1780

Java : large integer numbers error

I am trying to do the following math equation in Java:

(44334*(220*220))+ (81744*220) + 39416)

When I enter the same equation in WolframAlpha (or in Google) I get:

2163788696

In java I get negative number..

I have been struggling to find out why this is happening but with no luck. I also tried saving the answer in a BigInteger, but then I get negative values because the digits are too large.

What should I do?

Upvotes: 3

Views: 339

Answers (6)

jspcal
jspcal

Reputation: 51934

EDIT: To deal with the integer wrap-around, use a long:

System.out.println("Result: " +
    (44334L * 220 * 220 + 81744 * 220 + 39416));  // 2163788696

The plus operator is left-associative (whether it's being used for string concatenation or addition), so if the entire arithmetic expression weren't parenthesized, it would be concatenating the results of the sub-expressions as individual strings from left to right.

The left operand determines whether + is used for string concatenation or addition. In this case, the first operand is a string, so the right side ((44334*(220*220))) is converted into a string as well. The result of the first + operator is a string and is used as the left side of another + string concatenation operation. the next operand ((81744*220)) gets converted to a string again.

You can put parentheses around the entire arithmetic expression to avoid that.

Upvotes: 6

Stephen C
Stephen C

Reputation: 719641

The problem is that the + operator in Java is overloaded. It can mean either string concatenation or numeric addition. What is going on is that some of the + operations are being treated as string concatenations rather that numerical additions.

You need to either add a set of parentheses around the expression,

System.out.println("result="+((44334*(220*220))+ (81744*220) + 39416)); 

or use a temporary variable.

int res = (44334*(220*220))+ (81744*220) + 39416;
System.out.println("result="+res);

"The rules" that Java uses to determine the meaning of + are roughly as follows:

  • If the type of the left OR right operand is String, then the + means string concatenation.
  • If the types of both the left AND right operands are primitive numeric types or numeric wrapper types, then the + means a numeric addition.
  • Otherwise this is a compilation error.

The other problem here is that 2163788696 is greater than the largest int value - 2147483647. So to get the right answer (without overflow), you need to tell Java to use long arithmetic; e.g.

System.out.println("result=" + ((44334L * (220 * 220)) + (81744 * 220) + 39416));

If you don't then result will be a negative number ... in this example.

You could also use BigInteger, but it is a bit cumbersome, and long will do just fine here.

Upvotes: 1

mbinette
mbinette

Reputation: 5094

Right now, what you are doing is equivalent to:

System.out.println(("result="+(44334*220*220)) + (81744*220 + 39416) );
// = "result=2145765600" + 18023096
// = "result=214576560018023096"

Parentheses are important! Here is your code fixed:

System.out.println("result=" + (44334*220*220+ 81744*220 + 39416) );
// = "result=2163788696"

EDIT:

Also beware of automatic int casting. Use longs because your result is bigger than MAX_INT (but smaller than MAX_LONG which is 9223372036854775807).

(long)((long)44334*220*220 + (long)81744*220 + 39416)

Or put the suffix L after your numbers (so they're considered as longs).

Upvotes: 2

Hakan Serce
Hakan Serce

Reputation: 11266

Just put an L after every number you write.

(44334L*(220L*220L))+ (81744L*220L) + 39416L

Upvotes: 0

Mihai M.
Mihai M.

Reputation: 240

For the BigInteger version, try this, it will work correctly

BigInteger a = new BigInteger("44334").multiply(new BigInteger("220").multiply(new BigInteger("220")));
BigInteger b = new BigInteger("81744").multiply(new BigInteger("220"));
BigInteger c = new BigInteger("39416");
c = c.add(a).add(b);

System.out.println("resultVersA="+(44334*(220*220))+ (81744*220)  + 39416 );
System.out.println("resultVersB="+ c);

The result will be:

resultVersA=21457656001798368039416

resultVersB=2163788696

Upvotes: 1

Jan B
Jan B

Reputation: 399

The problem is due to the fact that the expression is evaluated from left to right. The first value is of type string, so the + operator then becomes the string concatenation operator instead of the mathmatical addition operator. The expressions (44334*(220*220)) and (81744*220) are then evaluated and converted to strings leading to an incorrect result. If you surround the entire expression with parentheses, the result is calculated properly and you see the correct result.

Upvotes: 2

Related Questions