Here I would like to mention the concept of integer clock.
The maximum and minimum values for int in Java are:
int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648
Please check the following results
int a = 2147483645;
for(int i=0; i<10; i++) {
System.out.println("a:" + a++);
}
Output:
a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642
It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.
-2147483648, <-----------------
-2147483647, |
-2147483646, |
. |
. |
. | (the next value will go back in -ve range)
0, |
+1, |
+2, |
+3, |
. |
. |
., |
+2147483645, |
+2147483646, |
+2147483647 ---------------------
If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be
6227020800
- 2147483647 (+ve max value)
-----------------
Value = 4079537153
- 2147483648 (-ve max value)
-----------------
value = 1932053505
- 1 (for zero in between -ve to +ve value)
----------------
Answer = 1932053504
So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.
You can use long datatype instead of integer to achieve your purpose.
Answer from Jay on Stack OverflowHere I would like to mention the concept of integer clock.
The maximum and minimum values for int in Java are:
int MAX_VALUE = 2147483647
int MIN_VALUE = -2147483648
Please check the following results
int a = 2147483645;
for(int i=0; i<10; i++) {
System.out.println("a:" + a++);
}
Output:
a:2147483645
a:2147483646
a:2147483647
a:-2147483648
a:-2147483647
a:-2147483646
a:-2147483645
a:-2147483644
a:-2147483643
a:-2147483642
It shows that when you go beyond the limit of the +ve range of integer, the next values starts from its negative starting value again.
-2147483648, <-----------------
-2147483647, |
-2147483646, |
. |
. |
. | (the next value will go back in -ve range)
0, |
+1, |
+2, |
+3, |
. |
. |
., |
+2147483645, |
+2147483646, |
+2147483647 ---------------------
If you calculate the factorial of 13 it is 6227020800. This value goes beyond the int range of java. So the new value will be
6227020800
- 2147483647 (+ve max value)
-----------------
Value = 4079537153
- 2147483648 (-ve max value)
-----------------
value = 1932053505
- 1 (for zero in between -ve to +ve value)
----------------
Answer = 1932053504
So, in your answer, the factorial of 13 is becoming 1932053504. This is how integer clock works.
You can use long datatype instead of integer to achieve your purpose.
Please run this code:
System.out.println("Minimum value of Integer is: " + Integer.MIN_VALUE);
System.out.println("Maximum value of Integer is: " + Integer.MAX_VALUE);
So you can see why it fails.
Videos
I am making a math workout program and I need to prompt the user to input what digit range would he want in his math problem (using numbers from 0 to 9, from 10 to 100...).
I thought about making an int array (say I want addition problems that user numbers no bigger than 100 and no smaller than 0, id write int[] range = {0, 100}).
Then id use that array in generating numbers for the problem.
Is this the best way?
You could add spacing ;)
if (i > 0 && i < 100)
For those using commons lang an option is to use Range:
Range<Integer> myRange = Range.between(100, 500);
if (myRange.contains(200)){
// do something
}
Also see: how to construct a apache commons 3.1 Range<Integer> object
Apache Commons Lang has a Range class for doing arbitrary ranges.
Range<Integer> test = Range.between(1, 3);
System.out.println(test.contains(2));
System.out.println(test.contains(4));
Guava Range has similar API.
If you are just wanting to check if a number fits into a long value or an int value, you could try using it through BigDecimal. There are methods for longValueExact and intValueExact that throw exceptions if the value is too big for those precisions.
You could create a class to represent this
public class Range
{
private int low;
private int high;
public Range(int low, int high){
this.low = low;
this.high = high;
}
public boolean contains(int number){
return (number >= low && number <= high);
}
}
Sample usage:
Range range = new Range(0, 2147483647);
if (range.contains(foo)) {
//do something
}