This is an automated email from the ASF dual-hosted git repository. desruisseaux pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
commit fbbd6525a877e26265e6fffd3dd4fd0914f07617 Author: Martin Desruisseaux <[email protected]> AuthorDate: Sat Jul 9 12:22:09 2022 +0200 Add a convenience method for testing if a floating-point value is an integer. --- .../sis/internal/processing/image/IsolineTracer.java | 5 +++-- .../java/org/apache/sis/internal/util/Numerics.java | 19 ++++++++++++++++++- .../org/apache/sis/math/LinearlyDerivedVector.java | 3 ++- .../src/main/java/org/apache/sis/math/Vector.java | 4 ++-- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/core/sis-feature/src/main/java/org/apache/sis/internal/processing/image/IsolineTracer.java b/core/sis-feature/src/main/java/org/apache/sis/internal/processing/image/IsolineTracer.java index 609cc8b45d..63dcb38366 100644 --- a/core/sis-feature/src/main/java/org/apache/sis/internal/processing/image/IsolineTracer.java +++ b/core/sis-feature/src/main/java/org/apache/sis/internal/processing/image/IsolineTracer.java @@ -28,6 +28,7 @@ import java.awt.Shape; import org.opengis.referencing.operation.MathTransform; import org.opengis.referencing.operation.TransformException; import org.apache.sis.internal.feature.j2d.PathBuilder; +import org.apache.sis.internal.util.Numerics; import org.apache.sis.util.ArraysExt; @@ -923,8 +924,8 @@ final class IsolineTracer { final boolean isLastPoint = (index >= 6); // See row [6] in above table. if (Double.isFinite(x) && Double.isFinite(y)) { final Point p = new Point((int) x, (int) y); - if (x != Math.floor(x)) p.x = ~p.x; - if (y != Math.floor(y)) p.y = ~p.y; + if (!Numerics.isInteger(x)) p.x = ~p.x; + if (!Numerics.isInteger(y)) p.y = ~p.y; if (isLastPoint) { lastPoint = p; break; // Done searching both points. diff --git a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java index 7721bdfa5f..47be423a21 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java +++ b/core/sis-utility/src/main/java/org/apache/sis/internal/util/Numerics.java @@ -39,7 +39,7 @@ import static java.lang.Math.ulp; * Miscellaneous utilities methods working on floating point numbers. * * @author Martin Desruisseaux (Geomatys) - * @version 1.2 + * @version 1.3 * @since 0.3 * @module */ @@ -190,6 +190,23 @@ public final class Numerics extends Static { return (bit & ~(Long.SIZE - 1)) == 0 ? (1L << bit) : 0; } + /** + * Returns {@code true} if the given number is an integer value. + * Special cases: + * + * <ul> + * <li>If the given value is NaN, than this method returns {@code false}.</li> + * <li>If the given value is positive or negative infinity, then this method returns {@code true} + * (should be false, but this method does not check for infinities for performance reasons).</li> + * </ul> + * + * @param x the value to test. + * @return whether the given value is an integer. + */ + public static boolean isInteger(final double x) { + return x == Math.rint(x); // `rint` is reported faster than `floor`. + } + /** * Returns the smallest (closest to negative infinity) integer value that is greater than or equals to x/y. * diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java index 16241b32f9..573a447299 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java +++ b/core/sis-utility/src/main/java/org/apache/sis/math/LinearlyDerivedVector.java @@ -20,6 +20,7 @@ import java.util.Objects; import java.io.Serializable; import java.util.function.IntSupplier; import org.apache.sis.util.Numbers; +import org.apache.sis.internal.util.Numerics; import org.apache.sis.measure.NumberRange; @@ -95,7 +96,7 @@ final class LinearlyDerivedVector extends Vector implements Serializable { */ @Override public boolean isInteger() { - if (scale == Math.floor(scale) && offset == Math.floor(offset) && base.isInteger()) { + if (Numerics.isInteger(scale) && Numerics.isInteger(offset) && base.isInteger()) { return true; } return super.isInteger(); diff --git a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java index d59cae0c28..31556b5932 100644 --- a/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java +++ b/core/sis-utility/src/main/java/org/apache/sis/math/Vector.java @@ -31,6 +31,7 @@ import org.apache.sis.util.ArraysExt; import org.apache.sis.util.ArgumentChecks; import org.apache.sis.util.logging.Logging; import org.apache.sis.util.resources.Errors; +import org.apache.sis.internal.util.Numerics; import org.apache.sis.internal.system.Loggers; import static java.util.logging.Logger.getLogger; @@ -304,8 +305,7 @@ public abstract class Vector extends AbstractList<Number> implements RandomAcces public boolean isInteger() { if (!Numbers.isInteger(getElementType())) { for (int i=size(); --i >= 0;) { - final double v = doubleValue(i); - if (v != Math.floor(v)) { + if (!Numerics.isInteger(doubleValue(i))) { return false; } }
