Skip to content

Commit 68e01fa

Browse files
committed
v1.16.0
- add hypot function
1 parent da63ed0 commit 68e01fa

File tree

7 files changed

+176
-1
lines changed

7 files changed

+176
-1
lines changed

docs/source/api/basic_functions.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ Basic functions
3939
.. doxygenfunction:: fmod(const T1, const T2)
4040
:project: gcem
4141

42+
.. _hypot-func-ref:
43+
.. doxygenfunction:: hypot(const T1, const T2)
44+
:project: gcem
45+
4246
.. _log-function-reference:
4347
.. doxygenfunction:: log(const T)
4448
:project: gcem

docs/source/api/math_index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Mathematical functions
6161
+---------------------------------------+----------------------------------------------------+
6262
| :ref:`fmod <fmod-func-ref>` | remainder of division function |
6363
+---------------------------------------+----------------------------------------------------+
64+
| :ref:`hypot <hypot-func-ref>` | Pythagorean addition function |
65+
+---------------------------------------+----------------------------------------------------+
6466
| :ref:`log <log-function-reference>` | natural logarithm function |
6567
+---------------------------------------+----------------------------------------------------+
6668
| :ref:`log1p <log1p-func-ref>` | natural logarithm 1 plus argument function |

include/gcem.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace gcem
4646
#include "gcem_incl/min.hpp"
4747
#include "gcem_incl/sqrt.hpp"
4848
#include "gcem_incl/inv_sqrt.hpp"
49+
#include "gcem_incl/hypot.hpp"
4950

5051
#include "gcem_incl/find_exponent.hpp"
5152
#include "gcem_incl/find_fraction.hpp"

include/gcem_incl/gcem_options.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#endif
5454

5555
#ifndef GCEM_VERSION_MINOR
56-
#define GCEM_VERSION_MINOR 15
56+
#define GCEM_VERSION_MINOR 16
5757
#endif
5858

5959
#ifndef GCEM_VERSION_PATCH

include/gcem_incl/hypot.hpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*################################################################################
2+
##
3+
## Copyright (C) 2016-2022 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 Pythagorean addition function
23+
*/
24+
25+
// see: https://en.wikipedia.org/wiki/Pythagorean_addition
26+
27+
#ifndef _gcem_hypot_HPP
28+
#define _gcem_hypot_HPP
29+
30+
namespace internal
31+
{
32+
33+
template<typename T>
34+
constexpr
35+
T
36+
hypot_compute(const T x, const T ydx)
37+
noexcept
38+
{
39+
return abs(x) * sqrt( T(1) + (ydx * ydx) );
40+
}
41+
42+
template<typename T>
43+
constexpr
44+
T
45+
hypot_vals_check(const T x, const T y)
46+
noexcept
47+
{
48+
return( any_nan(x, y) ? \
49+
GCLIM<T>::quiet_NaN() :
50+
//
51+
any_inf(x,y) ? \
52+
GCLIM<T>::infinity() :
53+
// indistinguishable from zero or one
54+
GCLIM<T>::min() > abs(x) ? \
55+
abs(y) :
56+
GCLIM<T>::min() > abs(y) ? \
57+
abs(x) :
58+
// else
59+
hypot_compute(x, y/x) );
60+
}
61+
62+
template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
63+
constexpr
64+
TC
65+
hypot_type_check(const T1 x, const T2 y)
66+
noexcept
67+
{
68+
return hypot_vals_check(static_cast<TC>(x),static_cast<TC>(y));
69+
}
70+
71+
}
72+
73+
/**
74+
* Compile-time Pythagorean addition function
75+
*
76+
* @param x a real-valued input.
77+
* @param y a real-valued input.
78+
* @return Computes \f$ x \oplus y = \sqrt{x^2 + y^2} \f$.
79+
*/
80+
81+
template<typename T1, typename T2>
82+
constexpr
83+
common_return_t<T1,T2>
84+
hypot(const T1 x, const T2 y)
85+
noexcept
86+
{
87+
return internal::hypot_type_check(x,y);
88+
}
89+
90+
#endif

tests/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ fmod:
9898
gcd:
9999
$(GCEM_MAKE_CALL)
100100

101+
hypot:
102+
$(GCEM_MAKE_CALL)
103+
101104
incomplete_beta:
102105
$(GCEM_MAKE_CALL)
103106

tests/hypot.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*################################################################################
2+
##
3+
## Copyright (C) 2016-2022 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+
#define TEST_PRINT_PRECISION_1 6
22+
#define TEST_PRINT_PRECISION_2 18
23+
24+
#include "gcem_tests.hpp"
25+
26+
int main()
27+
{
28+
print_begin("hypot");
29+
30+
//
31+
32+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, 0.0L);
33+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.0L, 0.0L);
34+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, -0.0L);
35+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.0L, -0.0L);
36+
37+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.2L, 0.0L);
38+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.2L, 0.0L);
39+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.001L, 0.001L);
40+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.49L, 0.49L);
41+
42+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.5L, -0.5L);
43+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.5L, -0.5L);
44+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.5L, 0.5L);
45+
46+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 9.6L, 8.4L);
47+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, 0.0L);
48+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, 1.0L);
49+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -1.0L, 0.0L);
50+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, -1.0L);
51+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, 3.0L);
52+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -5.0L, 2.5L);
53+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -1000.0L, -0.001L);
54+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.1337L, -123456.0L);
55+
56+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_POSINF, 2.0L);
57+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 2.0L, TEST_POSINF);
58+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_POSINF, TEST_POSINF);
59+
60+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NEGINF, 2.0L);
61+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 2.0L, TEST_NEGINF);
62+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NEGINF, TEST_NEGINF);
63+
64+
//
65+
66+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NAN, 1.0L);
67+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, TEST_NAN);
68+
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NAN, TEST_NAN);
69+
70+
//
71+
72+
print_final("hypot");
73+
74+
return 0;
75+
}

0 commit comments

Comments
 (0)