xref: /AOO41X/main/sal/qa/inc/valueequal.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir #include <math.h>
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir #define PREC_float 1
4*cdf0e10cSrcweir #define PREC_double 2
5*cdf0e10cSrcweir #define PREC_long_double 3
6*cdf0e10cSrcweir 
7*cdf0e10cSrcweir template<class T>
8*cdf0e10cSrcweir bool is_equal(T x, T y, sal_Int16 _nPrec)
9*cdf0e10cSrcweir {
10*cdf0e10cSrcweir     // due to the fact that this check looks only if both values are equal
11*cdf0e10cSrcweir     // we only need to look on one value
12*cdf0e10cSrcweir 
13*cdf0e10cSrcweir     // 14 digits will announce the checkPrecisionSize
14*cdf0e10cSrcweir 
15*cdf0e10cSrcweir     sal_Int32 nPRECISION;
16*cdf0e10cSrcweir     switch(_nPrec)
17*cdf0e10cSrcweir     {
18*cdf0e10cSrcweir     case PREC_float:
19*cdf0e10cSrcweir         nPRECISION = 6;
20*cdf0e10cSrcweir         break;
21*cdf0e10cSrcweir     case PREC_double:
22*cdf0e10cSrcweir         nPRECISION = 14;
23*cdf0e10cSrcweir         break;
24*cdf0e10cSrcweir     case PREC_long_double:
25*cdf0e10cSrcweir         nPRECISION = 20;
26*cdf0e10cSrcweir         break;
27*cdf0e10cSrcweir     default:
28*cdf0e10cSrcweir         nPRECISION = 2;
29*cdf0e10cSrcweir     }
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir     if (x < 0)
32*cdf0e10cSrcweir     {
33*cdf0e10cSrcweir         x = -x;
34*cdf0e10cSrcweir     }
35*cdf0e10cSrcweir     if (y < 0)
36*cdf0e10cSrcweir     {
37*cdf0e10cSrcweir         y = -y;
38*cdf0e10cSrcweir     }
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir     // LLA: due to a bug in printf with '%f' and long double within linux environment
41*cdf0e10cSrcweir     //      we have to use %lf instead.
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir     if (_nPrec != PREC_long_double)
44*cdf0e10cSrcweir     {
45*cdf0e10cSrcweir         t_print(T_VERBOSE, "double equal: %.20f\n", x);
46*cdf0e10cSrcweir         t_print(T_VERBOSE, "              %.20f\n", y);
47*cdf0e10cSrcweir     }
48*cdf0e10cSrcweir     //here nPrecOfN is the number after dot
49*cdf0e10cSrcweir     sal_Int32 nBeforeDot = sal_Int32( log10(x) );
50*cdf0e10cSrcweir     if ( nBeforeDot < 0)
51*cdf0e10cSrcweir     {
52*cdf0e10cSrcweir      	nBeforeDot = 0;
53*cdf0e10cSrcweir     }
54*cdf0e10cSrcweir     //t_print(T_VERBOSE, "nPRECISION is  %d\n", nPRECISION);
55*cdf0e10cSrcweir     sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir     if (_nPrec != PREC_long_double)
58*cdf0e10cSrcweir         t_print(T_VERBOSE, "nPrecOfN is  %d\n", nPrecOfN);
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir     long double nPrec = pow(0.1, -nPrecOfN);
61*cdf0e10cSrcweir 
62*cdf0e10cSrcweir     if (_nPrec != PREC_long_double)
63*cdf0e10cSrcweir         t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir     long double nDelta = fabs( x - y ) ;
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     if (_nPrec != PREC_long_double)
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         t_print(T_VERBOSE, "       delta: %.20f\n", nDelta);
70*cdf0e10cSrcweir         t_print(T_VERBOSE, "       nPrec: %.20f\n", nPrec);
71*cdf0e10cSrcweir         t_print(T_VERBOSE, "delta must be less or equal to prec!\n\n");
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     if (nDelta > nPrec)
75*cdf0e10cSrcweir     {
76*cdf0e10cSrcweir         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
77*cdf0e10cSrcweir         return false;
78*cdf0e10cSrcweir     }
79*cdf0e10cSrcweir     // else
80*cdf0e10cSrcweir     // {
81*cdf0e10cSrcweir     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
82*cdf0e10cSrcweir     return true;
83*cdf0e10cSrcweir     // }
84*cdf0e10cSrcweir }
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir // LLA: bool is_float_equal(float x, float y)
87*cdf0e10cSrcweir // LLA: {
88*cdf0e10cSrcweir // LLA:     // due to the fact that this check looks only if both values are equal
89*cdf0e10cSrcweir // LLA:     // we only need to look on one value
90*cdf0e10cSrcweir // LLA:
91*cdf0e10cSrcweir // LLA:     // 6 digits will announce the checkPrecisionSize
92*cdf0e10cSrcweir // LLA:
93*cdf0e10cSrcweir // LLA:     const sal_Int32 nPRECISION = 6;
94*cdf0e10cSrcweir // LLA:     if (x < 0)
95*cdf0e10cSrcweir // LLA:     {
96*cdf0e10cSrcweir // LLA:         x = -x;
97*cdf0e10cSrcweir // LLA:     }
98*cdf0e10cSrcweir // LLA:     if (y < 0)
99*cdf0e10cSrcweir // LLA:     {
100*cdf0e10cSrcweir // LLA:         y = -y;
101*cdf0e10cSrcweir // LLA:     }
102*cdf0e10cSrcweir // LLA:
103*cdf0e10cSrcweir // LLA:     t_print(T_VERBOSE, "double equal: %.20f\n#               %.20f\n", x, y);
104*cdf0e10cSrcweir // LLA:     sal_Int32 nPrecOfN = -nPRECISION + sal_Int32( log10(x) );
105*cdf0e10cSrcweir // LLA:
106*cdf0e10cSrcweir // LLA:     t_print(T_VERBOSE, "prec: %d\n", nPrecOfN);
107*cdf0e10cSrcweir // LLA:     double nPrec = pow(10, nPrecOfN) * 1;
108*cdf0e10cSrcweir // LLA:
109*cdf0e10cSrcweir // LLA:     t_print(T_VERBOSE, "        prec: %.20f\n", nPrec);
110*cdf0e10cSrcweir // LLA:
111*cdf0e10cSrcweir // LLA:     double nDelta = fabs( x - y );
112*cdf0e10cSrcweir // LLA:     t_print(T_VERBOSE, "       delta: %.20f\n\n", nDelta);
113*cdf0e10cSrcweir // LLA:
114*cdf0e10cSrcweir // LLA:     if (nDelta > nPrec)
115*cdf0e10cSrcweir // LLA:     {
116*cdf0e10cSrcweir // LLA:         // t_print(T_VERBOSE, "values are not equal! ndelta:%.20f\n", nDelta);
117*cdf0e10cSrcweir // LLA:         return false;
118*cdf0e10cSrcweir // LLA:     }
119*cdf0e10cSrcweir // LLA:     // else
120*cdf0e10cSrcweir // LLA:     // {
121*cdf0e10cSrcweir // LLA:     // t_print(T_VERBOSE, "values are equal.     ndelta:%.20f\n", nDelta);
122*cdf0e10cSrcweir // LLA:     return true;
123*cdf0e10cSrcweir // LLA:     // }
124*cdf0e10cSrcweir // LLA: }
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir bool is_float_equal(float x, float y)
127*cdf0e10cSrcweir {
128*cdf0e10cSrcweir     return is_equal<float>(x, y, PREC_float);
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir bool is_double_equal(double x, double y)
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir     return is_equal<double>(x, y, PREC_double);
133*cdf0e10cSrcweir }
134