3

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?

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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).

4 Comments

OK, now I get negative result, how do I fix that?
I tried bigInteger with n lack,tried long as well with no luck at all
Try: (long)((long)44334*220*220+ (long)81744*220 + 39416)
Glad I could help! I'll add the comment in my answer.
2

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.

Comments

1

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

6 Comments

Great this worked just fine. Thanks. My error was that I did not create new BigInteger every time.
For convenience, because I am going to use this inside an equation with variables, not numbers, I used intermediate casting for that,(long)((long)44334*220*220+ (long)81744*220 + 39416) and it worked
Do you know how to cast the bigInt into an int? (at some point the result will get smaller(by using mode), and I will be able to store it into an int)
Yes, you could use the intValue method, but you will have a problem if the number is bigger then the max int.
Example: BigInteger c = new BigInteger("39416"); use c.intValue();
|
1

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.

9 Comments

You're gonna have a hard time since the result is bigger than MAX_INT. ;-)
yes, this is the problem now, I tried using BigInteger but still with no luck. Any ideas?
Wouldn't just a long do it? But I'm not sure if you'd have to cast intermediary results as well.
Nop, long as far as I know is 9 digits, here is ten digits, (I tried that and I still got negative nmbers)
MAX_LONG is (2^63)-1 which is 9223372036854775807. Should be more than enough. Try casting intermediary results as well (or use just long variables).
|
0

Just put an L after every number you write.

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

2 Comments

Thanks, but the numbers are just an example, I am going to use it with variables in an equation, which are quite big. So, i do not think it will work with the variable names
using long type (rather than int) parameters would do the trick then.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.