diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-02-05 11:55:57 -0500 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-02-05 11:55:57 -0500 |
commit | 0cc599b31462e4c0db0d565053147c589face24d (patch) | |
tree | e2243371822b11f8c938f0f6f9e5677cba49f74f /floatprops.h | |
parent | 6d525adeb65c5300bd748017459252a3f4576392 (diff) | |
download | vecmathlib-0cc599b31462e4c0db0d565053147c589face24d.zip vecmathlib-0cc599b31462e4c0db0d565053147c589face24d.tar.gz |
Handle rint() overflow correctly
Diffstat (limited to 'floatprops.h')
-rw-r--r-- | floatprops.h | 20 |
1 files changed, 18 insertions, 2 deletions
diff --git a/floatprops.h b/floatprops.h index c58dfdd..3c423c1 100644 --- a/floatprops.h +++ b/floatprops.h @@ -88,7 +88,15 @@ namespace vecmathlib { { static_assert(sizeof std::lrint(x) >= sizeof(int_t), "lrint() has wrong return type"); - return std::lrint(x); + long res = std::lrint(x); + if (sizeof std::lrint(x) > sizeof(int_t)) { + if (res < std::numeric_limits<int_t>::min() || + res > std::numeric_limits<int_t>::max()) + { + return std::numeric_limits<int_t>::min(); + } + } + return res; } }; @@ -157,7 +165,15 @@ namespace vecmathlib { { static_assert(sizeof std::lrint(x) >= sizeof(int_t), "lrint() has wrong return type"); - return std::lrint(x); + long res = std::lrint(x); + if (sizeof std::lrint(x) > sizeof(int_t)) { + if (res < std::numeric_limits<int_t>::min() || + res > std::numeric_limits<int_t>::max()) + { + return std::numeric_limits<int_t>::min(); + } + } + return res; } }; |