diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2020-02-12 16:47:14 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-02-12 16:47:14 -0500 |
| commit | f07834e19a34d5f9c03d681083b5ba30e262889d (patch) | |
| tree | ad0b29fe3de15cad85ba6ecfa6bd35115ab34785 /source/core | |
| parent | e1e7a6eec04ee1d04dc7e0d0212208d4c7a9592b (diff) | |
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.
Diffstat (limited to 'source/core')
| -rw-r--r-- | source/core/slang-math.h | 27 |
1 files changed, 27 insertions, 0 deletions
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) { |
