1565d668cSAndrew Rist /**************************************************************
2565d668cSAndrew Rist *
3565d668cSAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4565d668cSAndrew Rist * or more contributor license agreements. See the NOTICE file
5565d668cSAndrew Rist * distributed with this work for additional information
6565d668cSAndrew Rist * regarding copyright ownership. The ASF licenses this file
7565d668cSAndrew Rist * to you under the Apache License, Version 2.0 (the
8565d668cSAndrew Rist * "License"); you may not use this file except in compliance
9565d668cSAndrew Rist * with the License. You may obtain a copy of the License at
10565d668cSAndrew Rist *
11565d668cSAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12565d668cSAndrew Rist *
13565d668cSAndrew Rist * Unless required by applicable law or agreed to in writing,
14565d668cSAndrew Rist * software distributed under the License is distributed on an
15565d668cSAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16565d668cSAndrew Rist * KIND, either express or implied. See the License for the
17565d668cSAndrew Rist * specific language governing permissions and limitations
18565d668cSAndrew Rist * under the License.
19565d668cSAndrew Rist *
20565d668cSAndrew Rist *************************************************************/
21565d668cSAndrew 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 {
66*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, "double equal: %.20f\n", x);
67*f67ef9ecSDamjan Jovanovic //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)
79*f67ef9ecSDamjan Jovanovic {
80*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, "nPrecOfN is %d\n", nPrecOfN);
81*f67ef9ecSDamjan Jovanovic }
82cdf0e10cSrcweir
83cdf0e10cSrcweir long double nPrec = pow(0.1, -nPrecOfN);
84cdf0e10cSrcweir
85cdf0e10cSrcweir if (_nPrec != PREC_long_double)
86*f67ef9ecSDamjan Jovanovic {
87*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, " prec: %.20f\n", nPrec);
88*f67ef9ecSDamjan Jovanovic }
89cdf0e10cSrcweir
90cdf0e10cSrcweir long double nDelta = fabs( x - y ) ;
91cdf0e10cSrcweir
92cdf0e10cSrcweir if (_nPrec != PREC_long_double)
93cdf0e10cSrcweir {
94*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, " delta: %.20f\n", nDelta);
95*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, " nPrec: %.20f\n", nPrec);
96*f67ef9ecSDamjan Jovanovic //t_print(T_VERBOSE, "delta must be less or equal to prec!\n\n");
97cdf0e10cSrcweir }
98cdf0e10cSrcweir
99cdf0e10cSrcweir if (nDelta > nPrec)
100cdf0e10cSrcweir {
101cdf0e10cSrcweir // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
102cdf0e10cSrcweir return false;
103cdf0e10cSrcweir }
104cdf0e10cSrcweir // else
105cdf0e10cSrcweir // {
106cdf0e10cSrcweir // t_print(T_VERBOSE, "values are equal. ndelta:%.20f\n", nDelta);
107cdf0e10cSrcweir return true;
108cdf0e10cSrcweir // }
109cdf0e10cSrcweir }
110cdf0e10cSrcweir
111cdf0e10cSrcweir // LLA: bool is_float_equal(float x, float y)
112cdf0e10cSrcweir // LLA: {
113cdf0e10cSrcweir // LLA: // due to the fact that this check looks only if both values are equal
114cdf0e10cSrcweir // LLA: // we only need to look on one value
115cdf0e10cSrcweir // LLA:
116cdf0e10cSrcweir // LLA: // 6 digits will announce the checkPrecisionSize
117cdf0e10cSrcweir // LLA:
118cdf0e10cSrcweir // LLA: const sal_Int32 nPRECISION = 6;
119cdf0e10cSrcweir // LLA: if (x < 0)
120cdf0e10cSrcweir // LLA: {
121cdf0e10cSrcweir // LLA: x = -x;
122cdf0e10cSrcweir // LLA: }
123cdf0e10cSrcweir // LLA: if (y < 0)
124cdf0e10cSrcweir // LLA: {
125cdf0e10cSrcweir // LLA: y = -y;
126cdf0e10cSrcweir // LLA: }
127cdf0e10cSrcweir // LLA:
128cdf0e10cSrcweir // LLA: t_print(T_VERBOSE, "double equal: %.20f\n# %.20f\n", x, y);
129cdf0e10cSrcweir // LLA: sal_Int32 nPrecOfN = -nPRECISION + sal_Int32( log10(x) );
130cdf0e10cSrcweir // LLA:
131cdf0e10cSrcweir // LLA: t_print(T_VERBOSE, "prec: %d\n", nPrecOfN);
132cdf0e10cSrcweir // LLA: double nPrec = pow(10, nPrecOfN) * 1;
133cdf0e10cSrcweir // LLA:
134cdf0e10cSrcweir // LLA: t_print(T_VERBOSE, " prec: %.20f\n", nPrec);
135cdf0e10cSrcweir // LLA:
136cdf0e10cSrcweir // LLA: double nDelta = fabs( x - y );
137cdf0e10cSrcweir // LLA: t_print(T_VERBOSE, " delta: %.20f\n\n", nDelta);
138cdf0e10cSrcweir // LLA:
139cdf0e10cSrcweir // LLA: if (nDelta > nPrec)
140cdf0e10cSrcweir // LLA: {
141cdf0e10cSrcweir // LLA: // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
142cdf0e10cSrcweir // LLA: return false;
143cdf0e10cSrcweir // LLA: }
144cdf0e10cSrcweir // LLA: // else
145cdf0e10cSrcweir // LLA: // {
146cdf0e10cSrcweir // LLA: // t_print(T_VERBOSE, "values are equal. ndelta:%.20f\n", nDelta);
147cdf0e10cSrcweir // LLA: return true;
148cdf0e10cSrcweir // LLA: // }
149cdf0e10cSrcweir // LLA: }
150cdf0e10cSrcweir
is_float_equal(float x,float y)151cdf0e10cSrcweir bool is_float_equal(float x, float y)
152cdf0e10cSrcweir {
153cdf0e10cSrcweir return is_equal<float>(x, y, PREC_float);
154cdf0e10cSrcweir }
is_double_equal(double x,double y)155cdf0e10cSrcweir bool is_double_equal(double x, double y)
156cdf0e10cSrcweir {
157cdf0e10cSrcweir return is_equal<double>(x, y, PREC_double);
158cdf0e10cSrcweir }
159