Integer division in Java [duplicate] - java

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 6 years ago.
This feels like a stupid question, but I can't find the answer anywhere in the Java documentation. If I declare two ints and then divide them, what exactly is happening? Are they converted to floats/doubles first, divided, then cast back to an integer, or is the division "done" as integers?
Also, purely from experimentation, integer division seems to round the answer towards zero (i.e. 3/2 = 1 and -3/2 = -1). Am I right in believing this?

They are being divided in integer arithmetics. So dividing integer a by integer b you get how many times b fits into a. Also a % b will give you a remainder of a division. So (a / b ) * b + a % b = a

Java does autoconvert types:
"It autoconverts ints to doubles. It autoconverts shorts and bytes to ints even when no ints are involved, requiring constant annoying casts when you want to do short or byte arithmetic. It autoconverts primitives to wrappers and vice versa for boxing and autoboxing." - user2357112
Java never casts anything without you specifying it.
But still integer / integer = integer.
Also, it does always truncate the result. So if the result would be 0.999999 as float the integer division would still return 0.

Related

Maximum value of Java Biginteger in pow [duplicate]

This question already has answers here:
BigInteger.pow(BigInteger)?
(9 answers)
Closed 3 years ago.
I have code that will get the exponent value from given input:
BigInteger a= new BigInteger(2,10);
BigInteger b;
b=a.pow(9999999999);
It is working when value is lower than 7 digits. For example:
BigInteger a= new BigInteger(2,10);
BigInteger b;
b=a.pow(1234567);
Does my code make it possible or is it not possible to have 10 digit in the exponent?
I'm using JDK 1.8.
BigInteger.pow() only exists for int parameters, so you can't take the power bigger than Integer.MAX_VALUE at once.
Those numbers would also be incredibly big (as in "rapidly approaching and passing the number of particles in the observable universe), if you could do it, and there are very few uses for this.
Note that the "power with modulus" operation which is often used in cryptography is implemented using BigInteger.modPow() which does take BigInteger arguments and can therefore handle effectively arbitrarily large values.
pow's parameter is an int. The range of int is -2147483648 to 2147483647, so the answer is it depends on which 10 digits you use. If those 10 digits are 1234567890, that's fine, it's in range (though potentially you'll get a really, really big BigInteger which may push your memory limits); if they're 9999999999 as in your question, that's not fine, it's out of range.
E.g., this compiles:
int a = 1234567890;
This does not:
int b = 9999999999;
^--------------- error: integer number too large: 9999999999

finding modulus of huge numbers in java [duplicate]

This question already has answers here:
How to calculate modulus of large numbers?
(10 answers)
Closed 6 years ago.
for(i=0; i<n+1; i++)
{
y=y+(a[i]*(int)Math.pow(j,i));
}
int r=y/786433;
s[k]=y-(r*786433);
k++;
Now in this code the j value can be 786432. So when I try to get modulus of a number say (1+2*(786432)^2+3*(786432)^3)%786433 then I get -521562 which is not correct I was using modulus operator before too but I got the same answer even with this approach I am getting the same answer. In this approach the modulus of the number is stored in array s[k]. Can anyone help?
If you use Math.pow you are using a double types. Then you convert it back to an int. Rounding can happen and also truncating if values are too big.
To solve this problem You need to use BigInteger:
Immutable arbitrary-precision integers
In particular the method mod:
Returns a BigInteger whose value is (this mod m). This method differs from remainder in that it always returns a non-negative BigInteger.

The difference made by ".0" in double calculation [duplicate]

This question already has answers here:
Why does the division of two integers return 0.0 in Java? [duplicate]
(6 answers)
Closed 8 years ago.
In the calculation of double numbers, what's the difference between, say, 6.0 and 6?
Because when I was solving a problem on online judge, the expression
estimatedPI = Math.sqrt(6*a/b);
got "Wrong answer" on OJ, while
estimatedPI = Math.sqrt(6.0*a/b);
got "Accepted" on OJ.
For the output, because I used
String result;
result = String.format("%.6f\n",estimatedPI);
System.out.print(result);
so the output looks exactly the same, with six digits after decimal point.
The estimatedPI is declared double and a,b declared int.
So why 6.0 got "Accepted" and 6 got "Wrong answer"? What would be the difference here?
Thanks.
Edit: Noted of duplicated questions.
6.0 is a double. 6 is an int.
If a and b are also ints, then 6*a/b is not a "double calculation" - it will be done using int arithmetic.
When you mix doubles and ints in a binary mathematical operation, the int is converted to a double and then the operation is done using double arithmetic. So 6.0*a does a double multiplication (converting a to double first), resulting in a double. Then (the result of that)/b also does a double division (converting b to double first).
The .0 is an indicator to the compiler that the constant is a floating point number rather than an integer. Your expression 6 * a / b will be treated as an integer expression.

Why does this Java division print out zero? [duplicate]

This question already has answers here:
Division of integers in Java [duplicate]
(7 answers)
Closed 9 years ago.
I have the following line of code:
System.out.println(5/9);
I expect to see 0.555 as a result, but instead it prints out zero. Can someone help me understand why this happens? I am currently learning programming and appreciate the help.
Thanks!
This happens because what you are unknowingly doing is Integer Division.
To make calculations fast, computer uses Integer division method when there's no decimal number involved, and hence decimal values are lost.
Try this out:
System.out.println(5.0 / 9.0);
or
System.out.println(5.0 / 9);
or
System.out.println(5 / 9.0);
or
System.out.println((float) 5 / 9);
or
System.out.println(5 / (float) 9);
You trying to divide 5 by 9.Both are integers.So the answer is also an integer.So return 0 as answer.So try like System.out.println(5.0/9); or assign values to float variables and go with that
Integer divide by Integer returns Integer
Do like this
System.out.println(5.0/9);
This is an integer division because both operands are of type integer. An integer division gives an integer result obtained by truncation.
When Integer division is ran in Java (Integer / Integer), the number is calculated then rounded down to the while number. Therefore, 5/9 is evaluated as 0.55555 then rounded down to 0.

integer giving negative values in java in multiplication using positive numbers [duplicate]

This question already has answers here:
Multiplication of two ints overflowing to result in a negative number
(5 answers)
Closed 9 years ago.
public class Test {
public static void main(String[] args) {
int sum=0;
for(int i=1;i<10;i++)
sum = sum+i*i*i*i*i*i*i*i*i*i;
System.out.println(sum);
}
}
OUTPUT:619374629
for(int i=1;i<10;i++)
sum = sum+i*i*i*i*i*i*i*i*i*i*i;
System.out.println(sum);
OUTPUT:
-585353335
In the second output i thought the integer range crossed.But why it is giving -ve number.It need to give me an error.What is the reason for this behaviour?
Thanks in advance...
You have overflowed the size of a 32 bit integer.
Consider what happens when i is equal to 10:
sum = sum + 100000000000 //1 with 11 zeroes
But the maximum positive number that can be stored in a 32 bit integer is only about 2 billion (2 with nine zeroes).
In fact, it gets even worse! The intermediate calculations will be performed with limited precision, and as soon as the multiplication of 10*10*10*10... overflows, then the 10s will be being multiplied with a weird negative number, and be already wrong.
So the number you end up with is not seeming to follow any rules of arithmetic, but in fact it makes perfect sense once you know that primitive integers have limited storage.
The solution is to use 64-bit long and hope you don't overflow THAT too, if you do then you need BigInteger.
Java defines integer math as signed 2s-complement mod 2^32 (for int) and 2^64 (for long). So any time that the result of an int multiply is 2^31 or higher, it wraps around and becomes a negative number. That's just the way integer math works in java.
Java spec
You are using the primitive type. So, when an integer overflows, it will only print out the bits contained in it which is negative. If you want error, try Integer.
As you predicted, you passed the integer range, though causing infinite values (as the sign [the highest bit] gets touched).

Categories

Resources