Loading [MathJax]/extensions/tex2jax.js
Iros
 
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages Concepts
Loading...
Searching...
No Matches
midpoint.h
Go to the documentation of this file.
1#pragma once
2
4#include "di/meta/language.h"
5#include "di/types/prelude.h"
6
7namespace di::math {
8namespace detail {
10 template<concepts::Integral T>
12 constexpr auto operator()(T a, T b) const -> T {
13 // Compute the midpoint without signed overflow.
14 // The implementation is from this CppCon talk:
15 // https://m.youtube.com/watch?v=sBtAGxBh-XI
16 using U = meta::MakeUnsigned<T>;
17 int sign = 1;
18 U min = a;
19 U max = b;
20 if (b < a) {
21 sign = -1;
22 min = b;
23 max = a;
24 }
25 return a + sign * T(U(max - min) >> 1);
26 }
27
28 template<concepts::Pointer T>
30 constexpr auto operator()(T a, T b) const -> T {
31 return a + (b - a) / 2;
32 }
33 };
34}
35
37}
38
39namespace di {
40using math::midpoint;
41}
Definition language.h:370
Definition core.h:114
constexpr auto curry_back
Definition curry_back.h:141
Definition abs.h:11
Definition abs.h:10
constexpr auto midpoint
Definition midpoint.h:36
detail::MakeUnsignedHelper< RemoveCV< T > >::Type MakeUnsigned
Definition language.h:362
constexpr auto c_
A value of type Constexpr<val>.
Definition constexpr.h:252
Definition zstring_parser.h:9
constexpr auto min
Definition min.h:47
constexpr auto max
Definition max.h:47
Definition midpoint.h:9