lmi-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi-commits] [lmi] master b87d1dd: Ensure correct floating-point arithm


From: Greg Chicares
Subject: [lmi-commits] [lmi] master b87d1dd: Ensure correct floating-point arithmetic
Date: Sun, 9 Apr 2017 13:04:09 -0400 (EDT)

branch: master
commit b87d1dde7b63482b872bffeb998f711f37d4d08a
Author: Gregory W. Chicares <address@hidden>
Commit: Gregory W. Chicares <address@hidden>

    Ensure correct floating-point arithmetic
---
 bourn_cast.hpp | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/bourn_cast.hpp b/bourn_cast.hpp
index a814c23..0c8b211 100644
--- a/bourn_cast.hpp
+++ b/bourn_cast.hpp
@@ -117,11 +117,18 @@ inline To bourn_cast(From from, std::true_type, 
std::false_type)
     using from_traits = std::numeric_limits<From>;
     static_assert(to_traits::is_integer && !from_traits::is_integer, "");
 
+    // At least with i686-w64-mingw32-g++ version 4.9.1, this change:
+    // - if(From(to_traits::max()) + 1 <= from)
+    // + if(adj_max <= from)
+    // fixes incorrect results with '-O0'.
+    static From const volatile raw_max = From(to_traits::max());
+    static From const volatile adj_max = raw_max + From(1);
+
     if(std::isnan(from))
         throw std::runtime_error("Cannot cast NaN to integral.");
     if(from < to_traits::lowest())
         throw std::runtime_error("Cast would transgress lower limit.");
-    if(From(to_traits::max()) + 1 <= from)
+    if(adj_max <= from)
         throw std::runtime_error("Cast would transgress upper limit.");
     To const r = static_cast<To>(from);
     if(r != from)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]