From 059d82014ec912fe0d63f48caee5e1a53e405201 Mon Sep 17 00:00:00 2001 From: Martin Taibr Date: Fri, 16 Nov 2018 10:57:30 +0100 Subject: [PATCH] document isnan --- qcsrc/lib/deglobalization.qh | 12 ------------ qcsrc/lib/float.qh | 1 + qcsrc/lib/warpzone/mathlib.qc | 10 ++++++++-- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/qcsrc/lib/deglobalization.qh b/qcsrc/lib/deglobalization.qh index 315ff6d6a..21f303a3e 100644 --- a/qcsrc/lib/deglobalization.qh +++ b/qcsrc/lib/deglobalization.qh @@ -31,18 +31,6 @@ STATIC_INIT(globals) { // and assert that the global vectors are NaN before calling the raw functions here // to make sure nobody (even builtins) is accidentally using them - NaN is the most likely value to expose remaining usages - // TODO make sure `isnan` actually works - potential compiler bug: - //LOG_INFOF("%f\n", 0.0/0.0 == 0.0/0.0); - //LOG_INFOF("%f\n", 0.0/0.0 != 0.0/0.0); - //float x = 0.0/0.0; - //LOG_INFOF("%f\n", x == x); - //LOG_INFOF("%f\n", x != x); - - //float y = __builtin_nan(); - //LOG_INFOF("%f\n", y); - //LOG_INFOF("%f\n", y == y); - //LOG_INFOF("%f\n", __builtin_isnan(y)); - CLEAR_V_GLOBALS(); } #endif diff --git a/qcsrc/lib/float.qh b/qcsrc/lib/float.qh index fa4ff77b5..b35494de4 100644 --- a/qcsrc/lib/float.qh +++ b/qcsrc/lib/float.qh @@ -2,4 +2,5 @@ const float FLOAT_MAX = 340282346638528859811704183484516925440.0f; const float FLOAT_EPSILON = 0.00000011920928955078125f; +/// Always use `isnan` function to compare because `float x = FLOAT_NAN; x == x;` gives true const float FLOAT_NAN = 0.0 / 0.0; diff --git a/qcsrc/lib/warpzone/mathlib.qc b/qcsrc/lib/warpzone/mathlib.qc index 4a7c88610..9105269ff 100644 --- a/qcsrc/lib/warpzone/mathlib.qc +++ b/qcsrc/lib/warpzone/mathlib.qc @@ -24,8 +24,13 @@ bool isinf(float e) } bool isnan(float e) { - float f = e; - return (e != f); + // the sane way to detect NaN is broken because of a compiler bug + // (works with constants but breaks when assigned to variables) + // use conversion to string instead + + //float f = e; + //return (e != f); + return ftos(e) == "-nan"; } bool isnormal(float e) { @@ -217,6 +222,7 @@ float copysign(float e, float f) { return fabs(e) * ((f>0) ? 1 : -1); } +/// Always use `isnan` function to compare because `float x = nan(); x == x;` gives true float nan(string tag) { return sqrt(-1); -- 2.39.2