Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between `x > 0` and `x > 0.0D` in Java?

The following code is from the Math.java of java standard library:

public static double abs(double a) {
    return (a <= 0.0D) ? 0.0D - a : a;
}

I have questions that:

  1. Is the code equal to return (a <= 0) ? 0.0D - a : a;
  2. Furthermore, is the code euqal to return (a <= 0) ? - a : a;

Thanks for any help!

like image 479
esse Avatar asked May 19 '21 06:05

esse


People also ask

Is 0.0 A float in Java?

The default value of a float in Java is 0.0f. Float data type is used when you want to save memory and when calculations don't require more than 6 or 7 digits of precision.

Is 0.0 a double?

A literal 0 is considered to be an int literal; literal 0.0 is a double literal. When assigning to a double , either will work (since the int can be cast in a widening conversion); however, casting 0.0 to an int is a narrowing conversion, and must be done explicitly; i.e. (int)0.0 .

How do you know if a double value is zero?

if(value != 0) //divide by value is safe when value is not exactly zero. Otherwise when checking if a floating point value like double or float is 0, an error threshold is used to detect if the value is near 0, but not quite 0.

Why do we use 0 in Java?

So to get the value of any character digit, you can just remove the '0', ie 48. If you don't remove '0' (48) the total sum will be over by 48 * numberOfDigits . See an ascii table to locate digits in it. Note that '0' is the character 0 , not the string "0" containing the character '0' .


Video Answer


1 Answers

a <= 0 when a is double converts 0 to double (due to binary numeric promotion), which means it's equivalent to a <= 0.0D.

a <= 0.0D might be more efficient, since it saves the conversion, but I wouldn't be surprised if the compiler converts a <= 0 to a <= 0.0D in order to save that type conversion.

Relevant JLS quotes:

15.20.1. Numerical Comparison Operators <, <=, >, and >=

Binary numeric promotion is performed on the operands (§5.6.2).

5.6.2. Binary Numeric Promotion

If either operand is of type double, the other is converted to double.

As for your second question, the code that uses unary negation operator is not equivalent to the code of the abs method, since:

For floating-point values, negation is not the same as subtraction from zero, because if x is +0.0, then 0.0-x is +0.0, but -x is -0.0. Unary minus merely inverts the sign of a floating-point number.

(15.15.4. Unary Minus Operator -)

BTW, the case of positive zero or negative zero is mentioned as a special case in the Javadoc of the abs method:

Special cases:

• If the argument is positive zero or negative zero, the result is positive zero.

• If the argument is infinite, the result is positive infinity.

• If the argument is NaN, the result is NaN.

The first two special cases are the cases in which -a differs from 0.0D - a.

like image 151
Eran Avatar answered Oct 17 '22 12:10

Eran