Skip to content

Commit d510410

Browse files
authored
Merge pull request #14 from kthohr/rounding-feature
Add rounding features
2 parents 2c328d4 + fe97b11 commit d510410

23 files changed

+429
-22
lines changed

.appveyor.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ build_script:
5656
- .\log_binomial_coef.test
5757
- .\other.test
5858
- .\pow.test
59+
- .\rounding.test
5960
- .\sin.test
6061
- .\sinh.test
6162
- .\sqrt.test

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(GCEM_HEADERS
5252
${GCEM_INCLUDE_DIR}/gcem_incl/atanh.hpp
5353
${GCEM_INCLUDE_DIR}/gcem_incl/beta.hpp
5454
${GCEM_INCLUDE_DIR}/gcem_incl/binomial_coef.hpp
55+
${GCEM_INCLUDE_DIR}/gcem_incl/ceil.hpp
5556
${GCEM_INCLUDE_DIR}/gcem_incl/cos.hpp
5657
${GCEM_INCLUDE_DIR}/gcem_incl/cosh.hpp
5758
${GCEM_INCLUDE_DIR}/gcem_incl/erf_inv.hpp
@@ -89,13 +90,15 @@ set(GCEM_HEADERS
8990
${GCEM_INCLUDE_DIR}/gcem_incl/pow.hpp
9091
${GCEM_INCLUDE_DIR}/gcem_incl/quadrature/gauss_legendre_30.hpp
9192
${GCEM_INCLUDE_DIR}/gcem_incl/quadrature/gauss_legendre_50.hpp
93+
${GCEM_INCLUDE_DIR}/gcem_incl/round.hpp
9294
${GCEM_INCLUDE_DIR}/gcem_incl/sgn.hpp
9395
${GCEM_INCLUDE_DIR}/gcem_incl/sin.hpp
9496
${GCEM_INCLUDE_DIR}/gcem_incl/sinh.hpp
9597
${GCEM_INCLUDE_DIR}/gcem_incl/sqrt.hpp
9698
${GCEM_INCLUDE_DIR}/gcem_incl/tan.hpp
9799
${GCEM_INCLUDE_DIR}/gcem_incl/tanh.hpp
98100
${GCEM_INCLUDE_DIR}/gcem_incl/tgamma.hpp
101+
${GCEM_INCLUDE_DIR}/gcem_incl/trunc.hpp
99102
)
100103

101104
#

docs/source/api/algorithms.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

docs/source/api/basic_functions.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44
@@ -11,6 +11,10 @@ Basic functions
1111
.. doxygenfunction:: abs(const T)
1212
:project: gcem
1313

14+
.. _ceil-function-reference:
15+
.. doxygenfunction:: ceil(const T)
16+
:project: gcem
17+
1418
.. _exp-function-reference:
1519
.. doxygenfunction:: exp(const T)
1620
:project: gcem
@@ -23,6 +27,10 @@ Basic functions
2327
.. doxygenfunction:: factorial(const T)
2428
:project: gcem
2529

30+
.. _floor-func-ref:
31+
.. doxygenfunction:: floor(const T)
32+
:project: gcem
33+
2634
.. _log-function-reference:
2735
.. doxygenfunction:: log(const T)
2836
:project: gcem
@@ -43,10 +51,18 @@ Basic functions
4351
.. doxygenfunction:: pow(const T1, const T2)
4452
:project: gcem
4553

54+
.. _round-func-ref:
55+
.. doxygenfunction:: round(const T)
56+
:project: gcem
57+
4658
.. _sgn-function-reference:
4759
.. doxygenfunction:: sgn(const T)
4860
:project: gcem
4961

5062
.. _sqrt-function-reference:
5163
.. doxygenfunction:: sqrt(const T)
5264
:project: gcem
65+
66+
.. _trunc-func-ref:
67+
.. doxygenfunction:: trunc(const T)
68+
:project: gcem

docs/source/api/hyperbolic_functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

docs/source/api/math_index.rst

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44
@@ -47,12 +47,16 @@ Mathematical functions
4747
+---------------------------------------+----------------------------------------------------+
4848
| :ref:`abs <abs-function-reference>` | absolute value |
4949
+---------------------------------------+----------------------------------------------------+
50+
| :ref:`ceil <ceil-function-reference>` | ceiling function |
51+
+---------------------------------------+----------------------------------------------------+
5052
| :ref:`exp <exp-function-reference>` | exponential function |
5153
+---------------------------------------+----------------------------------------------------+
5254
| :ref:`expm1 <expm1-func-ref>` | exponential minus 1 function |
5355
+---------------------------------------+----------------------------------------------------+
5456
| :ref:`factorial <factorial-func-ref>` | factorial function |
5557
+---------------------------------------+----------------------------------------------------+
58+
| :ref:`floor <floor-func-ref>` | floor function |
59+
+---------------------------------------+----------------------------------------------------+
5660
| :ref:`log <log-function-reference>` | natural logarithm function |
5761
+---------------------------------------+----------------------------------------------------+
5862
| :ref:`log1p <log1p-func-ref>` | natural logarithm 1 plus argument function |
@@ -63,10 +67,14 @@ Mathematical functions
6367
+---------------------------------------+----------------------------------------------------+
6468
| :ref:`pow <pow-function-reference>` | power function |
6569
+---------------------------------------+----------------------------------------------------+
70+
| :ref:`round <round-func-ref>` | round function |
71+
+---------------------------------------+----------------------------------------------------+
6672
| :ref:`sgn <sgn-function-reference>` | sign function |
6773
+---------------------------------------+----------------------------------------------------+
6874
| :ref:`sqrt <sqrt-function-reference>` | square root function |
6975
+---------------------------------------+----------------------------------------------------+
76+
| :ref:`trunc <trunc-func-ref>` | truncate function |
77+
+---------------------------------------+----------------------------------------------------+
7078

7179
.. toctree::
7280

docs/source/api/special_functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

docs/source/api/trigonometric_functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

docs/source/examples.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

docs/source/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.. Copyright (c) 2016--2018 Keith O'Hara
1+
.. Copyright (c) 2016-2019 Keith O'Hara
22
33
Distributed under the terms of the Apache License, Version 2.0.
44

include/gcem.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ namespace gcem
3232
#include "gcem_incl/is_finite.hpp"
3333

3434
#include "gcem_incl/abs.hpp"
35+
#include "gcem_incl/ceil.hpp"
3536
#include "gcem_incl/floor.hpp"
37+
#include "gcem_incl/trunc.hpp"
3638
#include "gcem_incl/is_odd.hpp"
3739
#include "gcem_incl/is_even.hpp"
3840
#include "gcem_incl/max.hpp"
@@ -46,6 +48,7 @@ namespace gcem
4648
#include "gcem_incl/find_fraction.hpp"
4749
#include "gcem_incl/find_whole.hpp"
4850
#include "gcem_incl/mantissa.hpp"
51+
#include "gcem_incl/round.hpp"
4952

5053
#include "gcem_incl/pow_integral.hpp"
5154
#include "gcem_incl/exp.hpp"

include/gcem_incl/ceil.hpp

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*################################################################################
2+
##
3+
## Copyright (C) 2016-2019 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+
#ifndef _gcem_ceil_HPP
22+
#define _gcem_ceil_HPP
23+
24+
namespace internal
25+
{
26+
27+
template<typename T>
28+
constexpr
29+
int
30+
ceil_resid(const T x, const T x_whole)
31+
noexcept
32+
{
33+
return( (x > T(0)) && (x > x_whole) );
34+
}
35+
36+
template<typename T>
37+
constexpr
38+
T
39+
ceil_int(const T x, const T x_whole)
40+
noexcept
41+
{
42+
return( x_whole + static_cast<T>(ceil_resid(x,x_whole)) );
43+
}
44+
45+
template<typename T>
46+
constexpr
47+
T
48+
ceil_check(const T x)
49+
noexcept
50+
{
51+
return( // NaN check
52+
is_nan(x) ? \
53+
GCLIM<T>::quiet_NaN() :
54+
// +/- infinite
55+
!is_finite(x) ? \
56+
x :
57+
// signed-zero cases
58+
GCLIM<T>::epsilon() > abs(x) ? \
59+
x :
60+
// else
61+
ceil_int(x, T(static_cast<llint_t>(x))) );
62+
}
63+
64+
}
65+
66+
/**
67+
* Compile-time ceil function
68+
*
69+
* @param x a real-valued input.
70+
* @return computes the ceiling-value of the input.
71+
*/
72+
73+
template<typename T>
74+
constexpr
75+
return_t<T>
76+
ceil(const T x)
77+
noexcept
78+
{
79+
return internal::ceil_check( static_cast<return_t<T>>(x) );
80+
}
81+
82+
#endif

include/gcem_incl/exp.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,13 @@ noexcept
7070
return( is_nan(x) ? \
7171
GCLIM<T>::quiet_NaN() :
7272
//
73-
x == - GCLIM<T>::infinity() ? \
73+
is_neginf(x) ? \
7474
T(0) :
7575
//
7676
GCLIM<T>::epsilon() > abs(x) ? \
7777
T(1) :
7878
//
79-
x == GCLIM<T>::infinity() ? \
79+
is_posinf(x) ? \
8080
GCLIM<T>::infinity() :
8181
//
8282
abs(x) < T(2) ? \

include/gcem_incl/find_fraction.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
################################################################################*/
2020

2121
/*
22-
* find the fraction part of x = n + r, where -0.5 < r < 0.5
22+
* find the fraction part of x = n + r, where -0.5 <= r <= 0.5
2323
*/
2424

2525
#ifndef _gcem_find_fraction_HPP
@@ -34,11 +34,11 @@ T
3434
find_fraction(const T x)
3535
noexcept
3636
{
37-
return( abs(x - internal::floor(x)) > T(0.5) ? \
37+
return( abs(x - internal::floor_check(x)) >= T(0.5) ? \
3838
// if
39-
x - internal::floor(x) - sgn(x) :
39+
x - internal::floor_check(x) - sgn(x) :
4040
//else
41-
x - internal::floor(x) );
41+
x - internal::floor_check(x) );
4242
}
4343

4444
}

include/gcem_incl/find_whole.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
################################################################################*/
2020

2121
/*
22-
* find the whole number part of x = n + r, where -0.5 < r < 0.5
22+
* find the whole number part of x = n + r, where -0.5 <= r <= 0.5
2323
*/
2424

2525
#ifndef _gcem_find_whole_HPP
@@ -34,11 +34,11 @@ llint_t
3434
find_whole(const T x)
3535
noexcept
3636
{
37-
return( abs(x - internal::floor(x)) > T(0.5) ? \
37+
return( abs(x - internal::floor_check(x)) >= T(0.5) ? \
3838
// if
39-
static_cast<llint_t>(internal::floor(x) + sgn(x)) :
39+
static_cast<llint_t>(internal::floor_check(x) + sgn(x)) :
4040
// else
41-
static_cast<llint_t>(internal::floor(x)) );
41+
static_cast<llint_t>(internal::floor_check(x)) );
4242
}
4343

4444
}

include/gcem_incl/floor.hpp

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,59 @@
2424
namespace internal
2525
{
2626

27+
template<typename T>
28+
constexpr
29+
int
30+
floor_resid(const T x, const T x_whole)
31+
noexcept
32+
{
33+
return( (x < T(0)) && (x < x_whole) );
34+
}
35+
2736
template<typename T>
2837
constexpr
2938
T
30-
floor(const T x)
39+
floor_int(const T x, const T x_whole)
40+
noexcept
41+
{
42+
return( x_whole - static_cast<T>(floor_resid(x,x_whole)) );
43+
}
44+
45+
template<typename T>
46+
constexpr
47+
T
48+
floor_check(const T x)
3149
noexcept
3250
{
33-
return T(static_cast<llint_t>(x));
51+
return( // NaN check
52+
is_nan(x) ? \
53+
GCLIM<T>::quiet_NaN() :
54+
// +/- infinite
55+
!is_finite(x) ? \
56+
x :
57+
// signed-zero cases
58+
GCLIM<T>::epsilon() > abs(x) ? \
59+
x :
60+
// else
61+
floor_int(x, T(static_cast<llint_t>(x))) );
3462
}
3563

3664
}
3765

66+
/**
67+
* Compile-time floor function
68+
*
69+
* @param x a real-valued input.
70+
* @return computes the floor-value of the input.
71+
*/
72+
73+
template<typename T>
74+
constexpr
75+
return_t<T>
76+
floor(const T x)
77+
noexcept
78+
{
79+
return internal::floor_check( static_cast<return_t<T>>(x) );
80+
}
81+
3882
#endif

0 commit comments

Comments
 (0)