compare « double « Java Data Type Q&A

1. Java double comparison epsilon

I wrote a class that tests for equality, less than, and greater than with two doubles in Java. My general case is comparing price that can have an accuracy of ...

2. Java: Double Value Comparison

Do we need to be careful when comparing a double value against zero?

if ( someAmount <= 0){

3. Why is Java's, double) implemented the way it is?

I was looking at the implementation of compare(double, double) in the Java standard library (6). It reads:

public static int compare(double d1, double d2) {
    if (d1 < d2)

4. What should be the epsilon value when performing double value equal comparison

Here is the output for the below program.

value is : 2.7755575615628914E-17 with zero : 1
isEqual with zero : true
My question is, what should be an epsilon value? Is there any robust ...

5. why do comparison of Double references return false?

Why String return true but Double return false ?

String s1 = "a";
String s2 = "a";
System.out.println(s1 == s2); // true

Double d1 = 1D;
Double d2 = 1D;
System.out.println(d1 == d2); // false
I have ...

6. Comparing doubles in Java gives odd results

I really can'get my head around why the following happens:

Double d = 0.0;
System.out.println(d == 0); // is true
System.out.println(d.equals(0)); // is false ?!
This however works as expected:
Double d = 0.0;
System.out.println(d == 0.0); ...

7. Java - Double Comparison

How can I do something like:

int a=5;

in Java?

8. Comparing two numbers

I have this program piece:

double stockWeight = 1;
    if(data[0] > 9 )        
        stockWeight ...

9. Cause of comparing long slower than comparing double

I wrote a little program to calculate the first 18 triples (x,y,z) with x<y<z, which satisfy x^3+y^3=z^3+1. While playing around to optimise the total runtime, I discovered, that using double for the ...

10. How to compare two double values in Java

Possible Duplicate:
How to resolve a Java Rounding Double issue
A simple comparison of two double values in Java creates some problems. Let's consider the following ...

11. Comparing a double against zero

I'm new to Java and I've been trying to implement an algorithm for finding the roots of a cubical equation. The problem arises when I calculate the discriminant and try to ...

12. compare three double numbers

You can't do it in one statement. Compare one statement to another and store the greatest value in a temp variable. Then compare the temp variable to the third variable and store the greate value in the temp variable...etc, until you've gone through all of the values. Math.max() is a useful method for this comparison.

13. Strange result from double comparison

Hello, I have a simple comparison of two double numbers. The same comparison when put in println function gives the correct response (see in the output) but the same comparison if assigned to a boolean value gives a wrong response; I tried to compare numbers inside if condition, in a boolean variable, in reverse for (i.e: gone > MAX_MOVE) but every ...

14. Double comparison

Hi, I have requirement to check following value1 = "99999999999.00" value2 = "123122.55"; double d1 = Double.parse(value1) ; double d2 = Double.parse(value2) ; if(d1>d2){ sop("Big") } else{ sop("small") } Expected output is Big but giving me small. I tried to print their values & its making 9.99999991E something when I use double parse option. Can someone help me in comparing these ...

15. Compare monetary doubles

Hi all, I want to compare 2 values to two decimal places, basically monetary values. I know about precision issues with straight up double comparison using ==, so to check are two values equal (to 2 places) I do the following: ... private static final BigDecimal PLUS_ZERO = new BigDecimal("0.00"); private static final BigDecimal MINUS_ZERO = new BigDecimal("-0.00"); public boolean compareMoney(final ...

17. Regarding comparing two double values.

Actually what I was saying was that you can't store, say, 1.2 accurately in a binary floating point number for the same reason you can't express 1/3 as a decimal number, and that converting a the nearest float value to 1.2 to BigDecimal won't trim off all those extra decimal places unless you specify the number of places the conversion rounds ...


19. Comparing two doubles

Hi, I'm working on a root finder function (something like Bissection or Newton) and I'm facing one doubt Use doubles to perform the algorithm or use BigDecimal? doubles give me faster computing but at some point I must compare the results with zero.For example I'm getting -1.0 0.25 -0.4375 -0.109375 0.06640625 -0.0224609375 0.021728515625 -4.2724609375E-4 0.0106353759765625 0.005100250244140625 0.0023355484008789062 9.539127349853516E-4 ... (one line ...

20. Direct double comparison