WPILibC++ 2024.3.2
sqrt.hpp
Go to the documentation of this file.
1/*################################################################################
2 ##
3 ## Copyright (C) 2016-2023 Keith O'Hara
4 ##
5 ## This file is part of the GCE-Math C++ library.
6 ##
7 ## Licensed under the Apache License, Version 2.0 (the "License");
8 ## you may not use this file except in compliance with the License.
9 ## You may obtain a copy of the License at
10 ##
11 ## http://www.apache.org/licenses/LICENSE-2.0
12 ##
13 ## Unless required by applicable law or agreed to in writing, software
14 ## distributed under the License is distributed on an "AS IS" BASIS,
15 ## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 ## See the License for the specific language governing permissions and
17 ## limitations under the License.
18 ##
19 ################################################################################*/
20
21/*
22 * compile-time square-root function
23 */
24
25#ifndef _gcem_sqrt_HPP
26#define _gcem_sqrt_HPP
27
28#include <cmath>
29#include <type_traits>
30
31namespace gcem
32{
33
34namespace internal
35{
36
37template<typename T>
38constexpr
39T
40sqrt_recur(const T x, const T xn, const int count)
41noexcept
42{
43 return( abs(xn - x/xn) / (T(1) + xn) < GCLIM<T>::min() ? \
44 // if
45 xn :
46 // else
48 // if
49 sqrt_recur(x, T(0.5)*(xn + x/xn), count+1) :
50 // else
51 xn );
52}
53
54template<typename T>
55constexpr
56T
57sqrt_simplify(const T x, const T m_val)
58noexcept
59{
60 return( x > T(1e+08) ? \
61 sqrt_simplify(x / T(1e+08), T(1e+04) * m_val) :
62 x > T(1e+06) ? \
63 sqrt_simplify(x / T(1e+06), T(1e+03) * m_val) :
64 x > T(1e+04) ? \
65 sqrt_simplify(x / T(1e+04), T(1e+02) * m_val) :
66 x > T(100) ? \
67 sqrt_simplify(x / T(100), T(10) * m_val) :
68 x > T(4) ? \
69 sqrt_simplify(x / T(4), T(2) * m_val) :
70 m_val * sqrt_recur(x, x / T(2), 0) );
71}
72
73template<typename T>
74constexpr
75T
76sqrt_check(const T x)
77noexcept
78{
79 return( is_nan(x) ? \
81 //
82 x < T(0) ? \
84 //
85 is_posinf(x) ? \
86 x :
87 // indistinguishable from zero or one
88 GCLIM<T>::min() > abs(x) ? \
89 T(0) :
90 GCLIM<T>::min() > abs(T(1) - x) ? \
91 x :
92 // else
93 sqrt_simplify(x, T(1)) );
94}
95
96}
97
98
99/**
100 * Compile-time square-root function
101 *
102 * @param x a real-valued input.
103 * @return Computes \f$ \sqrt{x} \f$ using a Newton-Raphson approach.
104 */
105
106template<typename T>
107constexpr
108return_t<T>
109sqrt(const T x)
110noexcept
111{
113 return internal::sqrt_check( static_cast<return_t<T>>(x) );
114 } else {
115 return std::sqrt(x);
116 }
117}
118
119}
120
121#endif
#define GCEM_SQRT_MAX_ITER
Definition: gcem_options.hpp:181
constexpr auto count() -> size_t
Definition: core.h:1203
constexpr FMT_INLINE auto is_constant_evaluated(bool default_value=false) noexcept -> bool
Definition: core.h:304
constexpr T sqrt_recur(const T x, const T xn, const int count) noexcept
Definition: sqrt.hpp:40
constexpr T sqrt_simplify(const T x, const T m_val) noexcept
Definition: sqrt.hpp:57
constexpr bool is_nan(const T x) noexcept
Definition: is_nan.hpp:39
constexpr bool is_posinf(const T x) noexcept
Definition: is_inf.hpp:84
constexpr T sqrt_check(const T x) noexcept
Definition: sqrt.hpp:76
Definition: is_even.hpp:29
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition: abs.hpp:40
constexpr return_t< T > sqrt(const T x) noexcept
Compile-time square-root function.
Definition: sqrt.hpp:109
std::numeric_limits< T > GCLIM
Definition: gcem_options.hpp:74
typename std::conditional< std::is_integral< T >::value, double, T >::type return_t
Definition: gcem_options.hpp:77
static constexpr const charge::coulomb_t e(1.6021766208e-19)
elementary charge.