xref: /aoo4110/main/sal/qa/inc/valueequal.hxx (revision b1cdbd2c)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 #include <math.h>
23 
24 #define PREC_float 1
25 #define PREC_double 2
26 #define PREC_long_double 3
27 
28 template<class T>
is_equal(T x,T y,sal_Int16 _nPrec)29 bool is_equal(T x, T y, sal_Int16 _nPrec)
30 {
31     // due to the fact that this check looks only if both values are equal
32     // we only need to look on one value
33 
34     // 14 digits will announce the checkPrecisionSize
35 
36     sal_Int32 nPRECISION;
37     switch(_nPrec)
38     {
39     case PREC_float:
40         nPRECISION = 6;
41         break;
42     case PREC_double:
43         nPRECISION = 14;
44         break;
45     case PREC_long_double:
46         nPRECISION = 20;
47         break;
48     default:
49         nPRECISION = 2;
50     }
51 
52     if (x < 0)
53     {
54         x = -x;
55     }
56     if (y < 0)
57     {
58         y = -y;
59     }
60 
61     // LLA: due to a bug in printf with '%f' and long double within linux environment
62     //      we have to use %lf instead.
63 
64     if (_nPrec != PREC_long_double)
65     {
66         t_print(T_VERBOSE, "double equal: %.20f\n", x);
67         t_print(T_VERBOSE, "              %.20f\n", y);
68     }
69     //here nPrecOfN is the number after dot
70     sal_Int32 nBeforeDot = sal_Int32( log10(x) );
71     if ( nBeforeDot < 0)
72     {
73      	nBeforeDot = 0;
74     }
75     //t_print(T_VERBOSE, "nPRECISION is  %d\n", nPRECISION);
76     sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot;
77 
78     if (_nPrec != PREC_long_double)
79         t_print(T_VERBOSE, "nPrecOfN is  %d\n", nPrecOfN);
80 
81     long double nPrec = pow(0.1, -nPrecOfN);
82 
83     if (_nPrec != PREC_long_double)
84         t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
85 
86     long double nDelta = fabs( x - y ) ;
87 
88     if (_nPrec != PREC_long_double)
89     {
90         t_print(T_VERBOSE, "       delta: %.20f\n", nDelta);
91         t_print(T_VERBOSE, "       nPrec: %.20f\n", nPrec);
92         t_print(T_VERBOSE, "delta must be less or equal to prec!\n\n");
93     }
94 
95     if (nDelta > nPrec)
96     {
97         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
98         return false;
99     }
100     // else
101     // {
102     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
103     return true;
104     // }
105 }
106 
107 // LLA: bool is_float_equal(float x, float y)
108 // LLA: {
109 // LLA:     // due to the fact that this check looks only if both values are equal
110 // LLA:     // we only need to look on one value
111 // LLA:
112 // LLA:     // 6 digits will announce the checkPrecisionSize
113 // LLA:
114 // LLA:     const sal_Int32 nPRECISION = 6;
115 // LLA:     if (x < 0)
116 // LLA:     {
117 // LLA:         x = -x;
118 // LLA:     }
119 // LLA:     if (y < 0)
120 // LLA:     {
121 // LLA:         y = -y;
122 // LLA:     }
123 // LLA:
124 // LLA:     t_print(T_VERBOSE, "double equal: %.20f\n#               %.20f\n", x, y);
125 // LLA:     sal_Int32 nPrecOfN = -nPRECISION + sal_Int32( log10(x) );
126 // LLA:
127 // LLA:     t_print(T_VERBOSE, "prec: %d\n", nPrecOfN);
128 // LLA:     double nPrec = pow(10, nPrecOfN) * 1;
129 // LLA:
130 // LLA:     t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
131 // LLA:
132 // LLA:     double nDelta = fabs( x - y );
133 // LLA:     t_print(T_VERBOSE, "       delta: %.20f\n\n", nDelta);
134 // LLA:
135 // LLA:     if (nDelta > nPrec)
136 // LLA:     {
137 // LLA:         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
138 // LLA:         return false;
139 // LLA:     }
140 // LLA:     // else
141 // LLA:     // {
142 // LLA:     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
143 // LLA:     return true;
144 // LLA:     // }
145 // LLA: }
146 
is_float_equal(float x,float y)147 bool is_float_equal(float x, float y)
148 {
149     return is_equal<float>(x, y, PREC_float);
150 }
is_double_equal(double x,double y)151 bool is_double_equal(double x, double y)
152 {
153     return is_equal<double>(x, y, PREC_double);
154 }
155