Skip to content

Commit

Permalink
Update FastRounding.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreySarnoff authored Jul 27, 2017
1 parent f397804 commit 56369a8
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/FastRounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -79,5 +106,6 @@ end
return (lo>=zero(T) || isnan(lo)) ? hi : prev_float(hi)
end
=#

end # module

0 comments on commit 56369a8

Please sign in to comment.