diff options
author | Chris Robinson <[email protected]> | 2021-01-29 09:56:38 -0800 |
---|---|---|
committer | Chris Robinson <[email protected]> | 2021-01-29 09:56:38 -0800 |
commit | 20057b848cf682344e2447dd663796236727a6d7 (patch) | |
tree | 1d3d511c391e975f0218f38d0ac1a6c34661805e /common | |
parent | 0cd5c37c20bdb4986daa1c3830ab3a30bd1bbe6d (diff) |
Calculate the square root after checking the limit
Diffstat (limited to 'common')
-rw-r--r-- | common/vecmat.h | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/common/vecmat.h b/common/vecmat.h index 997b535b..62cba193 100644 --- a/common/vecmat.h +++ b/common/vecmat.h @@ -35,12 +35,13 @@ public: return *this; } - T normalize(T lim = std::numeric_limits<T>::epsilon()) + T normalize(T limit = std::numeric_limits<T>::epsilon()) { - lim = std::max(lim, std::numeric_limits<T>::epsilon()); - const T length{std::sqrt(mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2])}; - if(length > lim) + limit = std::max(limit, std::numeric_limits<T>::epsilon()); + const T length_sqr{mVals[0]*mVals[0] + mVals[1]*mVals[1] + mVals[2]*mVals[2]}; + if(length_sqr > limit*limit) { + const T length{std::sqrt(length_sqr)}; T inv_length{T{1}/length}; mVals[0] *= inv_length; mVals[1] *= inv_length; |