xref: /trunk/main/sal/qa/inc/valueequal.hxx (revision f67ef9ec)
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     {
80         //t_print(T_VERBOSE, "nPrecOfN is  %d\n", nPrecOfN);
81     }
82 
83     long double nPrec = pow(0.1, -nPrecOfN);
84 
85     if (_nPrec != PREC_long_double)
86     {
87         //t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
88     }
89 
90     long double nDelta = fabs( x - y ) ;
91 
92     if (_nPrec != PREC_long_double)
93     {
94         //t_print(T_VERBOSE, "       delta: %.20f\n", nDelta);
95         //t_print(T_VERBOSE, "       nPrec: %.20f\n", nPrec);
96         //t_print(T_VERBOSE, "delta must be less or equal to prec!\n\n");
97     }
98 
99     if (nDelta > nPrec)
100     {
101         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
102         return false;
103     }
104     // else
105     // {
106     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
107     return true;
108     // }
109 }
110 
111 // LLA: bool is_float_equal(float x, float y)
112 // LLA: {
113 // LLA:     // due to the fact that this check looks only if both values are equal
114 // LLA:     // we only need to look on one value
115 // LLA:
116 // LLA:     // 6 digits will announce the checkPrecisionSize
117 // LLA:
118 // LLA:     const sal_Int32 nPRECISION = 6;
119 // LLA:     if (x < 0)
120 // LLA:     {
121 // LLA:         x = -x;
122 // LLA:     }
123 // LLA:     if (y < 0)
124 // LLA:     {
125 // LLA:         y = -y;
126 // LLA:     }
127 // LLA:
128 // LLA:     t_print(T_VERBOSE, "double equal: %.20f\n#               %.20f\n", x, y);
129 // LLA:     sal_Int32 nPrecOfN = -nPRECISION + sal_Int32( log10(x) );
130 // LLA:
131 // LLA:     t_print(T_VERBOSE, "prec: %d\n", nPrecOfN);
132 // LLA:     double nPrec = pow(10, nPrecOfN) * 1;
133 // LLA:
134 // LLA:     t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
135 // LLA:
136 // LLA:     double nDelta = fabs( x - y );
137 // LLA:     t_print(T_VERBOSE, "       delta: %.20f\n\n", nDelta);
138 // LLA:
139 // LLA:     if (nDelta > nPrec)
140 // LLA:     {
141 // LLA:         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
142 // LLA:         return false;
143 // LLA:     }
144 // LLA:     // else
145 // LLA:     // {
146 // LLA:     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
147 // LLA:     return true;
148 // LLA:     // }
149 // LLA: }
150 
is_float_equal(float x,float y)151 bool is_float_equal(float x, float y)
152 {
153     return is_equal<float>(x, y, PREC_float);
154 }
is_double_equal(double x,double y)155 bool is_double_equal(double x, double y)
156 {
157     return is_equal<double>(x, y, PREC_double);
158 }
159