Skip to content

Commit

Permalink
Merge 40eb7f5 into 2db8cdf
Browse files Browse the repository at this point in the history
  • Loading branch information
darkma773r committed Jan 20, 2019
2 parents 2db8cdf + 40eb7f5 commit 0101e48
Show file tree
Hide file tree
Showing 19 changed files with 1,255 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
package org.apache.commons.numbers.complex.streams;

import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.numbers.core.Precision;

import org.apache.commons.numbers.core.precision.Precision;
import org.junit.Assert;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.numbers.core.Precision;

import org.apache.commons.numbers.core.precision.Precision;

/**
* Representation of a Complex number, i.e. a number which has both a
Expand All @@ -39,7 +40,7 @@
* Note that this contradicts the IEEE-754 standard for floating
* point numbers (according to which the test {@code x == x} must fail if
* {@code x} is {@code NaN}). The method
* {@link org.apache.commons.numbers.core.Precision#equals(double,double,int)
* {@link org.apache.commons.numbers.core.precision.Precision#equals(double,double,int)
* equals for primitive double} in class {@code Precision} conforms with
* IEEE-754 while this class conforms with the standard behavior for Java
* object types.</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
import java.io.ObjectOutputStream;

import org.apache.commons.numbers.complex.Complex;
import org.apache.commons.numbers.core.Precision;

import org.apache.commons.numbers.core.precision.Precision;
import org.junit.Assert;

/**
Expand Down
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();
}
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();
}
}
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();
}
}
Loading

0 comments on commit 0101e48

Please sign in to comment.