This question already has answers here:
Mod in Java produces negative numbers [duplicate]
(5 answers)
Closed 2 years ago.
I am converting a program written in Python to Java. There is an equation in Python code: (19 - 20) % 91 and it Java version is (19 - 20) % 91 means same. This equation is giving the answer 90 in Python but, in Java it is giving the answer -1. How do I modify this equation from Python to Java so that it can work and run properly?
The problem here is that in Python the % operator returns the modulus and in Java it returns the remainder. These functions give the same values for positive arguments, but the modulus always returns positive results for negative input, whereas the remainder may give negative results. There's some more information about it in this question.
You can find the positive value by doing this:
int i = (19-20) % 91;
if (i<0) i += 91;
Or you can use Math.floorMod():
Math.floorMod((19-20), 91);
Related
This question already has answers here:
Best way to make Java's modulus behave like it should with negative numbers?
(6 answers)
Closed 5 years ago.
I was writing a code (I knew the output,In Java) , after compiling it giving the input i got a weird value.
Code Snippet -
long a,b,c;
a=-245436499;
b=992;
c=(a+b);
System.out.print(c%b);
And the Output it gave me was
-819
But when i calculated it on calculator, it was
173 \\why?
Proof:
Calculators output
Compiler's output
There are two ways of calculating modulo of a negative number:
Java (as per the JLS) uses the "preserve sign" method; ie if the first operand is negative, non-zero results are negative
Results are (made) positive by adding (if necessary) the second operand to the result of method 1
Your calculator clearly uses method 2 (-819 + 992 = 173)
This question already has answers here:
How do I generate random integers within a specific range in Java?
(72 answers)
Closed 7 years ago.
I'm new to java and was viewing a tutorial and saw nextInt, the guy said that it was supposed to give me a number between -5 and 5 but i have two problems :
(r.nextInt(5 - -5) + -5)
1)In what order does it operate
2)The explanation that i found says that it doesn't accept negatives then why is it supposed to allow me to give me -5?
Thanks a lot.
The answer is unary negative and subtration. Usually, that's written as +. Like
(r.nextInt(5 + 5) + -5)
which is
(r.nextInt(10) - 5)
I assume this is for a Random, in which case it returns [0,10). Which if you subtract 5, is [-5,5).
What your code will do is generate a random integer between 0 and the specified value, and will then subtract 5 from the generated integer.
This question already has answers here:
Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java
(6 answers)
Closed 8 years ago.
I noticed something weird earlier today. I was writing some code that was supposed to make graphs in complex quadrants. Anyway, I typed int i = 1/0; and it wouldn't compile. When I changed the code to double i = 1.0/0.0; the code compiled fine. When I ran the code it gave an error / by 0. I was expecting that... But why does it compile fine when using doubles and not integers? I am using the Blue J IDE
Dividing an int value by zero would result in a ArithmeticException, hence the expression 1 / 0 is illegal.
The result of dividing a double value by zero is infinity or NaN *, so the expression 1.0 / 0.0 is legal.
*) See t_over's comment for specifics:
This question already has answers here:
Mod in Java produces negative numbers [duplicate]
(5 answers)
How does java do modulus calculations with negative numbers?
(15 answers)
Closed 9 years ago.
is something wrong with my code below?
public class testing111 {
public static void main(String[] args) {
int t = 0;
System.out.println((t-5)%360);
}
}
My code above outputs -5. I thought the answer should be 355 and I even checked wolframalpha: http://www.wolframalpha.com/input/?i=%280-5%29%25360
What is causing it to do this?
Thanks!
It seems like WolframAlpha and Java behaves differently here.
In Java, you can get a negative result from the modulo operator. Think "If I divide -5 by 360 and take the remainder of that", since -5 / 360 is 0, you are left over with -5 that simply couldn't be divided. And so the result in Java is -5.
If you want the answer that WolframAlpha produces, you should add 360 if the result from the modulo operator is less than 0.
This question already has answers here:
C and Python - different behaviour of the modulo (%) operation [duplicate]
(6 answers)
How does the modulo (%) operator work on negative numbers in Python?
(12 answers)
Why is operator% referred to as the "modulus" operator instead of the "remainder" operator?
(3 answers)
Closed 9 years ago.
Why modulo operator is not working as intended in C and Java?
Wikipedia has a nice table which shows the sign of the operation for various languages. In Python it is the sign of the divisor (26), in Java/C the sign of the dividend (-1).
Python's %-operator calculates the mathematical remainder, not the modulus. The remainder is by definition a number between 0 and the divisor, it doesn't depend on the sign of the dividend like the modulus.
It is working as specified.
The contract is that
a == (a/b) * b + (a % b)
and integer division truncates toward zero. So with a negative dividend, you get a negative remainder.
Not sure about python but % operator in java returns the remainder obtained after the division.
-1%26
breaking it down:
26)-1(0
0
---
-1 ---> Remainder as (-1+0=-1)