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