Strange division by 0 behavior [duplicate] - java

This question already has answers here:
Confusion on NaN in Java
(4 answers)
Are the bit patterns of NaNs really hardware-dependent?
(4 answers)
Closed 5 years ago.
I had an issue with some NaNs that came from 0/0 not being Canonical in a system I was making and I reduced the problem to this case.
public class Test {
public static void main(String[] args){
Float zero = Float.intBitsToFloat(0);
// Passes
assert Float.floatToRawIntBits(zero) == Float.floatToRawIntBits(0f);
// Fails
assert Float.floatToRawIntBits(zero / zero) == Float.floatToRawIntBits(0f / 0f);
}
}
This is quite bizarre to me. Additionally when debugging with Intellij, watch expressions respond like you would expect; both assertions pass.
I am using openjdk version "1.8.0_131".

Related

puzzle 65-java puzzlers. int overflow can occur in calculations involving subtractions of negative numbers, int.Max_Value, etc How do you avoid it? [duplicate]

This question already has answers here:
How can I check if multiplying two numbers in Java will cause an overflow?
(15 answers)
Closed 1 year ago.
int ans = Integer.MAX_VALUE -(-1); //should I explicitly cast my method parameters in calculation to a wider bit type ?
Found a solution on searching the internet. An article that may help learners like me.
https://www.drdobbs.com/jvm/signalling-integer-overflows-in-java/210500001

calculating the power in Java [duplicate]

This question already has answers here:
Raising a number to a power in Java
(10 answers)
Closed 8 years ago.
I am trying to calculate the power as below but it is giving me 'bad operands type for binary operator '^'. I am guessing that it is a precedence issue but it still doesn't fix with inserting additional brackets
double pw = ((N - (df + 1))^2);
You should use java.lang.Math.pow(x,y)
Example: java.lang.Math.pow(2,3) returns 8
See this
http://www.tutorialspoint.com/java/lang/math_pow.htm
java.lang.Math.pow(double a, double b)
You can use static import for this.

Java Programming - Calculating % [duplicate]

This question already has answers here:
Please explain why 17 % 40 = 17
(5 answers)
Closed 8 years ago.
What is 3 % 5?
Plus how do you figure this out?
I really have no idea how to calculate this. I have done some research but everything is blocked.
It is called Modulo operator: which gives the remainder during a division.
example
46%9=1 as 46-(5*9)=1
38%6=2 as 38-(6*6)=2
3%5= 3. As 3-(5*0)=3
It's called the Modulo operator, it essentially calculates the remainder.
http://en.wikipedia.org/wiki/Modulo_operation
The modulo operator is the remainder operation.
To test you could try:
public class Test {
public static void main(String[]args) {
System.out.println(3%5);
}
}

Java doesn't know how to multiply? What? [duplicate]

This question already has answers here:
Floating point arithmetic not producing exact results [duplicate]
(7 answers)
Closed 8 years ago.
So I'm making a cookie clicker clone in java (shame on me) and for one thing, I need the double to increase by .1 for each cursor you own. The code looks like this (cookies is a double, and all other values are ints):
cookies = cookies + (cursors*.1+grandmas/2+farms*4+factories*10);
however, when this outputs, it outputs numbers like 2.399999999999999 instead of 2.4. What is going on?
That is double's precision. You could use
cookies = Math.round( cookies * 100.0 ) / 100.0;

Java Double Multiplication explanation? [duplicate]

This question already has answers here:
Moving decimal places over in a double
(9 answers)
Closed 9 years ago.
I recently tested something out that I heard using the following code
public static void main(String[] args) {
double x = 4.35 * 100;
System.out.println(x);
}.
I am interested as to why this produces 434.99999999999994 rather than 435.0 . Thanks
When you type:
double x = 4.35;
x is not stored as-is. It is stored in a approaching form (probably 4.349999999 in this case).
If you want exact result, please use BigDecimal.
You can learn about the accuracy problems of floating-point technology.

Categories

Resources