Revert " modify license, permission and remove ^M char"
[framework/osp/uifw.git] / src / ui / inc / FUi_Math.h
1 //
2 // Open Service Platform
3 // Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
4 //
5 // Licensed under the Flora License, Version 1.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://floralicense.org/license/
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an AS IS BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17
18 /**
19  * @file                FUi_Math.h
20  * @brief               Header file for internal float math functions
21  *
22  * This file contains declarations internal float math functions.
23  */
24
25
26 #ifndef _FUI_INTERNAL_MATH_H_
27 #define _FUI_INTERNAL_MATH_H_
28
29 #include <math.h>
30 #include <float.h>
31
32 namespace Tizen { namespace Ui
33 {
34
35 template<typename T>
36 inline T
37 _Abs(const T& t) { return t >= 0 ? t : -t;}
38
39 template<typename T>
40 inline const T&
41 _Min(const T& a, const T& b)
42 {
43         if (a < b)
44                 return a;
45         return b;
46 }
47
48 template<typename T>
49 inline const T&
50 _Max(const T& a, const T& b)
51 {
52         if (a < b)
53                 return b;
54         return a;
55 }
56
57 #define ALMOST_ZERO_FLOAT   0.00001f
58 #define ALMOST_ZERO_DOUBLE  0.000000000001
59
60 // We do not use epsilon but hard-coded value to check if the two values are close "*ENOUGH*".
61 static inline bool
62 _FloatCompare(double p1, double p2)
63 {
64         return (p1 == p2 || _Abs(p1 - p2) <= ALMOST_ZERO_DOUBLE * _Min(_Abs(p1), _Abs(p2)));
65 }
66
67 static inline bool
68 _FloatCompare(float p1, float p2)
69 {
70         return (p1 == p2 || _Abs(p1 - p2) <= ALMOST_ZERO_FLOAT * _Min(_Abs(p1), _Abs(p2)));
71 }
72
73 static inline bool
74 _FloatCompareGE(float p1, float p2)
75 {
76     return (p1 >= p2 || _Abs(p1 - p2) <= ALMOST_ZERO_FLOAT * _Min(_Abs(p1), _Abs(p2)));
77 }
78
79 static inline bool
80 _FloatCompareGE(double p1, double p2)
81 {
82     return (p1 >= p2 || _Abs(p1 - p2) <= ALMOST_ZERO_DOUBLE * _Min(_Abs(p1), _Abs(p2)));
83 }
84
85 static inline bool
86 _FloatCompareLE(float p1, float p2)
87 {
88     return (p1 <= p2 || _Abs(p1 - p2) <= ALMOST_ZERO_FLOAT * _Min(_Abs(p1), _Abs(p2)));
89 }
90
91 static inline bool
92 _FloatCompareLE(double p1, double p2)
93 {
94     return (p1 <= p2 || _Abs(p1 - p2) <= ALMOST_ZERO_DOUBLE * _Min(_Abs(p1), _Abs(p2)));
95 }
96
97 static inline bool
98 _FloatHardCompare(double p1, double p2)
99 {
100         return (_Abs(p1 - p2) <= DBL_EPSILON);
101 }
102
103 static inline bool
104 _FloatHardCompare(float p1, float p2)
105 {
106         return (_Abs(p1 - p2) <= FLT_EPSILON);
107 }
108
109 }}      // Tizen::Ui
110
111 #endif  // _FUI_INTERNAL_MATH_H_