-
Notifications
You must be signed in to change notification settings - Fork 102
Expand file tree
/
Copy pathRiaNumericalTools.cpp
More file actions
120 lines (99 loc) · 4.33 KB
/
Copy pathRiaNumericalTools.cpp
File metadata and controls
120 lines (99 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2022 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaNumericalTools.h"
#include "cvfMath.h"
#include <algorithm>
#include <cmath>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::roundToClosestPowerOfTenCeil( double value )
{
auto exponent = computeTenExponentCeil( value );
return pow( 10.0, exponent );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::roundToClosestPowerOfTenFloor( double value )
{
auto exponent = computeTenExponentFloor( value );
return pow( 10.0, exponent );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::computeTenExponentCeil( double value )
{
if ( value < 0.0 ) return 0.0;
double logDecValueMax = log10( value );
logDecValueMax = cvf::Math::ceil( logDecValueMax );
return logDecValueMax;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::computeTenExponentFloor( double value )
{
if ( value < 0.0 ) return 0.0;
double logDecValueMin = log10( value );
logDecValueMin = cvf::Math::floor( logDecValueMin );
return logDecValueMin;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::roundToNumSignificantDigitsFloor( double value, double numSignificantDigits )
{
return roundToNumSignificantDigits( value, numSignificantDigits, RoundToSignificantDigitsMode::FLOOR );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::roundToNumSignificantDigitsCeil( double value, double numSignificantDigits )
{
return roundToNumSignificantDigits( value, numSignificantDigits, RoundToSignificantDigitsMode::CEIL );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaNumericalTools::roundToNumSignificantDigits( double value, double numSignificantDigits, RoundToSignificantDigitsMode mode )
{
double absoluteValue = cvf::Math::abs( value );
if ( absoluteValue == 0.0 )
{
return 0.0;
}
double logDecValue = log10( absoluteValue );
logDecValue = cvf::Math::ceil( logDecValue );
double factor = pow( 10.0, numSignificantDigits - logDecValue );
double tmp = value * factor;
double integerPart = 0.0;
modf( tmp, &integerPart );
double candidateValue = integerPart / factor;
if ( mode == RoundToSignificantDigitsMode::CEIL && candidateValue < value )
{
integerPart++;
}
else if ( mode == RoundToSignificantDigitsMode::FLOOR && value < candidateValue )
{
integerPart--;
}
double roundedValue = integerPart / factor;
return roundedValue;
}