dimval 0.2.0
Modern C++23 header-only library of dimensional values (units, measures, ranges)
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1#pragma once
2
10
11#include <dimval/measure.hpp>
12#include <dimval/range.hpp>
13#include <dimval/traits.hpp>
14#include <dimval/unit.hpp>
15
16namespace dimval {
17
18template <UnitLike U, NumericValue T>
19[[nodiscard]] constexpr UnitValue<U, T> abs(const UnitValue<U, T>& v) noexcept {
20 return UnitValue<U, T>{v.v < T{} ? static_cast<T>(-v.v) : v.v};
21}
22
23template <UnitLike U, NumericValue T>
24[[nodiscard]] constexpr UnitValue<U, T> min(const UnitValue<U, T>& a,
25 const UnitValue<U, T>& b) noexcept {
26 return a.v < b.v ? a : b;
27}
28
29template <UnitLike U, NumericValue T>
30[[nodiscard]] constexpr UnitValue<U, T> max(const UnitValue<U, T>& a,
31 const UnitValue<U, T>& b) noexcept {
32 return a.v < b.v ? b : a;
33}
34
35template <UnitLike U, NumericValue T>
36[[nodiscard]] constexpr UnitValue<U, T>
37clamp(const UnitValue<U, T>& v, const UnitValue<U, T>& lo, const UnitValue<U, T>& hi) noexcept {
38 if (v.v < lo.v)
39 return lo;
40 if (hi.v < v.v)
41 return hi;
42 return v;
43}
44
45template <MeasureLike M, NumericValue T>
46[[nodiscard]] constexpr MeasureValue<M, T> abs(const MeasureValue<M, T>& v) noexcept {
47 return MeasureValue<M, T>{v.v < T{} ? static_cast<T>(-v.v) : v.v};
48}
49
50template <MeasureLike M, NumericValue T>
51[[nodiscard]] constexpr MeasureValue<M, T> min(const MeasureValue<M, T>& a,
52 const MeasureValue<M, T>& b) noexcept {
53 return a.v < b.v ? a : b;
54}
55
56template <MeasureLike M, NumericValue T>
57[[nodiscard]] constexpr MeasureValue<M, T> max(const MeasureValue<M, T>& a,
58 const MeasureValue<M, T>& b) noexcept {
59 return a.v < b.v ? b : a;
60}
61
62template <MeasureLike M, NumericValue T>
63[[nodiscard]] constexpr MeasureValue<M, T> clamp(const MeasureValue<M, T>& v,
64 const MeasureValue<M, T>& lo,
65 const MeasureValue<M, T>& hi) noexcept {
66 if (v.v < lo.v)
67 return lo;
68 if (hi.v < v.v)
69 return hi;
70 return v;
71}
72
75template <UnitLike U, NumericValue T>
76[[nodiscard]] constexpr UnitValue<U, T> midpoint(const UnitRangeValue<U, T>& r) noexcept {
77 return UnitValue<U, T>{static_cast<T>((r.min().v + r.max().v) / T{2})};
78}
79
80template <MeasureLike M, NumericValue T>
81[[nodiscard]] constexpr MeasureValue<M, T> midpoint(const MeasureRangeValue<M, T>& r) noexcept {
82 return MeasureValue<M, T>{static_cast<T>((r.min().v + r.max().v) / T{2})};
83}
84
85} // namespace dimval
Closed/open interval over a UnitValue<U,T>.
Definition range.hpp:45
constexpr UnitValue< U, T > midpoint(const UnitRangeValue< U, T > &r) noexcept
Midpoint of a range's bounds.
Definition math.hpp:76
MeasureValue<M,T>: a UnitValue tagged with an additional semantic measure.
UnitRangeValue and MeasureRangeValue — closed/open intervals of dimensional values.
A value paired at the type level with a unit tag.
Definition unit.hpp:36
Concepts and helper accessors that drive the static side of dimval.
UnitValue<U,T>: a strongly-typed value carrying a compile-time unit tag.