Skip to content

Commit 33f2583

Browse files
authored
Improve throw_converterror (#205)
This uses the type aliases for the message and avoids showing huge numbers. This also reduces the compilation time, i.e. the CI time.
1 parent 55ee461 commit 33f2583

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/FixedPointNumbers.jl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ function show(io::IO, x::FixedPoint{T,f}) where {T,f}
296296
end
297297
end
298298

299-
if VERSION < v"1.6.0-DEV.356"
299+
if VERSION < v"1.6.0-DEV.356" # JuliaLang/julia#36107
300300
function Base.showarg(io::IO, a::Array{X}, toplevel) where {X<:FixedPoint}
301301
toplevel || print(io, "::")
302302
print(io, "Array{")
@@ -340,13 +340,17 @@ scaledual(::Type{Tdual}, x::FixedPoint) where Tdual = convert(Tdual, 1/rawone(x)
340340
scaledual(::Type{Tdual}, x::AbstractArray{T}) where {Tdual, T <: FixedPoint} =
341341
convert(Tdual, 1/rawone(T)), reinterpret(rawtype(T), x)
342342

343-
@noinline function throw_converterror(::Type{X}, x) where {X <: FixedPoint}
344-
n = 2^bitwidth(X)
345-
bitstring = bitwidth(X) == 8 ? "an 8-bit" : "a $(bitwidth(X))-bit"
343+
@noinline function throw_converterror(::Type{X}, @nospecialize(x)) where X <: FixedPoint
344+
nbits = bitwidth(rawtype(X))
346345
io = IOBuffer()
347-
show(IOContext(io, :compact=>true), typemin(X)); Xmin = String(take!(io))
348-
show(IOContext(io, :compact=>true), typemax(X)); Xmax = String(take!(io))
349-
throw(ArgumentError("$X is $bitstring type representing $n values from $Xmin to $Xmax; cannot represent $x"))
346+
showtype(io, X)
347+
print(io, " is ")
348+
print(io, nbits == 8 ? "an " : "a ", nbits, "-bit type representing ")
349+
print(io, nbits <= 16 ? string(2^nbits) : "2^$nbits", " values from ")
350+
print(IOContext(io, :compact=>true), typemin(X), " to ")
351+
print(IOContext(io, :compact=>true), typemax(X), "; ")
352+
print(io, "cannot represent ", x)
353+
throw(ArgumentError(String(take!(io))))
350354
end
351355

352356
rand(::Type{T}) where {T <: FixedPoint} = reinterpret(T, rand(rawtype(T)))

test/fixed.jl

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,25 @@ end
9090
@test_throws ArgumentError one(Q0f15)
9191
@test_throws ArgumentError oneunit(Q0f31)
9292
@test_throws ArgumentError one(Fixed{Int8,8}) # TODO: remove this at end of its support
93+
94+
@test_throws ArgumentError convert(Q0f7, 0.999)
95+
@test_throws ArgumentError convert(Q0f7, 1.0)
96+
@test_throws ArgumentError convert(Q0f7, 1)
97+
@test_throws ArgumentError convert(Q0f7, 2)
98+
99+
ret = @test_throws ArgumentError Q0f7(127)
100+
msg = ret.value.msg
101+
@test occursin("Q0f7 is an 8-bit type representing 256 values from -1.0 to 0.992;", msg)
102+
ret = @test_throws ArgumentError convert(Fixed{Int128,100}, 10.0^9)
103+
msg = ret.value.msg
104+
@test occursin("Fixed{Int128,100} is a 128-bit type representing 2^128 values", msg)
93105
end
94106

95107
@testset "conversion" begin
96108
@test isapprox(convert(Fixed{Int8,7}, 0.8), 0.797, atol=0.001)
97109
@test isapprox(convert(Fixed{Int8,7}, 0.9), 0.898, atol=0.001)
98-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 0.999)
99-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 1.0)
100-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 1)
101-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 2)
102-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 128)
103-
@test_throws ArgumentError convert(Fixed{Int8, 7}, 1.0)
104110

105111
@test convert(Q0f7, -128.5/128) == -1
106-
107112
@test convert(Q0f7, -0.75f0) == -0.75
108113
@test convert(Q0f7, Float16(-0.75)) == -0.75
109114
@test convert(Q0f7, BigFloat(-0.75)) == -0.75

test/normed.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,20 @@ end
6969
@testset "inexactness" begin
7070
# TODO: change back to InexactError when it allows message strings
7171
@test_throws ArgumentError N0f8(2)
72-
@test_throws ArgumentError N0f8(255)
7372
@test_throws ArgumentError N0f8(0xff)
7473
@test_throws ArgumentError N0f16(2)
7574
@test_throws ArgumentError N0f16(0xff)
7675
@test_throws ArgumentError N0f16(0xffff)
7776
@test_throws ArgumentError convert(N0f8, typemax(N6f10))
7877
@test_throws ArgumentError convert(N0f16, typemax(N6f10))
7978
@test_throws ArgumentError convert(Normed{UInt128,100}, 10^9)
80-
@test_throws ArgumentError convert(Normed{UInt128,100}, 10.0^9)
79+
80+
ret = @test_throws ArgumentError N0f8(255)
81+
msg = ret.value.msg
82+
@test occursin("N0f8 is an 8-bit type representing 256 values from 0.0 to 1.0;", msg)
83+
ret = @test_throws ArgumentError convert(Normed{UInt128,100}, 10.0^9)
84+
msg = ret.value.msg
85+
@test occursin("Normed{UInt128,100} is a 128-bit type representing 2^128 values", msg)
8186
end
8287

8388
@testset "conversion" begin

0 commit comments

Comments
 (0)