diff options
author | Erik Schnetter <schnetter@gmail.com> | 2013-02-19 12:40:38 -0500 |
---|---|---|
committer | Erik Schnetter <schnetter@gmail.com> | 2013-02-19 12:40:38 -0500 |
commit | 771a4335c707875e53349c5292e193744e852b37 (patch) | |
tree | d04763424658795dd6f288decbe39359db80b1e0 /mathfuncs_convert.h | |
parent | b92e98bf8864e33e2531cfda5b65b1d7822e875f (diff) | |
download | vecmathlib-771a4335c707875e53349c5292e193744e852b37.zip vecmathlib-771a4335c707875e53349c5292e193744e852b37.tar.gz |
Add rint(), correct round()
Diffstat (limited to 'mathfuncs_convert.h')
-rw-r--r-- | mathfuncs_convert.h | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/mathfuncs_convert.h b/mathfuncs_convert.h index 190388f..b81e4e1 100644 --- a/mathfuncs_convert.h +++ b/mathfuncs_convert.h @@ -95,8 +95,10 @@ namespace vecmathlib { + // Round to nearest integer, breaking ties using prevailing rounding + // mode (default: round to even) template<typename realvec_t> - realvec_t mathfuncs<realvec_t>::vml_round(realvec_t x) + realvec_t mathfuncs<realvec_t>::vml_rint(realvec_t x) { realvec_t r = x; // Round by adding a large number, destroying all excess precision @@ -108,22 +110,32 @@ namespace vecmathlib { return r; } + // Round to next integer above template<typename realvec_t> realvec_t mathfuncs<realvec_t>::vml_ceil(realvec_t x) { boolvec_t iszero = x == RV(0.0); realvec_t offset = RV(0.5) - ldexp(fabs(x), I(-FP::mantissa_bits)); - return ifthen(iszero, x, round(x + offset)); + return ifthen(iszero, x, rint(x + offset)); } + // Round to next integer below template<typename realvec_t> realvec_t mathfuncs<realvec_t>::vml_floor(realvec_t x) { boolvec_t iszero = x == RV(0.0); realvec_t offset = RV(0.5) - ldexp(fabs(x), I(-FP::mantissa_bits)); - return ifthen(iszero, x, round(x - offset)); + return ifthen(iszero, x, rint(x - offset)); + } + + // Round to nearest integer, breaking ties away from zero + template<typename realvec_t> + realvec_t mathfuncs<realvec_t>::vml_round(realvec_t x) + { + return copysign(floor(fabs(x)+RV(0.5)), x); } + // Round towards zero template<typename realvec_t> realvec_t mathfuncs<realvec_t>::vml_trunc(realvec_t x) { |