MRPT  1.9.9
round.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2019, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #pragma once
11 
12 #include <mrpt/core/SSE_types.h> // needed by SSE intrinsics used in some inline functions below.
13 #include <cmath> // pow(), lrint()
14 
15 namespace mrpt
16 {
17 /** \addtogroup mrpt_round Round functions (in #include <mrpt/core/round.h>)
18  * \ingroup mrpt_core_grp
19  * @{ */
20 
21 /** Returns the closer integer (int) to x */
22 template <typename T>
23 inline int round(const T value)
24 {
25 #if MRPT_HAS_SSE2
26  __m128d t = _mm_set_sd(value);
27  return _mm_cvtsd_si32(t);
28 #else
29  return static_cast<int>(lrint(value));
30 #endif
31 }
32 
33 /** Returns the closer integer (long) to x */
34 template <typename T>
35 inline long round_long(const T value)
36 {
37 #if MRPT_HAS_SSE2 && MRPT_WORD_SIZE == 64
38  __m128d t = _mm_set_sd(value);
39  return _mm_cvtsd_si64(t);
40 #else
41  return lrint(value);
42 #endif
43 }
44 
45 /** Round a decimal number up to the given 10'th power (eg, to 1000,100,10, and
46  * also fractions)
47  * power10 means round up to: 1 -> 10, 2 -> 100, 3 -> 1000, ... -1 -> 0.1, -2
48  * -> 0.01, ...
49  */
50 template <class T>
51 T round_10power(T val, int power10)
52 {
53  long double F = ::pow((long double)10.0, -(long double)power10);
54  long int t = round_long(val * F);
55  return T(t / F);
56 }
57 
58 /** @} */
59 } // namespace mrpt
GLdouble GLdouble t
Definition: glext.h:3695
long round_long(const T value)
Returns the closer integer (long) to x.
Definition: round.h:35
int val
Definition: mrpt_jpeglib.h:957
T round_10power(T val, int power10)
Round a decimal number up to the given 10&#39;th power (eg, to 1000,100,10, and also fractions) power10 m...
Definition: round.h:51
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
GLsizei const GLfloat * value
Definition: glext.h:4134
int round(const T value)
Returns the closer integer (int) to x.
Definition: round.h:23



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019