xrpld
Loading...
Searching...
No Matches
MathUtilities.h
1#pragma once
2
3#include <algorithm>
4#include <cassert>
5#include <cstddef>
6#include <cstdint>
7#include <limits>
8#include <optional>
9
10namespace xrpl {
11
16[[nodiscard]] constexpr std::optional<std::int64_t>
18{
20
21 if ((b > 0 && a > L::max() - b) || (b < 0 && a < L::min() - b))
22 return std::nullopt;
23
24 return a + b;
25}
26
31[[nodiscard]] constexpr std::optional<std::int64_t>
33{
35
36 if ((b > 0 && a < L::min() + b) || (b < 0 && a > L::max() + b))
37 return std::nullopt;
38
39 return a - b;
40}
41
46[[nodiscard]] constexpr std::optional<std::int64_t>
48{
50
51 if (a == 0 || b == 0)
52 return 0;
53
54 if (a > 0)
55 {
56 if (b > 0 ? a > L::max() / b : b < L::min() / a)
57 return std::nullopt;
58 }
59 else if (b > 0 ? a < L::min() / b : b < L::max() / a)
60 {
61 return std::nullopt;
62 }
63
64 return a * b;
65}
66
67static_assert(checkedAdd(0, 0) == 0);
68static_assert(checkedAdd(1, -1) == 0);
69static_assert(checkedAdd(-5, 2) == -3);
70static_assert(!checkedAdd(std::numeric_limits<std::int64_t>::max(), 1).has_value());
71static_assert(!checkedAdd(std::numeric_limits<std::int64_t>::min(), -1).has_value());
72static_assert(
75static_assert(
79static_assert(
83static_assert(
85 .has_value());
86static_assert(
88 .has_value());
89
90static_assert(checkedSub(0, 0) == 0);
91static_assert(checkedSub(1, 1) == 0);
92static_assert(checkedSub(-5, 2) == -7);
93static_assert(checkedSub(-5, -2) == -3);
94static_assert(!checkedSub(std::numeric_limits<std::int64_t>::min(), 1).has_value());
95static_assert(!checkedSub(std::numeric_limits<std::int64_t>::max(), -1).has_value());
96static_assert(
99static_assert(
102static_assert(
104 .has_value());
105static_assert(
107 .has_value());
108
109static_assert(checkedMul(0, std::numeric_limits<std::int64_t>::min()) == 0);
110static_assert(checkedMul(std::numeric_limits<std::int64_t>::min(), 0) == 0);
111static_assert(checkedMul(-1, -1) == 1);
112// a > 0, b > 0
113static_assert(
116static_assert(
119static_assert(!checkedMul((std::numeric_limits<std::int64_t>::max() / 2) + 1, 2).has_value());
120static_assert(!checkedMul(2, (std::numeric_limits<std::int64_t>::max() / 2) + 1).has_value());
121// a > 0, b < 0
122static_assert(
125static_assert(
128static_assert(!checkedMul((std::numeric_limits<std::int64_t>::max() / 2) + 2, -2).has_value());
129static_assert(
132static_assert(
135static_assert(!checkedMul(2, (std::numeric_limits<std::int64_t>::min() / 2) - 1).has_value());
136// a < 0, b > 0
137static_assert(
140static_assert(
143static_assert(!checkedMul((std::numeric_limits<std::int64_t>::min() / 2) - 1, 2).has_value());
144static_assert(!checkedMul(std::numeric_limits<std::int64_t>::min(), 2).has_value());
145// a < 0, b < 0
146static_assert(
149static_assert(!checkedMul(std::numeric_limits<std::int64_t>::min() / 2, -2).has_value());
150static_assert(
153static_assert(!checkedMul(std::numeric_limits<std::int64_t>::min(), -1).has_value());
154static_assert(!checkedMul(-1, std::numeric_limits<std::int64_t>::min()).has_value());
155
171constexpr std::size_t
173{
174 assert(total != 0); // NOTE No XRPL_ASSERT here, because constexpr
175 return ((std::min(count, total) * 100) + total - 1) / total;
176}
177
178// unit tests
179static_assert(calculatePercent(1, 2) == 50);
180static_assert(calculatePercent(0, 100) == 0);
181static_assert(calculatePercent(100, 100) == 100);
182static_assert(calculatePercent(200, 100) == 100);
183static_assert(calculatePercent(1, 100) == 1);
184static_assert(calculatePercent(1, 99) == 2);
185static_assert(calculatePercent(6, 14) == 43);
186static_assert(calculatePercent(29, 33) == 88);
187static_assert(calculatePercent(1, 64) == 2);
188static_assert(calculatePercent(0, 100'000'000) == 0);
189static_assert(calculatePercent(1, 100'000'000) == 1);
190static_assert(calculatePercent(50'000'000, 100'000'000) == 50);
191static_assert(calculatePercent(50'000'001, 100'000'000) == 51);
192static_assert(calculatePercent(99'999'999, 100'000'000) == 100);
193
194} // namespace xrpl
T max(T... args)
T min(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
constexpr std::optional< std::int64_t > checkedSub(std::int64_t a, std::int64_t b) noexcept
Subtract two signed 64-bit integers, returning std::nullopt when the exact mathematical difference is...
constexpr std::optional< std::int64_t > checkedAdd(std::int64_t a, std::int64_t b) noexcept
Add two signed 64-bit integers, returning std::nullopt when the exact mathematical sum is not represe...
constexpr std::size_t calculatePercent(std::size_t count, std::size_t total)
Calculate one number divided by another number in percentage.
constexpr std::optional< std::int64_t > checkedMul(std::int64_t a, std::int64_t b) noexcept
Multiply two signed 64-bit integers, returning std::nullopt when the exact mathematical product is no...