From 56369a8f4e2a4af683eb5471ea2c802f7b7e0714 Mon Sep 17 00:00:00 2001 From: Jeffrey Sarnoff Date: Thu, 27 Jul 2017 06:42:16 -0400 Subject: [PATCH] Update FastRounding.jl --- src/FastRounding.jl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/FastRounding.jl b/src/FastRounding.jl index d700c54..bbabe9b 100644 --- a/src/FastRounding.jl +++ b/src/FastRounding.jl @@ -59,6 +59,33 @@ sqrt_round{T<:SysFloat}(a::T) = sqrt(a) and quick, accurate float adjacency value calculation. =# +@inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:Nearest})::T + !isinf(hi) && return hi + return signbit(hi) ? T(-Inf) : T(Inf) +end + +@inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:ToZero})::T + !isinf(hi) && return signbit(hi) != signbit(lo) ? AdjacentFloats.next_nearerto_zero(hi) : hi + return signbit(hi) ? nextfloat(T(-Inf)) : prevfloat(T(Inf)) +end + +@inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:FromZero})::T + !isinf(hi) && return signbit(hi) == signbit(lo) ? AdjacentFloats.next_awayfrom_zero(hi) : hi + return signbit(hi) ? T(-Inf) : T(Inf) +end + +@inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:Up})::T + !isinf(hi) && return (lo<=zero(T) || isnan(lo)) ? hi : next_float(hi) + return signbit(hi) ? nextfloat(T(-Inf)) : T(Inf) +end + +@inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:Down})::T + !isinf(hi) && return (lo>=zero(T) || isnan(lo)) ? hi : prev_float(hi) + return signbit(hi) ? T(-Inf) : prevfloat(T(Inf)) +end + +#= prior (reasonable, conflicts with interval rounding on Infinities) + @inline function round_errorfree{T<:SysFloat}(hi::T, lo::T, ::RoundingMode{:Nearest})::T return hi end @@ -79,5 +106,6 @@ end return (lo>=zero(T) || isnan(lo)) ? hi : prev_float(hi) end +=# end # module