-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber.java
30 lines (28 loc) · 941 Bytes
/
Number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public abstract class Number{
public abstract double getValue();
/*return 0 when this Number equals the other RealNumber
return a negative value when this Number is smaller than the other Number
return a positive value when this Number is larger than the other Number
*/
public int compareTo(Number other){
if ((double)this.getValue() == (double)other.getValue()){
return 0;
}else if ((double)this.getValue() < (double)other.getValue()){
return -1;
}else{
return 1;
}
}
/*
*Return true when the % difference of the values
*are within 0.00001 of eachother.
*Special case: if one is exactly zero, the other must be exactly zero.
*/
public boolean equals(Number other){
double a = Math.round((double)this.getValue()*1000000);
double b = Math.round((double)other.getValue()*1000000);
a /= 1000000;
b /= 1000000;
return a == b;
}
}