OGS
Time.h
Go to the documentation of this file.
1
11#pragma once
12
13#include <algorithm>
14#include <cmath>
15#include <compare>
16#include <limits>
17
18#include "MathLib/KahanSum.h"
19
20namespace NumLib
21{
22struct Time;
23}
24
25template <>
26struct fmt::formatter<NumLib::Time> : fmt::ostream_formatter
27{
28};
29
30namespace NumLib
31{
32struct Time
33{
34 constexpr explicit Time(double const time) : value_{time} {}
35
36 constexpr double operator()() const { return value_(); }
37
38 constexpr Time& operator+=(double const increment)
39 {
40 value_ += increment;
41 return *this;
42 }
43
44 constexpr Time operator+(double const increment) const
45 {
46 Time result = *this;
47 result += increment;
48 return result;
49 }
50
51 constexpr Time& operator-=(double const decrement)
52 {
53 value_ -= decrement;
54 return *this;
55 }
56
57 constexpr Time operator-(double const decrement) const
58 {
59 Time result = *this;
60 result -= decrement;
61 return result;
62 }
63
64 constexpr inline std::weak_ordering operator<=>(Time const& b) const
65 {
66 Time const& a = *this;
67 double const diff = b() - a();
68
69 double const eps = 10 * std::numeric_limits<double>::epsilon() *
70 std::max(1., (std::abs(a()) + std::abs(b())) / 2);
71
72 // Keep for debugging
73 // DBUG("Compare {} to {}. x is {} and y is {}. Eps {}, diff {}", a, b,
74 // x, y, eps, diff);
75
76 if (diff < -eps)
77 {
78 return std::weak_ordering::greater;
79 }
80 if (diff > eps)
81 {
82 return std::weak_ordering::less;
83 }
84 return std::weak_ordering::equivalent;
85 }
86
87 constexpr inline bool operator==(Time const& x) const
88 {
89 return (*this <=> x) == std::weak_ordering::equivalent;
90 }
91
92 friend inline std::ostream& operator<<(std::ostream& os, Time const& t)
93 {
94 auto const precision = os.precision();
95 return os << std::setprecision(
96 std::numeric_limits<double>::max_digits10)
97 << t() << std::setprecision(precision);
98 }
99
100private:
102};
103
104} // namespace NumLib
constexpr Time(double const time)
Definition Time.h:34
constexpr double operator()() const
Definition Time.h:36
constexpr bool operator==(Time const &x) const
Definition Time.h:87
friend std::ostream & operator<<(std::ostream &os, Time const &t)
Definition Time.h:92
constexpr Time & operator+=(double const increment)
Definition Time.h:38
MathLib::KahanSum value_
Definition Time.h:101
constexpr Time operator-(double const decrement) const
Definition Time.h:57
constexpr std::weak_ordering operator<=>(Time const &b) const
Definition Time.h:64
constexpr Time & operator-=(double const decrement)
Definition Time.h:51
constexpr Time operator+(double const increment) const
Definition Time.h:44