From 48fde63fa085ea8aa0098cb0bbea9911ddc8d944 Mon Sep 17 00:00:00 2001 From: Alexander Bashkirov Date: Sun, 3 Oct 2021 17:39:21 +0300 Subject: [PATCH] [util] Added some common operations with floating points into MathUtil GitOrigin-RevId: e7e02e7360d64a8310611c07160f8325f572e35f --- .../util/src/com/intellij/util/MathUtil.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/platform/util/src/com/intellij/util/MathUtil.java b/platform/util/src/com/intellij/util/MathUtil.java index fc81ff732939..75ec12cea9c7 100644 --- a/platform/util/src/com/intellij/util/MathUtil.java +++ b/platform/util/src/com/intellij/util/MathUtil.java @@ -73,4 +73,48 @@ public final class MathUtil { } return Math.min(max, Math.max(value, min)); } + + /** + * @return {@code true} if {@code a == b} accurate to {@code epsilon} + */ + public static boolean equals(float a, float b, float epsilon) { + return Math.abs(a - b) < epsilon; + } + + /** + * @return {@code true} if {@code a == b} accurate to {@code epsilon} + */ + public static boolean equals(double a, double b, double epsilon) { + return Math.abs(a - b) < epsilon; + } + + /** + * Imprecise version of {@link Float#compare(float, float)} with the given accuracy. + * @param epsilon Accuracy + */ + public static int compare(float a, float b, float epsilon) { + return Math.abs(a - b) < epsilon ? 0 : Float.compare(a, b); + } + + /** + * Imprecise version of {@link Double#compare(double, double)} with the given accuracy. + * @param epsilon Accuracy + */ + public static int compare(double a, double b, double epsilon) { + return Math.abs(a - b) < epsilon ? 0 : Double.compare(a, b); + } + + /** + * @return {@code true} if {@code min <= med <= max} accurate to {@code epsilon} + */ + public static boolean between(float min, float med, float max, float epsilon) { + return compare(min, med, epsilon) < 0 && compare(med, max, epsilon) < 0; + } + + /** + * @return {@code true} if {@code min <= med <= max} accurate to {@code epsilon} + */ + public static boolean between(double min, double med, double max, double epsilon) { + return compare(min, med, epsilon) < 0 && compare(med, max, epsilon) < 0; + } }