Skip to content

Commit 2776bd8

Browse files
authored
Fix overflow in Float --> Normed{T,f} conversions where f == 1 (#201)
1 parent c5909c8 commit 2776bd8

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

src/normed.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ function _convert(::Type{N}, x::Tf) where {T, f, N <: Normed{T,f}, Tf <: Union{F
7676
0 <= x <= Tf((typemax(T)-rawone(N))/rawone(N)+1) || throw_converterror(N, x)
7777
end
7878

79+
f == 1 && x == Tf(typemax(N)) && return typemax(N)
7980
if f <= (significand_bits(Tf) + 1) && bitwidth(T) < significand_bits(Tf)
8081
return reinterpret(N, unsafe_trunc(T, round(rawone(N) * x)))
8182
end

test/normed.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ end
158158
if isinf(input_typemax)
159159
@test reinterpret(N(floatmax(Tf))) >= round(T, floatmax(Tf))
160160
else
161-
@test reinterpret(N(input_typemax)) >= (typemax(T)>>1) # overflow check
161+
@test reinterpret(N(input_typemax)) > (typemax(T)>>1) # overflow check
162+
@test N(input_typemax) >= N(prevfloat(input_typemax))
162163
end
163164

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

0 commit comments

Comments
 (0)