Simple-Utility v2.3.1
Loading...
Searching...
No Matches
math.hpp
Go to the documentation of this file.
1// Copyright Dominic Koepke 2019 - 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at
4// https://www.boost.org/LICENSE_1_0.txt)
5
6#ifndef SL_UTILITY_MATH_HPP
7#define SL_UTILITY_MATH_HPP
8
9#pragma once
10
11#include <cmath>
12#include <concepts>
13
14#if __cpp_lib_constexpr_cmath >= 202202L
15#define SL_CONSTEXPR_MATH constexpr
16#else
17#define SL_CONSTEXPR_MATH
18#endif
19
20namespace sl::math
21{
30 template <std::floating_point T>
32 {
34 int quo;
35 };
36
43 template <class T>
45
56 constexpr auto remquo = [](const auto x, const auto y) SL_CONSTEXPR_MATH
57 requires requires { std::remquo(x, x, nullptr); }
58 {
59 int quo{};
60 const auto remainder{std::remquo(x, y, &quo)};
61 return remquo_result{remainder, quo};
62 };
63
68 template <std::floating_point T>
70 {
72 int exp;
73 };
74
81 template <class T>
83
92 constexpr auto frexp = [](const auto value) SL_CONSTEXPR_MATH
93 requires requires { std::frexp(value, nullptr); }
94 {
95 int exp{};
96 const auto fraction{std::frexp(value, &exp)};
97 return frexp_result{fraction, exp};
98 };
99
104 template <std::floating_point T>
106 {
109 };
110
119 constexpr auto modf = []<std::floating_point T>(const T value) SL_CONSTEXPR_MATH
120 {
121 T integral{};
122 T fraction{std::modf(value, &integral)};
123 return modf_result<T>{integral, fraction};
124 };
125
127}
128
129#endif
Checks whether the given template type is usable as value type for unique_handle types.
Definition: unique_handle.hpp:161
constexpr auto frexp
Decomposes given floating point value into a normalized fraction and an integral power of two.
Definition: math.hpp:92
frexp_result(T, int) -> frexp_result< T >
Manual deduction guide for the frexp_result type.
remquo_result(T, int) -> remquo_result< T >
Manual deduction guide for the remquo_result type.
constexpr auto remquo
Computes the floating-point remainder of the division operation x/y. Additionally,...
Definition: math.hpp:56
constexpr auto modf
Decomposes given floating point value into integral and fractional parts, each having the same type a...
Definition: math.hpp:119
#define SL_CONSTEXPR_MATH
Definition: math.hpp:17
Definition: math.hpp:21
Result type for the frexp operation.
Definition: math.hpp:70
int exp
Definition: math.hpp:72
T fraction
Definition: math.hpp:71
Result type for the modf operation.
Definition: math.hpp:106
T integral
Definition: math.hpp:107
T fraction
Definition: math.hpp:108
Result type for the remquo operation.
Definition: math.hpp:32
int quo
Definition: math.hpp:34
T remainder
Definition: math.hpp:33