floating_point.h Source File

CPP API: floating_point.h Source File
floating_point.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Daniel Abele
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_MATH_FLOATING_POINT_H
21 #define MIO_MATH_FLOATING_POINT_H
22 
23 #include "memilio/config.h"
24 
25 #include <algorithm>
26 #include <cmath>
27 #include <limits>
28 
29 namespace mio
30 {
31 
38 template <typename FP>
39 FP abs_max(FP v1, FP v2)
40 {
41  using std::abs;
42  using std::max;
43  return max<FP>(abs(v1), abs(v2));
44 }
45 
56 template <typename FP>
57 bool floating_point_equal(FP v1, FP v2, FP abs_tol = 0, FP rel_tol = std::numeric_limits<FP>::min())
58 {
59  using std::abs;
60  auto diff = abs(v1 - v2);
61  return diff <= abs_tol || diff <= abs_max<FP>(v1, v2) * rel_tol;
62 }
63 
77 template <typename FP>
78 bool floating_point_less(FP v1, FP v2, FP abs_tol = 0, FP rel_tol = std::numeric_limits<FP>::min())
79 {
80  auto diff = v1 - v2;
81  return diff < -(abs_tol + rel_tol * abs_max<FP>(v1, v2));
82 }
83 
97 template <typename FP>
98 bool floating_point_greater(FP v1, FP v2, FP abs_tol = 0, FP rel_tol = std::numeric_limits<FP>::min())
99 {
100  return floating_point_less<FP>(v2, v1, abs_tol, rel_tol);
101 }
102 
116 template <typename FP>
117 bool floating_point_less_equal(FP v1, FP v2, FP abs_tol = 0, FP rel_tol = std::numeric_limits<FP>::min())
118 {
119  return !floating_point_greater<FP>(v1, v2, abs_tol, rel_tol);
120 }
121 
135 template <typename FP>
136 bool floating_point_greater_equal(FP v1, FP v2, FP abs_tol = 0, FP rel_tol = std::numeric_limits<FP>::min())
137 {
138  return !floating_point_less<FP>(v1, v2, abs_tol, rel_tol);
139 }
140 
141 } // namespace mio
142 
143 #endif // MIO_MATH_FLOATING_POINT_H
static min_max_return_type< ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > >::type min(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &a, const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &b)
Definition: ad.hpp:2599
ad::internal::unary_intermediate< AD_TAPE_REAL, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::operations::ad_fabs< AD_TAPE_REAL > > abs(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &x1)
Definition: ad.hpp:1144
static min_max_return_type< ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 >, ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > >::type max(const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &a, const ad::internal::active_type< AD_TAPE_REAL, DATA_HANDLER_1 > &b)
Definition: ad.hpp:2596
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
bool floating_point_greater(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values with tolerances.
Definition: floating_point.h:98
bool floating_point_equal(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values for equality with tolerances.
Definition: floating_point.h:57
bool floating_point_less(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values with tolerances.
Definition: floating_point.h:78
bool floating_point_greater_equal(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values with tolerances.
Definition: floating_point.h:136
bool floating_point_less_equal(FP v1, FP v2, FP abs_tol=0, FP rel_tol=std::numeric_limits< FP >::min())
compare two floating point values with tolerances.
Definition: floating_point.h:117
FP abs_max(FP v1, FP v2)
maximum absolute value of two numbers.
Definition: floating_point.h:39