Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <math.h>
21 :
22 : #define PREC_float 1
23 : #define PREC_double 2
24 : #define PREC_long_double 3
25 :
26 : template<class T>
27 105 : bool is_equal(T x, T y, sal_Int16 _nPrec)
28 : {
29 : // due to the fact that this check looks only if both values are equal
30 : // we only need to look on one value
31 :
32 : // 14 digits will announce the checkPrecisionSize
33 :
34 : sal_Int32 nPRECISION;
35 105 : switch(_nPrec)
36 : {
37 : case PREC_float:
38 50 : nPRECISION = 6;
39 50 : break;
40 : case PREC_double:
41 55 : nPRECISION = 14;
42 55 : break;
43 : case PREC_long_double:
44 0 : nPRECISION = 20;
45 0 : break;
46 : default:
47 0 : nPRECISION = 2;
48 : }
49 :
50 105 : if (x < 0)
51 : {
52 41 : x = -x;
53 : }
54 105 : if (y < 0)
55 : {
56 41 : y = -y;
57 : }
58 :
59 105 : if (_nPrec != PREC_long_double)
60 : {
61 105 : printf("double equal: %.20f\n", x);
62 105 : printf(" %.20f\n", y);
63 : }
64 : //here nPrecOfN is the number after dot
65 105 : sal_Int32 nBeforeDot = sal_Int32( log10(x) );
66 105 : if ( nBeforeDot < 0)
67 : {
68 10 : nBeforeDot = 0;
69 : }
70 : //printf("nPRECISION is %d\n", nPRECISION);
71 105 : sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot;
72 :
73 105 : if (_nPrec != PREC_long_double)
74 105 : printf("nPrecOfN is %" SAL_PRIdINT32 "\n", nPrecOfN);
75 :
76 105 : double nPrec = pow(0.1, -nPrecOfN);
77 :
78 105 : if (_nPrec != PREC_long_double)
79 105 : printf(" prec: %.20f\n", nPrec);
80 :
81 105 : double nDelta = fabs( x - y ) ;
82 :
83 105 : if (_nPrec != PREC_long_double)
84 : {
85 105 : printf(" delta: %.20f\n", nDelta);
86 105 : printf(" nPrec: %.20f\n", nPrec);
87 105 : printf("delta must be less or equal to prec\n\n");
88 : }
89 :
90 105 : if (nDelta > nPrec)
91 : {
92 : // values are not equal
93 17 : return false;
94 : }
95 :
96 : // values are equal
97 88 : return true;
98 : }
99 :
100 50 : bool is_float_equal(float x, float y)
101 : {
102 50 : return is_equal<float>(x, y, PREC_float);
103 : }
104 55 : bool is_double_equal(double x, double y)
105 : {
106 55 : return is_equal<double>(x, y, PREC_double);
107 : }
108 :
109 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|