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