From 0a4628be10707aea78547354a5142645042b969f Mon Sep 17 00:00:00 2001 From: Keith O'Hara Date: Sun, 28 Apr 2024 11:16:10 -0400 Subject: [PATCH] add fabs, fabsf, and fabsl --- docs/source/api/basic_functions.rst | 12 +++++ docs/source/api/math_index.rst | 6 +++ include/gcem.hpp | 3 ++ include/gcem_incl/fabs.hpp | 40 ++++++++++++++++ include/gcem_incl/fabsf.hpp | 40 ++++++++++++++++ include/gcem_incl/fabsl.hpp | 40 ++++++++++++++++ include/gcem_incl/gcem_options.hpp | 2 +- tests/Makefile | 9 ++++ tests/fabs.cpp | 71 +++++++++++++++++++++++++++++ tests/fabsf.cpp | 50 ++++++++++++++++++++ tests/fabsl.cpp | 63 +++++++++++++++++++++++++ 11 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 include/gcem_incl/fabs.hpp create mode 100644 include/gcem_incl/fabsf.hpp create mode 100644 include/gcem_incl/fabsl.hpp create mode 100644 tests/fabs.cpp create mode 100644 tests/fabsf.cpp create mode 100644 tests/fabsl.cpp diff --git a/docs/source/api/basic_functions.rst b/docs/source/api/basic_functions.rst index 9a17e12..0143834 100644 --- a/docs/source/api/basic_functions.rst +++ b/docs/source/api/basic_functions.rst @@ -11,6 +11,18 @@ Basic functions .. doxygenfunction:: abs(const T) :project: gcem +.. _fabs-function-reference: +.. doxygenfunction:: fabs(const T) + :project: gcem + +.. _fabsf-func-ref: +.. doxygenfunction:: fabsf(const T) + :project: gcem + +.. _fabsl-func-ref: +.. doxygenfunction:: fabsl(const T) + :project: gcem + .. _ceil-function-reference: .. doxygenfunction:: ceil(const T) :project: gcem diff --git a/docs/source/api/math_index.rst b/docs/source/api/math_index.rst index d7a0280..24ce6cf 100644 --- a/docs/source/api/math_index.rst +++ b/docs/source/api/math_index.rst @@ -47,6 +47,12 @@ Mathematical functions +---------------------------------------+----------------------------------------------------+ | :ref:`abs ` | absolute value | +---------------------------------------+----------------------------------------------------+ +| :ref:`fabs ` | absolute value | ++---------------------------------------+----------------------------------------------------+ +| :ref:`fabsf ` | absolute value | ++---------------------------------------+----------------------------------------------------+ +| :ref:`fabsl ` | absolute value | ++---------------------------------------+----------------------------------------------------+ | :ref:`ceil ` | ceiling function | +---------------------------------------+----------------------------------------------------+ | :ref:`copysign ` | copy sign function | diff --git a/include/gcem.hpp b/include/gcem.hpp index 990738d..4da27db 100644 --- a/include/gcem.hpp +++ b/include/gcem.hpp @@ -37,6 +37,9 @@ namespace gcem #include "gcem_incl/sgn.hpp" #include "gcem_incl/abs.hpp" + #include "gcem_incl/fabs.hpp" + #include "gcem_incl/fabsf.hpp" + #include "gcem_incl/fabsl.hpp" #include "gcem_incl/ceil.hpp" #include "gcem_incl/floor.hpp" #include "gcem_incl/trunc.hpp" diff --git a/include/gcem_incl/fabs.hpp b/include/gcem_incl/fabs.hpp new file mode 100644 index 0000000..58db046 --- /dev/null +++ b/include/gcem_incl/fabs.hpp @@ -0,0 +1,40 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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. + ## + ################################################################################*/ + +#ifndef _gcem_fabs_HPP +#define _gcem_fabs_HPP + +/** + * Compile-time floating-point absolute value function + * + * @param x a real-valued input. + * @return the absolute value of \c x, \f$ |x| \f$, where the return type is a floating point number (float, double, or long double). + */ + +template +constexpr +return_t +fabs(const T x) +noexcept +{ + return gcem::abs( static_cast>(x) ); +} + +#endif diff --git a/include/gcem_incl/fabsf.hpp b/include/gcem_incl/fabsf.hpp new file mode 100644 index 0000000..9377b0c --- /dev/null +++ b/include/gcem_incl/fabsf.hpp @@ -0,0 +1,40 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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. + ## + ################################################################################*/ + +#ifndef _gcem_fabsf_HPP +#define _gcem_fabsf_HPP + +/** + * Compile-time floating-point absolute value function + * + * @param x a real-valued input. + * @return the absolute value of \c x, \f$ |x| \f$, where the return type is a floating point number (float only). + */ + +template +constexpr +float +fabsf(const T x) +noexcept +{ + return gcem::abs( static_cast(x) ); +} + +#endif diff --git a/include/gcem_incl/fabsl.hpp b/include/gcem_incl/fabsl.hpp new file mode 100644 index 0000000..99aae95 --- /dev/null +++ b/include/gcem_incl/fabsl.hpp @@ -0,0 +1,40 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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. + ## + ################################################################################*/ + +#ifndef _gcem_fabsl_HPP +#define _gcem_fabsl_HPP + +/** + * Compile-time floating-point absolute value function + * + * @param x a real-valued input. + * @return the absolute value of \c x, \f$ |x| \f$, where the return type is a floating point number (long double only). + */ + +template +constexpr +long double +fabsl(const T x) +noexcept +{ + return gcem::abs( static_cast(x) ); +} + +#endif diff --git a/include/gcem_incl/gcem_options.hpp b/include/gcem_incl/gcem_options.hpp index 4a3e666..1e988c3 100644 --- a/include/gcem_incl/gcem_options.hpp +++ b/include/gcem_incl/gcem_options.hpp @@ -53,7 +53,7 @@ #endif #ifndef GCEM_VERSION_MINOR - #define GCEM_VERSION_MINOR 17 + #define GCEM_VERSION_MINOR 18 #endif #ifndef GCEM_VERSION_PATCH diff --git a/tests/Makefile b/tests/Makefile index f67cf5d..57913d4 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -89,6 +89,15 @@ exp: expm1: $(GCEM_MAKE_CALL) +fabs: + $(GCEM_MAKE_CALL) + +fabsf: + $(GCEM_MAKE_CALL) + +fabsl: + $(GCEM_MAKE_CALL) + factorial: $(GCEM_MAKE_CALL) diff --git a/tests/fabs.cpp b/tests/fabs.cpp new file mode 100644 index 0000000..4a6bfd4 --- /dev/null +++ b/tests/fabs.cpp @@ -0,0 +1,71 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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("fabs"); + + // + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 0.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-0.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 1.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-1.0f); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 0.0); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-0.0); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 1.0); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-1.0); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 0); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 1); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-1); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 0.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-0.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, 1.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs,-1.0L); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, TEST_NAN); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, TEST_NEGINF); + GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, TEST_POSINF); + + // + + print_final("fabs"); + + return 0; +} diff --git a/tests/fabsf.cpp b/tests/fabsf.cpp new file mode 100644 index 0000000..53e5862 --- /dev/null +++ b/tests/fabsf.cpp @@ -0,0 +1,50 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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("fabsf"); + + // + + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, 0.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf,-0.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, 1.0f); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf,-1.0f); + + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits::quiet_NaN()); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits::infinity()); + GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, -std::numeric_limits::infinity()); + + // + + print_final("fabsf"); + + return 0; +} diff --git a/tests/fabsl.cpp b/tests/fabsl.cpp new file mode 100644 index 0000000..e53d58c --- /dev/null +++ b/tests/fabsl.cpp @@ -0,0 +1,63 @@ +/*################################################################################ + ## + ## Copyright (C) 2016-2024 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("fabsl"); + + // + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, 0.0); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl,-0.0); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, 1.0); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl,-1.0); + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, 0.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl,-0.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, 1.0L); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl,-1.0L); + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::lowest()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::min()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::max()); + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::quiet_NaN()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::infinity()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, -std::numeric_limits::infinity()); + + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::quiet_NaN()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits::infinity()); + GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, -std::numeric_limits::infinity()); + + // + + print_final("fabsl"); + + return 0; +}