From c7122a8c0daf47a9468404f1a8b805331f603ecd Mon Sep 17 00:00:00 2001 From: Paul Cornett Date: Wed, 14 Oct 2020 12:03:02 -0700 Subject: [PATCH] Add a wxRound() overload for float --- include/wx/math.h | 48 +++++++++++++++++++++++++++-------------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/include/wx/math.h b/include/wx/math.h index 9bd231fc9a..b2eea32c2a 100644 --- a/include/wx/math.h +++ b/include/wx/math.h @@ -62,14 +62,6 @@ #define wxFinite(x) std::isfinite(x) #define wxIsNaN(x) std::isnan(x) - - inline int wxRound(double x) - { - wxASSERT_MSG(x > (double)INT_MIN - 0.5 && x < (double)INT_MAX + 0.5, - wxT("argument out of supported range")); - - return (int)std::lround(x); - } #else /* C++98 */ #if defined(__VISUALC__) || defined(__BORLANDC__) @@ -112,18 +104,6 @@ #define wxIsNaN(x) ((x) != (x)) #endif - inline int wxRound(double x) - { - wxASSERT_MSG(x > (double)INT_MIN - 0.5 && x < (double)INT_MAX + 0.5, - wxT("argument out of supported range")); - -#if defined(HAVE_ROUND) || wxCHECK_VISUALC_VERSION(12) - return int(lround(x)); -#else - return (int)(x < 0 ? x - 0.5 : x + 0.5); -#endif - } - #endif /* C++11/C++98 */ #ifdef __INTELC__ @@ -154,6 +134,34 @@ inline bool wxIsNullDouble(double x) { return wxIsSameDouble(x, 0.); } +inline int wxRound(double x) +{ + wxASSERT_MSG(x > double(INT_MIN) - 0.5 && x < double(INT_MAX) + 0.5, + "argument out of supported range"); + + #if __cplusplus >= 201103 + return int(std::lround(x)); + #elif defined(HAVE_ROUND) || wxCHECK_VISUALC_VERSION(12) + return int(lround(x)); + #else + return int(x < 0 ? x - 0.5 : x + 0.5); + #endif +} + +inline int wxRound(float x) +{ + wxASSERT_MSG(x > float(INT_MIN) && x < float(INT_MAX), + "argument out of supported range"); + + #if __cplusplus >= 201103 + return int(std::lround(x)); + #elif defined(HAVE_ROUND) || wxCHECK_VISUALC_VERSION(12) + return int(lroundf(x)); + #else + return int(x < 0.0f ? x - 0.5f : x + 0.5f); + #endif +} + // Convert between degrees and radians. inline double wxDegToRad(double deg) { return (deg * M_PI) / 180.0; } inline double wxRadToDeg(double rad) { return (rad * 180.0) / M_PI; }