[Orxonox-commit 6084] r10742 - code/branches/cpp11_v2/src/libraries/util

landauf at orxonox.net landauf at orxonox.net
Sun Nov 1 14:52:04 CET 2015


Author: landauf
Date: 2015-11-01 14:52:04 +0100 (Sun, 01 Nov 2015)
New Revision: 10742

Modified:
   code/branches/cpp11_v2/src/libraries/util/Math.h
Log:
use constexpr for some math functions and constants

Modified: code/branches/cpp11_v2/src/libraries/util/Math.h
===================================================================
--- code/branches/cpp11_v2/src/libraries/util/Math.h	2015-11-01 13:26:37 UTC (rev 10741)
+++ code/branches/cpp11_v2/src/libraries/util/Math.h	2015-11-01 13:52:04 UTC (rev 10742)
@@ -72,13 +72,13 @@
     */
     namespace math
     {
-        const float twoPi   = 6.283185482025146484375f;     ///< PI * 2
-        const float pi      = 3.1415927410125732421875f;    ///< PI
-        const float pi_2    = 1.57079637050628662109375f;   ///< PI / 2
-        const float pi_4    = 0.785398185253143310546875f;  ///< PI / 4
-        const float e       = 2.718281269073486328125f;     ///< e
-        const float sqrt2   = 1.41421353816986083984375f;   ///< sqrt(2)
-        const float sqrt2_2 = 0.707106769084930419921875f;  ///< sqrt(2) / 2
+        constexpr float twoPi   = 6.283185482025146484375f;     ///< PI * 2
+        constexpr float pi      = 3.1415927410125732421875f;    ///< PI
+        constexpr float pi_2    = 1.57079637050628662109375f;   ///< PI / 2
+        constexpr float pi_4    = 0.785398185253143310546875f;  ///< PI / 4
+        constexpr float e       = 2.718281269073486328125f;     ///< e
+        constexpr float sqrt2   = 1.41421353816986083984375f;   ///< sqrt(2)
+        constexpr float sqrt2_2 = 0.707106769084930419921875f;  ///< sqrt(2) / 2
     }
 
 #if OGRE_VERSION < 0x010603
@@ -103,7 +103,7 @@
         @return 1 if the value is positive or zero, -1 if the value is negative
     */
     template <typename T>
-    inline T sgn(T x)
+    constexpr inline T sgn(T x)
     {
         return (x >= 0) ? (T)1 : (T)-1;
     }
@@ -115,22 +115,16 @@
         @param max The upper limit
     */
     template <typename T>
-    inline T clamp(T x, T min, T max)
+    constexpr inline T clamp(T x, T min, T max)
     {
-        if (x < min)
-            return min;
-
-        if (x > max)
-            return max;
-
-        return x;
+        return x < min ? min : (x > max ? max : x);
     }
 
     /**
         @brief Returns the squared value (x^2).
     */
     template <typename T>
-    inline T square(T x)
+    constexpr inline T square(T x)
     {
         return x*x;
     }
@@ -139,7 +133,7 @@
         @brief Returns the cubed value (x^3).
     */
     template <typename T>
-    inline T cube(T x)
+    constexpr inline T cube(T x)
     {
         return x*x*x;
     }




More information about the Orxonox-commit mailing list