LCOV - code coverage report
Current view: top level - sal/qa/inc - valueequal.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 36 39 92.3 %
Date: 2014-11-03 Functions: 4 4 100.0 %
Legend: Lines: hit not hit

          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             : #ifndef INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
      21             : #define INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
      22             : 
      23             : #include <sal/config.h>
      24             : 
      25             : #include <math.h>
      26             : 
      27             : #include <sal/mathconf.h>
      28             : 
      29             : #define PREC_float 1
      30             : #define PREC_double 2
      31             : #define PREC_long_double 3
      32             : 
      33             : template<class T>
      34         210 : bool is_equal(T x, T y, sal_Int16 _nPrec)
      35             : {
      36         210 :     if (!(SAL_MATH_FINITE(x) && SAL_MATH_FINITE(y))) {
      37           8 :         return x == y;
      38             :     }
      39             : 
      40             :     // due to the fact that this check looks only if both values are equal
      41             :     // we only need to look on one value
      42             : 
      43             :     // 14 digits will announce the checkPrecisionSize
      44             : 
      45             :     sal_Int32 nPRECISION;
      46         202 :     switch(_nPrec)
      47             :     {
      48             :     case PREC_float:
      49          92 :         nPRECISION = 6;
      50          92 :         break;
      51             :     case PREC_double:
      52         110 :         nPRECISION = 14;
      53         110 :         break;
      54             :     case PREC_long_double:
      55           0 :         nPRECISION = 20;
      56           0 :         break;
      57             :     default:
      58           0 :         nPRECISION = 2;
      59             :     }
      60             : 
      61         202 :     if (x < 0)
      62             :     {
      63          78 :         x = -x;
      64             :     }
      65         202 :     if (y < 0)
      66             :     {
      67          78 :         y = -y;
      68             :     }
      69             : 
      70         202 :     if (_nPrec != PREC_long_double)
      71             :     {
      72         202 :         printf("double equal: %.20f\n", x);
      73         202 :         printf("              %.20f\n", y);
      74             :     }
      75             :     //here nPrecOfN is the number after dot
      76         202 :     sal_Int32 nBeforeDot = sal_Int32( log10(x) );
      77         202 :     if ( nBeforeDot < 0)
      78             :     {
      79          12 :          nBeforeDot = 0;
      80             :     }
      81             :     //printf("nPRECISION is  %d\n", nPRECISION);
      82         202 :     sal_Int32 nPrecOfN = -nPRECISION + nBeforeDot;
      83             : 
      84         202 :     if (_nPrec != PREC_long_double)
      85         202 :         printf("nPrecOfN is  %" SAL_PRIdINT32 "\n", nPrecOfN);
      86             : 
      87         202 :     double nPrec = pow(0.1, -nPrecOfN);
      88             : 
      89         202 :     if (_nPrec != PREC_long_double)
      90         202 :         printf("        prec: %.20f\n", nPrec);
      91             : 
      92         202 :     double nDelta = fabs( x - y ) ;
      93             : 
      94         202 :     if (_nPrec != PREC_long_double)
      95             :     {
      96         202 :         printf("       delta: %.20f\n", nDelta);
      97         202 :         printf("       nPrec: %.20f\n", nPrec);
      98         202 :         printf("delta must be less or equal to prec\n\n");
      99             :     }
     100             : 
     101         202 :     if (nDelta > nPrec)
     102             :     {
     103             :         // values are not equal
     104          34 :         return false;
     105             :     }
     106             : 
     107             :     // values are equal
     108         168 :     return true;
     109             : }
     110             : 
     111         100 : bool is_float_equal(float x, float y)
     112             : {
     113         100 :     return is_equal<float>(x, y, PREC_float);
     114             : }
     115         110 : bool is_double_equal(double x, double y)
     116             : {
     117         110 :     return is_equal<double>(x, y, PREC_double);
     118             : }
     119             : 
     120             : #endif // INCLUDED_SAL_QA_INC_VALUEEQUAL_HXX
     121             : 
     122             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10