-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
1,255 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...-core/src/main/java/org/apache/commons/numbers/core/precision/DoublePrecisionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.numbers.core.precision; | ||
|
||
import java.util.Comparator; | ||
|
||
/** Class encapsulating the concept of comparison operations for doubles. | ||
*/ | ||
public abstract class DoublePrecisionContext implements Comparator<Double> { | ||
|
||
/** Return true if the given values are considered equal to each other. | ||
* @param a first value | ||
* @param b second value | ||
* @return true if the given values are considered equal | ||
*/ | ||
public boolean areEqual(final double a, final double b) { | ||
return compare(a, b) == 0; | ||
} | ||
|
||
/** Return true if the given value is considered equal to zero. This is | ||
* equivalent {@code context.areEqual(n, 0.0)} but with a more explicit | ||
* method name. | ||
* @param n the number to compare to zero | ||
* @return true if the argument is considered equal to zero. | ||
*/ | ||
public boolean isZero(final double n) { | ||
return areEqual(n, 0.0); | ||
} | ||
|
||
/** | ||
* Return true if the first argument is strictly less than the second. | ||
* @param a first value | ||
* @param b second value | ||
* @return true if {@code a < b} | ||
*/ | ||
public boolean isLessThan(final double a, final double b) { | ||
return compare(a, b) < 0; | ||
} | ||
|
||
/** | ||
* Return true if the first argument is less than or equal to the second. | ||
* @param a first value | ||
* @param b second value | ||
* @return true if {@code a <= b} | ||
*/ | ||
public boolean isLessThanOrEqual(final double a, final double b) { | ||
return compare(a, b) <= 0; | ||
} | ||
|
||
/** | ||
* Return true if the first argument is strictly greater than the second. | ||
* @param a first value | ||
* @param b second value | ||
* @return true if {@code a > b} | ||
*/ | ||
public boolean isGreaterThan(final double a, final double b) { | ||
return compare(a, b) > 0; | ||
} | ||
|
||
/** | ||
* Return true if the first argument is greater than or equal to the second. | ||
* @param a first value | ||
* @param b second value | ||
* @return true if {@code a >= b} | ||
*/ | ||
public boolean isGreaterThanOrEqual(final double a, final double b) { | ||
return compare(a, b) >= 0; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public int compare(final Double a, final Double b) { | ||
return compare(a.doubleValue(), b.doubleValue()); | ||
} | ||
|
||
/** Compare two double values. The returned value is | ||
* <ul> | ||
* <li> | ||
* {@code 0} if the arguments are considered equal, | ||
* </li> | ||
* <li> | ||
* {@code -1} if {@code a < b}, | ||
* </li> | ||
* <li> | ||
* {@code +1} if {@code a > b} or if either value is NaN. | ||
* </li> | ||
* </ul> | ||
* | ||
* @param a first value | ||
* @param b second value | ||
* @return {@code 0} if the values are considered equal, {@code -1} if the | ||
* first is smaller than the second, {@code 1} is the first is larger | ||
* than the second or either value is NaN. | ||
*/ | ||
public abstract int compare(final double a, final double b); | ||
|
||
/** Get the largest positive double value that is still considered equal | ||
* to zero by this instance. | ||
* @return the largest positive double value still considered equal to zero | ||
*/ | ||
public abstract double getMaxZero(); | ||
} |
108 changes: 108 additions & 0 deletions
108
...rc/main/java/org/apache/commons/numbers/core/precision/EpsilonDoublePrecisionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.numbers.core.precision; | ||
|
||
import java.io.Serializable; | ||
|
||
/** Simple {@link DoublePrecisionContext} subclass that uses an absolute epsilon value to | ||
* determine equality between doubles. | ||
* | ||
* <p>This class uses the {@link Precision#compareTo(double, double, double)} method to compare | ||
* numbers. Two values are considered equal if there is no floating point | ||
* value strictly between them or if their numerical difference is less than or equal | ||
* to the configured epsilon value.</p> | ||
* | ||
* @see Precision#compareTo(double, double, double) | ||
*/ | ||
public class EpsilonDoublePrecisionContext extends DoublePrecisionContext implements Serializable { | ||
|
||
/** Serializable identifer */ | ||
private static final long serialVersionUID = 20190119L; | ||
|
||
/** Epsilon value. */ | ||
private final double epsilon; | ||
|
||
/** Simple constructor. | ||
* @param eps Epsilon value. Numbers are considered equal if there is no | ||
* floating point value strictly between them or if their difference is less | ||
* than or equal to this value. | ||
*/ | ||
public EpsilonDoublePrecisionContext(final double eps) { | ||
this.epsilon = eps; | ||
} | ||
|
||
/** Get the epsilon value for the instance. Numbers are considered equal if there | ||
* is no floating point value strictly between them or if their difference is less | ||
* than or equal to this value. | ||
* @return the epsilon value for the instance | ||
*/ | ||
public double getEpsilon() { | ||
return epsilon; | ||
} | ||
|
||
/** {@inheritDoc} | ||
* This value is equal to the epsilon value for the instance. | ||
* @see #getEpsilon() | ||
*/ | ||
@Override | ||
public double getMaxZero() { | ||
return epsilon; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public int compare(double a, double b) { | ||
return Precision.compareTo(a, b, epsilon); | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public int hashCode() { | ||
int result = 31; | ||
result += 17 * Double.hashCode(epsilon); | ||
|
||
return result; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof EpsilonDoublePrecisionContext)) { | ||
return false; | ||
} | ||
|
||
EpsilonDoublePrecisionContext other = (EpsilonDoublePrecisionContext) obj; | ||
|
||
return this.epsilon == other.epsilon; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(this.getClass().getSimpleName()) | ||
.append("[") | ||
.append("epsilon= ") | ||
.append(epsilon) | ||
.append("]"); | ||
|
||
return sb.toString(); | ||
} | ||
} |
114 changes: 114 additions & 0 deletions
114
...src/main/java/org/apache/commons/numbers/core/precision/EpsilonFloatPrecisionContext.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.commons.numbers.core.precision; | ||
|
||
import java.io.Serializable; | ||
|
||
/** Simple {@link FloatPrecisionContext} subclass that uses an absolute epsilon value to | ||
* determine equality between float. | ||
* | ||
* <p>This class uses the {@link Precision#compareTo(float, float, float)} method to compare | ||
* numbers. Two values are considered equal if there is no floating point | ||
* value strictly between them or if their numerical difference is less than or equal | ||
* to the configured epsilon value.</p> | ||
* | ||
* @see Precision#compareTo(float, float, float) | ||
*/ | ||
public class EpsilonFloatPrecisionContext extends FloatPrecisionContext implements Serializable { | ||
|
||
/** Serializable identifer */ | ||
private static final long serialVersionUID = 20190119L; | ||
|
||
/** Epsilon value. */ | ||
private final float epsilon; | ||
|
||
/** Simple constructor. | ||
* @param eps Epsilon value. Numbers are considered equal if there is no | ||
* floating point value strictly between them or if their difference is less | ||
* than or equal to this value. | ||
*/ | ||
public EpsilonFloatPrecisionContext(final float eps) { | ||
this.epsilon = eps; | ||
} | ||
|
||
/** Get the epsilon value for the instance. Numbers are considered equal if there | ||
* is no floating point value strictly between them or if their difference is less | ||
* than or equal to this value. | ||
* @return the epsilon value for the instance | ||
*/ | ||
public float getEpsilon() { | ||
return epsilon; | ||
} | ||
|
||
/** {@inheritDoc} | ||
* This value is equal to the epsilon value for the instance. | ||
* @see #getEpsilon() | ||
*/ | ||
@Override | ||
public float getMaxZero() { | ||
return epsilon; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public int compare(float a, float b) { | ||
if (Precision.equals(a, b, epsilon)) { | ||
return 0; | ||
} | ||
else if (a < b) { | ||
return -1; | ||
} | ||
return 1; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public int hashCode() { | ||
int result = 31; | ||
result += 17 * Float.hashCode(epsilon); | ||
|
||
return result; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public boolean equals(Object obj) { | ||
if (this == obj) { | ||
return true; | ||
} | ||
if (!(obj instanceof EpsilonFloatPrecisionContext)) { | ||
return false; | ||
} | ||
|
||
EpsilonFloatPrecisionContext other = (EpsilonFloatPrecisionContext) obj; | ||
|
||
return this.epsilon == other.epsilon; | ||
} | ||
|
||
/** {@inheritDoc} **/ | ||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(this.getClass().getSimpleName()) | ||
.append("[") | ||
.append("epsilon= ") | ||
.append(epsilon) | ||
.append("]"); | ||
|
||
return sb.toString(); | ||
} | ||
} |
Oops, something went wrong.