Reputation: 49
I want to check if something between (including) two doubles is a multiple of one hundred.
With if (double1 % 100 == 0)
I can check one double, but I want to check two doubles.
Example:
double double1 = 398, double2 = 399.6;
should return false but
double double1 = 797.5, double2 = 801.2;
should return true.
Upvotes: 0
Views: 1352
Reputation: 706
Given doubles d1 and d2, is there an integer x such that x is a multiple of 100 and in the range [d1, d2]?
1) If the difference between the two numbers is >= 100, return true.
2) If d1 / 100 (integer division) != d2 / 100, return true
3) If d1 * d2 <= 0, return true
4) If either d1 or d2 is a multiple of 100, return true
5) return false
Step 1 handles cases such as (100, 1100). Step 2 handles cases such as (250, 300). Step 3 handles cases such as (-5, 5), or (0, 10). Step 4 handles cases such as (100, 101).
function test(d1, d2) {
if (d2 - d1 >= 100) return true;
if (((d1/100)|0) != ((d2/100)|0)) return true;
if (d1*d2 <= 0) return true;
if ((d1|0) % 100 == 0 || (d2|0) % 100 == 0) return true;
return false;
}
var cases = [
true, 100, 200,
true, 0, 1,
true, -5, 5,
true, 99, 101,
true, 10.2, 101.2,
true, -0.1, 1.1,
true, 100, 100,
true, 0, 0,
false, 50, 99,
false, 101, 102,
false, 394.3, 399.9,
false, 1, 1,
false, 1001.2, 1001.4
];
for (var i = 0; i < cases.length; i += 3) {
if (test(cases[i + 1], cases[i + 2]) != cases[i]) {
alert("fail! " + cases[i + 1] + " " + cases[i + 2]);
}
}
Upvotes: 0
Reputation: 39457
I think the other solutions suggested here are a bit too complex.
Here is how you can do it in O(1).
public class Test011 {
public static void main(String[] args) {
System.out.println(containsMultipleOf100(99.5, 101.2));
System.out.println(containsMultipleOf100(101.1, 201.2));
System.out.println(containsMultipleOf100(100.001, 199.992));
}
static boolean containsMultipleOf100(double double1, double double2) {
long int1 = (long) Math.ceil(double1);
long int2 = (long) Math.floor(double2);
if ((int2 - (int1-1)) >= 100){
return true;
}else{
return (int2 / 100) * 100 >= int1;
}
}
}
Upvotes: 0
Reputation: 1493
Consider that there will always be a different digit in the hundreds position (and greater), when the case is true
double1 = 398
^ //look at that digit - (and greater)
double2 = 399.6
^ //they are the same, return false!
double1 = 797.5
^ //look at that digit - (and greater)
double2 = 801.2
^ //they are not the same, return true!
double1 = 2300
^^ //look at that digit - (and greater)
double2 = 230
^ //they are not the same, return true!
You could easily compare them by turning them into an int
value (thus ignoring any decimal values), and dividing by 100 (chops off anything below the hundreds position since it is now an int)
In the case where your values could also be negative, -75
and 75
would both divide out to zero and be the same, resulting in returning false
.
For this case, one simple way to check is to multiply the doubles and see if it is less than or equal to 0
double1 = -50;
double2 = 1;
//multiply, result is <= 0, return true
double1 = -5;
double2 = -2;
//multiply, result is > 0, return false
double1 = 1;
double2 = 3;
//multiply, result is > 0, return false
double1 = 0;
double2 = 3;
//multiply, result is <= 0, return true
Upvotes: 0
Reputation: 21
If I understand your question correctly, then the following steps might just help:
Based on the assumption that the first number is always less than the second, an example code is shown below, this is not very efficient though.
static boolean isDivisibleby100(double double1, double double2) {
int int1 = (int) Math.ceil(double1);
int int2 = (int) Math.floor(double2);
for (int i = int1; i <= int2; i++) {
if (i % 100 == 0) {
return true;
}
}
return false;
}
Upvotes: 1