Skip to content

Commit

Permalink
Fix overflow in Float --> Normed{T,f} conversions where f == 1 (#201
Browse files Browse the repository at this point in the history
)
  • Loading branch information
kimikage authored Jul 21, 2020
1 parent c5909c8 commit 2776bd8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ function _convert(::Type{N}, x::Tf) where {T, f, N <: Normed{T,f}, Tf <: Union{F
0 <= x <= Tf((typemax(T)-rawone(N))/rawone(N)+1) || throw_converterror(N, x)
end

f == 1 && x == Tf(typemax(N)) && return typemax(N)
if f <= (significand_bits(Tf) + 1) && bitwidth(T) < significand_bits(Tf)
return reinterpret(N, unsafe_trunc(T, round(rawone(N) * x)))
end
Expand Down
3 changes: 2 additions & 1 deletion test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ end
if isinf(input_typemax)
@test reinterpret(N(floatmax(Tf))) >= round(T, floatmax(Tf))
else
@test reinterpret(N(input_typemax)) >= (typemax(T)>>1) # overflow check
@test reinterpret(N(input_typemax)) > (typemax(T)>>1) # overflow check
@test N(input_typemax) >= N(prevfloat(input_typemax))
end

input_upper = Tf(BigFloat(typemax(T)) / r, RoundDown)
Expand Down

0 comments on commit 2776bd8

Please sign in to comment.