Often big numbers become negative
This image shows what you're looking for. In your case it's obviously larger numbers, but the principle stays the same.
Examples of limits in java are:
int: −2,147,483,648 to 2,147,483,647.
long: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
In the image 0000, 0001 etc, shows the binary representation of the numbers.
EDIT: In project euler you often have to think of a way to work around the lagre numbers. The problems are designed with numbers that big so that you can't use the ordinary way of problem solving. However, if you find that you really need to use them, i suggest studying BigInteger anyway. You will find it useful in the long run, and it's not all that complicated. Here is a link with lots of understandable examples:
BigInteger Example
Java: Why does long number get negative?
Java doesn't throw an error if you increase a number after its maximum value. If you wish to have this behaviour, you could use the Math.addExact(long x, long y)
method from Java 8. This method will throw an ArithmeticException
if you pass the Long.MAX_VALUE
.
The reason why Java doesn't throw an exception and you receive negative numbers has to do with the way numbers are stored. For a long primitive the first byte is used for indicating the sign of the number (0 -> positive, 1 -> negative), while the rest are used for the numeric value. This means that Long.MAX_VALUE
which is the biggest positive value will be stored as 01111...111 (0 followed by 63 bits of 1). Since you add a number to Long.MAX_VALUE
you will start receiving negative integers since the sign byte changes to 1. This means you have an numeric overflow, but this error isn't thrown by Java.
Java: why does multiplying large positive number cause negative results?
Let's examine this by taking an example of Integer
.
Integer.MAX_VALUE
can be represented as 01111111111111111111111111111111
which is a 32
bit long string(including sign bit). Now if you happen to add 1
to the above string, it results in 10000000000000000000000000000000
which is same as Integer.MIN_VALUE
. This is called overflow of Integer
.
System.out.println(Integer.toBinaryString(Integer.MAX_VALUE));
// 1111111111111111111111111111111
According to Integer#toBinaryString
:
The unsigned integer value is the argument plus 232 if the argument is negative; otherwise it is equal to the argument. This value is converted to a string of ASCII digits in binary (base 2) with no extra leading 0s.
So that's why you can't see the sign bit but the real value of Integer.MAX_VALUE
is 01111111111111111111111111111111
. Now take a look at this code:
System.out.println(Integer.toBinaryString(Integer.MAX_VALUE + 1));
// 10000000000000000000000000000000
System.out.println(Integer.toBinaryString(Integer.MIN_VALUE));
// 10000000000000000000000000000000
The output of both the numbers is same. Java doesn't protect against Integer
overflow. It is the developer who should take care of this. So what could be the possible solution to this problem? You can use other data types such as long
or BigInteger
. Here are the max values you might be interested in:
System.out.println(Integer.MAX_VALUE); // 2147483647
System.out.println(Long.MAX_VALUE); // 9223372036854775807
System.out.println(Double.MAX_VALUE); // 1.7976931348623157E308
System.out.println(Float.MAX_VALUE); // 3.4028235E38
Once the Integer
reaches MAX_VALUE
it will start to overflow and you will end up in negative value.
why the value of sum is coming out to be negative?
Using long long
instead of long
, the program works:
Ouput: 56074206897
Reason
Range of long: -2^31+1 to +2^31-1
Range of long long: -2^63+1 to +2^63-1
As you can see 2^31-1 = 2147483647 <
56074206897;
but 2^63-1 = 9,223,372,036,854,775,807 > 56074206897
This leads to overflow. According to the C standard, the result of signed integer overflow is undefined behavior. What that means is that if this condition ever happens at runtime, the compiler is allowed to make your code do anything. Your program could crash, or produce the wrong answer, or have unpredictable effects on other parts of your code, or it might silently do what you intended.
In your case it is overflowing the maximum value of long int
on your system. Because long int
is signed, when the most significant bit gets set, it becomes a negative number.
Adding positive integer to int causes it to become negative
As mentioned by others, your int
can't take that number and overflows. Using an Int64
instead does what you want.
Int64 Total = 0;
int First = 0;
int Second = 10000;
while (First <= Second)
{
Total += 5000 + (250 * First);
Console.WriteLine(Total);
First++;
}
Console.ReadKey();
Related Topics
String Parsing in Java with Delimiter Tab "\T" Using Split
How to Disable Loggers of a Class or of Whole Package
How to Use Session in Jsp Pages to Get Information
Eventlisteners Using Hibernate 4.0 with Spring 3.1.0.Release
Java Try-Finally Return Design Question
How to Retrieve a List of Available/Installed Fonts in Android
How to Pass a Value from One Jsp to Another Jsp Page
Netbeans How to Set Command Line Arguments in Java
How to Attach Source or Javadoc in Eclipse for Any Jar File E.G. Javafx
How to Set Eclipse Console Locale/Language
JPA Query.Getresultlist() - Use in a Generic Way
File Upload Using Selenium Webdriver and Java Robot Class
Eclipse Error ... Cannot Be Resolved to a Type