From f07834e19a34d5f9c03d681083b5ba30e262889d Mon Sep 17 00:00:00 2001 From: jsmall-nvidia Date: Wed, 12 Feb 2020 16:47:14 -0500 Subject: Nvrtc disable warnings/Float literal improvements (#1220) * Added 'truncate' for fixing floats, for floats near the max value (as opposed to making infinite). Put AreNearlyEqual into Math * Test for ::make static method. --- source/core/slang-math.h | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'source/core') diff --git a/source/core/slang-math.h b/source/core/slang-math.h index fa7eea6be..7d3a741a4 100644 --- a/source/core/slang-math.h +++ b/source/core/slang-math.h @@ -134,6 +134,33 @@ namespace Slang return log2; } */ + + static bool AreNearlyEqual(double a, double b, double epsilon) + { + // If they are equal then we are done + if (a == b) + { + return true; + } + + const double absA = Abs(a); + const double absB = Abs(b); + const double diff = Abs(a - b); + + // https://en.wikipedia.org/wiki/Double_precision_floating-point_format + const double minNormal = 2.2250738585072014e-308; + // Either a or b are very close to being zero, so doing relative comparison isn't really appropriate + if (a == 0.0 || b == 0.0 || (absA + absB < minNormal)) + { + return diff < (epsilon * minNormal); + } + else + { + // Calculate a relative relative error + return diff < epsilon * (absA + absB); + } + } + }; inline int FloatAsInt(float val) { -- cgit v1.2.3