Skip to content

Commit

Permalink
add fabs, fabsf, and fabsl
Browse files Browse the repository at this point in the history
  • Loading branch information
kthohr committed Apr 28, 2024
1 parent b4244bc commit 0a4628b
Show file tree
Hide file tree
Showing 11 changed files with 335 additions and 1 deletion.
12 changes: 12 additions & 0 deletions docs/source/api/basic_functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions docs/source/api/math_index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ Mathematical functions
+---------------------------------------+----------------------------------------------------+
| :ref:`abs <abs-function-reference>` | absolute value |
+---------------------------------------+----------------------------------------------------+
| :ref:`fabs <fabs-function-reference>` | absolute value |
+---------------------------------------+----------------------------------------------------+
| :ref:`fabsf <fabsf-func-ref>` | absolute value |
+---------------------------------------+----------------------------------------------------+
| :ref:`fabsl <fabsl-func-ref>` | absolute value |
+---------------------------------------+----------------------------------------------------+
| :ref:`ceil <ceil-function-reference>` | ceiling function |
+---------------------------------------+----------------------------------------------------+
| :ref:`copysign <copysign-func-ref>` | copy sign function |
Expand Down
3 changes: 3 additions & 0 deletions include/gcem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
40 changes: 40 additions & 0 deletions include/gcem_incl/fabs.hpp
Original file line number Diff line number Diff line change
@@ -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<typename T>
constexpr
return_t<T>
fabs(const T x)
noexcept
{
return gcem::abs( static_cast<return_t<T>>(x) );
}

#endif
40 changes: 40 additions & 0 deletions include/gcem_incl/fabsf.hpp
Original file line number Diff line number Diff line change
@@ -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<typename T>
constexpr
float
fabsf(const T x)
noexcept
{
return gcem::abs( static_cast<float>(x) );
}

#endif
40 changes: 40 additions & 0 deletions include/gcem_incl/fabsl.hpp
Original file line number Diff line number Diff line change
@@ -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<typename T>
constexpr
long double
fabsl(const T x)
noexcept
{
return gcem::abs( static_cast<long double>(x) );
}

#endif
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 17
#define GCEM_VERSION_MINOR 18
#endif

#ifndef GCEM_VERSION_PATCH
Expand Down
9 changes: 9 additions & 0 deletions tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
71 changes: 71 additions & 0 deletions tests/fabs.cpp
Original file line number Diff line number Diff line change
@@ -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<float>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<float>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<float>::max());

GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<double>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<double>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<double>::max());

GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<long double>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<long double>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabs,std::fabs, std::numeric_limits<long double>::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;
}
50 changes: 50 additions & 0 deletions tests/fabsf.cpp
Original file line number Diff line number Diff line change
@@ -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<float>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits<float>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits<float>::max());

GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits<float>::quiet_NaN());
GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, std::numeric_limits<float>::infinity());
GCEM_TEST_COMPARE_VALS(gcem::fabsf,std::fabsf, -std::numeric_limits<float>::infinity());

//

print_final("fabsf");

return 0;
}
63 changes: 63 additions & 0 deletions tests/fabsl.cpp
Original file line number Diff line number Diff line change
@@ -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<double>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<double>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<double>::max());

GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<long double>::lowest());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<long double>::min());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<long double>::max());

GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<double>::quiet_NaN());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<double>::infinity());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, -std::numeric_limits<double>::infinity());

GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<long double>::quiet_NaN());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, std::numeric_limits<long double>::infinity());
GCEM_TEST_COMPARE_VALS(gcem::fabsl,std::fabsl, -std::numeric_limits<long double>::infinity());

//

print_final("fabsl");

return 0;
}

0 comments on commit 0a4628b

Please sign in to comment.