Java: large integer error

I want to use the following mathematical formula in Java:

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

When I enter the same equation in Wolfram Alpha (or Google), I get:

2163788696

In Java, I get negative numbers

I've been trying to find out why, but I'm out of luck I also tried to save the answer in BigInteger, but then I got a negative value because the number was too large

What should I do?

Solution

Now, what you're doing is equivalent to:

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

Parentheses are important! This is your code fix:

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

Edit:

Also pay attention to automatic int casting Use long because the result is greater than max_ Int (but less than max_long, i.e. 9223372036854775807)

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

Or add the suffix l to your numbers (so they are considered long)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>