1

I have two double values and I want to check for equal or larger and equal or smaller by comparing both values.

I already searched on the internet and also read other related SO questions but couldn't find the solution.

All I could find is, how to compare two double values,

     int returnVal = Double.compare(d1, d2);

But how can I check for d1 <= d2 and d1 >= d2?

Edit:

double remRangeInKm;
if (remRange == 200 || remRange == 500){
     if (remRange == 200) remRangeInKm = 0.2;
     else remRangeInKm = 0.5;
}
else remRangeInKm = remRange * 1.0;

int earthRadius = 6371; //in km
double distance = Math.abs(Math.acos(Math.sin(remLatitude)*Math.sin(currentLatitude)
              + Math.cos(remLatitude)*Math.cos(currentLatitude)
              * Math.cos(currentLongitude - remLongitude)) * earthRadius);

Log.d("Distance after formula", String.valueOf(distance));

if (distance <= remRangeInKm){
    triggerNotification();
}

The distance is 91.482605 meters and remRangeInKm is 200 meters even though it's not executing if statement.

user4583520
  • 77
  • 1
  • 13
  • 1
    Read the Javadocs of `Double#compare(double d1, double d2)`. – M A Mar 18 '15 at 19:45
  • How does d1 <= d2 not work? I am confused – Code Whisperer Mar 18 '15 at 19:46
  • How do you want to check for d1 <= d2 and d1 >= d2 at the same time? Decide if its d1 <= d2 and d1 > d2 or d1 < d2 and d1 >= d2. then just use if() statement – Łukasz Motyczka Mar 18 '15 at 19:49
  • @CodeWhisperer no it doesn't work I already checked. – user4583520 Mar 18 '15 at 19:52
  • @ŁukaszMotyczka in my program I want to check for `<=` but I also included `>=` in my question so if I need it ever I can refer this. – user4583520 Mar 18 '15 at 19:54
  • 1
    show us where `remRangeInKm` is declared and modified – Code Whisperer Mar 18 '15 at 20:12
  • @CodeWhisperer check, I have added it in my question – user4583520 Mar 18 '15 at 20:16
  • Also, how do you know that `triggerNotification` isn't called? – Paul Boddington Mar 18 '15 at 20:17
  • @pbabcdefp man I'm writing android app, if `triggerNotification()` was called it would have notified me. And I also have set `Log.d` inside `if` statement, so if `if` statement executed, it would have printed `Log` in `Logcat` – user4583520 Mar 18 '15 at 20:19
  • OK, so it's not that. We now need to see where remRange is declared and modified. – Paul Boddington Mar 18 '15 at 20:21
  • @pbabcdefp it's a long long chain, actual value comes from sqlite databse in string type, I split the string and convert it into two integers and after that, those int values are converted into kilometers and then to `double`. How much code I post? And I am writing log, there's no mistake I'm doing. – user4583520 Mar 18 '15 at 20:24
  • There clearly is a mistake in what you're doing, otherwise it would work. It looks like you're getting units muddled up somewhere but it's impossible to tell with this information. You said `remRangeInKm` was 200 even though this represents 200 metres. That makes no sense at all. – Paul Boddington Mar 18 '15 at 20:33
  • @pbabcdefp There's no mistake in my code, and you are wrong, as IEEE 754 issue says `==` or `<=` or `>=` MUST NOT be used to compare float or double. See the answer of Binary Judy and James Boone, it's how float or double can truely compared – user4583520 Mar 18 '15 at 20:48
  • 1
    Double and float can be compared using <= >= it is one of the basics of Java. You don't have to use compare(). Your variable is not a double at some point and you refuse to accept it and try to trace where the bad input is coming from. It will work for now but you might have another bad input that might crash the program later and will take longer to fix. You should focus on fixing the cause of the issue not the result of the issue like you are now. – Code Whisperer Mar 23 '15 at 18:55

6 Answers6

6
int returnVal = Double.compare(d1, d2);

This does what you want and returns:

The value 0 if d1 is numerically equal to d2; a value less than 0 if d1 is numerically less than d2; and a value greater than 0 if d1 is numerically greater than d2.

DigitalNinja
  • 1,078
  • 9
  • 17
2

This will do it.

// true if d1 is equal or smaller than d2
Double.compare(d1, d2) <= 0

// true if d1 is equal or larger than d2
Double.compare(d1, d2) >= 0
James Dunn
  • 8,064
  • 13
  • 53
  • 87
  • I tried `if(Double.compare(d1, d2) <= 0)` but it doesn't return true or false but it returns actual difference between d1 and d2. Anyways, your answer helped me. – user4583520 Mar 18 '15 at 20:43
1

What about:

double d1 = 1.0;
double d2 = 2.0;
if (d1 <= d2) {
   System.out.println(d1 + " is lesser than " + d2);
}

And that works for Double: the compiler will box/unbox as needed. Of course you can do it the other way...

if (d2 >= d1) {
   System.out.println(d1 + " is lesser than " + d2);
}
T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • @CodeWhisperer as I said, I have tested but no success – user4583520 Mar 18 '15 at 19:56
  • What does no success mean? All of the answers here work. – Paul Boddington Mar 18 '15 at 19:57
  • 1
    @user4583520 can you show us the code where you try to compare using <= >= – Code Whisperer Mar 18 '15 at 19:57
  • @CodeWhisperer I'm not trying to prove you wrong but.. let me explain. I'm building android application, where I need to compare two latitude and longitude values and there's also a pre-specified range (i.e. distance in meters or kilometers). If the distance value is less than specified range value then it performs a task. and I already have tried `distance <= range` but it really doesn't work. That's why I posted a question. While testing my app `distance` was `91.482605` meters and `range` was 200 meters even though it's not executing `if(distance <= range)` statement. – user4583520 Mar 18 '15 at 20:04
  • 1
    Are you sure you are comparing 2 _numbers_? – T.Gounelle Mar 18 '15 at 20:08
  • 1
    You need to post all the relevant code in the question so we can reproduce the problem. However the issue is definitely not that <= doesn't work. – Paul Boddington Mar 18 '15 at 20:08
0
int returnVal = Double.compare(d1, d2);     
boolean lessThanOrEqual = returnVal <= 0;
Benten
  • 1,014
  • 12
  • 17
0

Try BigDecimal instead as shown below example.

import java.math.BigDecimal; BigDecimal premium = BigDecimal.valueOf(1586.6d); BigDecimal netToCompany = BigDecimal.valueOf(708.75d); BigDecimal commission = premium.subtract(netToCompany); System.out.println(commission + " = " + premium + " - " + netToCompany);

This results in the following output.

877.85 = 1586.6 - 708.75

Possible duplicate : How to resolve a Java Rounding Double issue

Community
  • 1
  • 1
Om.
  • 2,532
  • 4
  • 22
  • 22
0

This needs some fixing, there is dead code here:

double remRangeInKm;
if (remRange == 200 || remRange == 500){
     if (remRange == 200) remRangeInKm = 0.2;
     else remRangeInKm = 0.5;
}
else remRangeInKm = remRange * 1.0;

Fixed:

double remRangeInKm = 0.0;
if (remRange == 200 || remRange == 500) {
     if (remRange == 200) { 
         remRangeInKm = 0.2;
     } else {
         remRangeInKm = 0.5;
     }
} else {
    // there was a logic mistake here, needs to be multiplied by 0.001 
    // not 1.0 to convert from meters to km
    // if you have 600m * 1.0 == 600.0km
    // should be 600m * 0.001 == 0.6km
    remRangeInKm = remRange * 0.001;
}

Now try to run your code with this fix.

Note: use brackets for if/else, it is much easier to follow the logic and understand what happens in each case.

Code Whisperer
  • 1,041
  • 8
  • 16
  • There's no need to set useless curly braces. I also have tried that, it makes no difference – user4583520 Mar 18 '15 at 20:21
  • Most people would not agree with you. Check the logic mistake as well in conversion. You program probably compares 600 km not .600 km at that check because of the logic issue, hence you never see notification. – Code Whisperer Mar 18 '15 at 20:23
  • There's no logic issue. I have written very accurate code, and what you keep suggesting me is wrong, as per IEEE 754 issue. See the answer of James Boone and Binary Judy, they are true. – user4583520 Mar 18 '15 at 20:45
  • I'm sorry but you are converting METERS to KILOMETERS by multiplying by 1.0. 1000m != 1000km. 1000m = 1km. It should be multiplied by 0.001. I do not believe your logic is correct. – Code Whisperer Mar 23 '15 at 18:47
  • And using <= >= is not an issue your source of inputs is the root issue of all of this but you refuse to trace the inputs and try to fix the result of the issue not the cause. – Code Whisperer Mar 23 '15 at 18:58