Skip to content

Commit

Permalink
v1.16.0
Browse files Browse the repository at this point in the history
- add hypot function
  • Loading branch information
kthohr committed Aug 27, 2022
1 parent da63ed0 commit 68e01fa
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 1 deletion.
4 changes: 4 additions & 0 deletions docs/source/api/basic_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ Basic functions
.. doxygenfunction:: fmod(const T1, const T2)
:project: gcem

.. _hypot-func-ref:
.. doxygenfunction:: hypot(const T1, const T2)
:project: gcem

.. _log-function-reference:
.. doxygenfunction:: log(const T)
:project: gcem
Expand Down
2 changes: 2 additions & 0 deletions docs/source/api/math_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Mathematical functions
+---------------------------------------+----------------------------------------------------+
| :ref:`fmod <fmod-func-ref>` | remainder of division function |
+---------------------------------------+----------------------------------------------------+
| :ref:`hypot <hypot-func-ref>` | Pythagorean addition function |
+---------------------------------------+----------------------------------------------------+
| :ref:`log <log-function-reference>` | natural logarithm function |
+---------------------------------------+----------------------------------------------------+
| :ref:`log1p <log1p-func-ref>` | natural logarithm 1 plus argument function |
Expand Down
1 change: 1 addition & 0 deletions include/gcem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace gcem
#include "gcem_incl/min.hpp"
#include "gcem_incl/sqrt.hpp"
#include "gcem_incl/inv_sqrt.hpp"
#include "gcem_incl/hypot.hpp"

#include "gcem_incl/find_exponent.hpp"
#include "gcem_incl/find_fraction.hpp"
Expand Down
2 changes: 1 addition & 1 deletion include/gcem_incl/gcem_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
#endif

#ifndef GCEM_VERSION_MINOR
#define GCEM_VERSION_MINOR 15
#define GCEM_VERSION_MINOR 16
#endif

#ifndef GCEM_VERSION_PATCH
Expand Down
90 changes: 90 additions & 0 deletions include/gcem_incl/hypot.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*################################################################################
##
## Copyright (C) 2016-2022 Keith O'Hara
##
## This file is part of the GCE-Math C++ library.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
################################################################################*/

/*
* compile-time Pythagorean addition function
*/

// see: https://en.wikipedia.org/wiki/Pythagorean_addition

#ifndef _gcem_hypot_HPP
#define _gcem_hypot_HPP

namespace internal
{

template<typename T>
constexpr
T
hypot_compute(const T x, const T ydx)
noexcept
{
return abs(x) * sqrt( T(1) + (ydx * ydx) );
}

template<typename T>
constexpr
T
hypot_vals_check(const T x, const T y)
noexcept
{
return( any_nan(x, y) ? \
GCLIM<T>::quiet_NaN() :
//
any_inf(x,y) ? \
GCLIM<T>::infinity() :
// indistinguishable from zero or one
GCLIM<T>::min() > abs(x) ? \
abs(y) :
GCLIM<T>::min() > abs(y) ? \
abs(x) :
// else
hypot_compute(x, y/x) );
}

template<typename T1, typename T2, typename TC = common_return_t<T1,T2>>
constexpr
TC
hypot_type_check(const T1 x, const T2 y)
noexcept
{
return hypot_vals_check(static_cast<TC>(x),static_cast<TC>(y));
}

}

/**
* Compile-time Pythagorean addition function
*
* @param x a real-valued input.
* @param y a real-valued input.
* @return Computes \f$ x \oplus y = \sqrt{x^2 + y^2} \f$.
*/

template<typename T1, typename T2>
constexpr
common_return_t<T1,T2>
hypot(const T1 x, const T2 y)
noexcept
{
return internal::hypot_type_check(x,y);
}

#endif
3 changes: 3 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ fmod:
gcd:
$(GCEM_MAKE_CALL)

hypot:
$(GCEM_MAKE_CALL)

incomplete_beta:
$(GCEM_MAKE_CALL)

Expand Down
75 changes: 75 additions & 0 deletions tests/hypot.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*################################################################################
##
## Copyright (C) 2016-2022 Keith O'Hara
##
## This file is part of the GCE-Math C++ library.
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
################################################################################*/

#define TEST_PRINT_PRECISION_1 6
#define TEST_PRINT_PRECISION_2 18

#include "gcem_tests.hpp"

int main()
{
print_begin("hypot");

//

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.0L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, -0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.0L, -0.0L);

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.2L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.2L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.001L, 0.001L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.49L, 0.49L);

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.5L, -0.5L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.5L, -0.5L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -0.5L, 0.5L);

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 9.6L, 8.4L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, 1.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -1.0L, 0.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.0L, -1.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, 3.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -5.0L, 2.5L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, -1000.0L, -0.001L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 0.1337L, -123456.0L);

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_POSINF, 2.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 2.0L, TEST_POSINF);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_POSINF, TEST_POSINF);

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NEGINF, 2.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 2.0L, TEST_NEGINF);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NEGINF, TEST_NEGINF);

//

GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NAN, 1.0L);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, 1.0L, TEST_NAN);
GCEM_TEST_COMPARE_VALS(gcem::hypot,std::hypot, TEST_NAN, TEST_NAN);

//

print_final("hypot");

return 0;
}

0 comments on commit 68e01fa

Please sign in to comment.