LCOV - code coverage report
Current view: top level - api - roundestimate.h (source / functions) Hit Total Coverage
Test: Test Coverage for xapian-core 954b5873a738 Lines: 13 16 81.2 %
Date: 2019-06-30 05:20:33 Functions: 1 1 100.0 %
Branches: 14 22 63.6 %

           Branch data     Line data    Source code
       1                 :            : /** @file roundestimate.h
       2                 :            :  * @brief Round a bounded estimate to an appropriate number of S.F.
       3                 :            :  */
       4                 :            : /* Copyright 2017 Olly Betts
       5                 :            :  *
       6                 :            :  * This program is free software; you can redistribute it and/or
       7                 :            :  * modify it under the terms of the GNU General Public License as
       8                 :            :  * published by the Free Software Foundation; either version 2 of the
       9                 :            :  * License, or (at your option) any later version.
      10                 :            :  *
      11                 :            :  * This program is distributed in the hope that it will be useful,
      12                 :            :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      13                 :            :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      14                 :            :  * GNU General Public License for more details.
      15                 :            :  *
      16                 :            :  * You should have received a copy of the GNU General Public License
      17                 :            :  * along with this program; if not, write to the Free Software
      18                 :            :  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
      19                 :            :  */
      20                 :            : 
      21                 :            : #ifndef XAPIAN_INCLUDED_ROUNDESTIMATE_H
      22                 :            : #define XAPIAN_INCLUDED_ROUNDESTIMATE_H
      23                 :            : 
      24                 :            : #include <cmath>
      25                 :            : #include "exp10.h"
      26                 :            : 
      27                 :            : /** Round a bounded estimate to an appropriate number of S.F.
      28                 :            :  *
      29                 :            :  *  The algorithm used looks at the lower and upper bound and where the
      30                 :            :  *  estimate sits between them and picks a number of significant figures
      31                 :            :  *  which doesn't suggest much more precision than there is, while still
      32                 :            :  *  providing a useful estimate.
      33                 :            :  */
      34                 :            : template<typename T>
      35                 :            : inline Xapian::doccount
      36                 :       1370 : round_estimate(T m, T M, T e)
      37                 :            : {
      38                 :            :     using namespace std;
      39                 :            : 
      40                 :       1370 :     T D = M - m;
      41 [ +  + ][ -  + ]:       1370 :     if (D == 0 || e == 0) {
      42                 :            :         // Estimate is exact or zero.  A zero but non-exact estimate can happen
      43                 :            :         // with get_mset(0, 0).
      44                 :       1162 :         return e;
      45                 :            :     }
      46                 :            : 
      47                 :        208 :     T r = T(exp10(int(log10(D))) + 0.5);
      48         [ +  + ]:        222 :     while (r > e) r /= 10;
      49                 :            : 
      50                 :        208 :     T R = e / r * r;
      51         [ +  + ]:        208 :     if (R < m) {
      52                 :          7 :         R += r;
      53         [ -  + ]:        201 :     } else if (R > M) {
      54                 :          0 :         R -= r;
      55 [ +  + ][ +  - ]:        201 :     } else if (R < e && r % 2 == 0 && e - R == r / 2) {
                 [ -  + ]
      56                 :            :         // Round towards the centre of the range.
      57         [ #  # ]:          0 :         if (e - m < M - e) {
      58                 :          0 :             R += r;
      59                 :            :         }
      60                 :            :     }
      61                 :            : 
      62                 :            :     // If it all goes pear-shaped, just stick to the original estimate.
      63 [ +  - ][ -  + ]:        208 :     if (R < m || R > M) R = e;
      64                 :        208 :     return R;
      65                 :            : }
      66                 :            : 
      67                 :            : #endif // XAPIAN_INCLUDED_ROUNDESTIMATE_H

Generated by: LCOV version 1.11