LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sal / qa / inc / valueequal.hxx
blob5e6d47d30f7aa586228b69096913e165025121b9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
21 #define INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
23 #include <sal/config.h>
25 #include <cmath>
27 #define PREC_float 1
28 #define PREC_double 2
29 #define PREC_long_double 3
31 template<class T>
32 bool is_equal(T x, T y, sal_Int16 _nPrec)
34 if (!(std::isfinite(x) && std::isfinite(y))) {
35 return x == y;
38 // due to the fact that this check looks only if both values are equal
39 // we only need to look on one value
41 // 14 digits will announce the checkPrecisionSize
43 sal_Int32 nPRECISION;
44 switch(_nPrec)
46 case PREC_float:
47 nPRECISION = 6;
48 break;
49 case PREC_double:
50 nPRECISION = 14;
51 break;
52 case PREC_long_double:
53 nPRECISION = 20;
54 break;
55 default:
56 nPRECISION = 2;
59 if (x < 0)
61 x = -x;
63 if (y < 0)
65 y = -y;
68 if (_nPrec != PREC_long_double)
70 printf("double equal: %.20f\n", x);
71 printf(" %.20f\n", y);
73 //here nPrecOfN is the number after dot
74 sal_Int32 nBeforeDot = sal_Int32( log10(x) );
75 if ( nBeforeDot < 0)
77 nBeforeDot = 0;
79 //printf("nPRECISION is %d\n", nPRECISION);
80 sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot;
82 if (_nPrec != PREC_long_double)
83 printf("nPrecOfN is %" SAL_PRIdINT32 "\n", nPrecOfN);
85 double nPrec = pow(0.1, -nPrecOfN);
87 if (_nPrec != PREC_long_double)
88 printf(" prec: %.20f\n", nPrec);
90 double nDelta = fabs( x - y ) ;
92 if (_nPrec != PREC_long_double)
94 printf(" delta: %.20f\n", nDelta);
95 printf(" nPrec: %.20f\n", nPrec);
96 printf("delta must be less or equal to prec\n\n");
99 if (nDelta > nPrec)
101 // values are not equal
102 return false;
105 // values are equal
106 return true;
109 bool is_float_equal(float x, float y)
111 return is_equal<float>(x, y, PREC_float);
113 bool is_double_equal(double x, double y)
115 return is_equal<double>(x, y, PREC_double);
118 #endif // INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */