Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ function _length(r::StepRange{<:FixedPoint})
end

hasalias(::Type) = false
hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}} = f isa Int
function hasalias(::Type{X}) where {T<:NotBiggerThanInt64, f, X<:FixedPoint{T,f}}
isprimitivetype(T) && (f isa Int)
end

# `alias_symbol` is used to define type aliases, so we need this before we
# include "src/fixed.jl" / "src/normed.jl".
Expand Down Expand Up @@ -623,15 +625,18 @@ scaledual(::Type{Tdual}, x::AbstractArray{T}) where {Tdual, T <: FixedPoint} =
convert(Tdual, 1/rawone(T)), reinterpret(rawtype(T), x)

@noinline function throw_converterror(::Type{X}, @nospecialize(x)) where X <: FixedPoint
nbits = bitwidth(rawtype(X))
io = IOBuffer()
showtype(io, X)
print(io, " is ")
print(io, nbits == 8 ? "an " : "a ", nbits, "-bit type representing ")
print(io, nbits <= 16 ? string(2^nbits) : "2^$nbits", " values from ")
print(IOContext(io, :compact=>true), typemin(X), " to ")
print(IOContext(io, :compact=>true), typemax(X), "; ")
print(io, "cannot represent ", x)
T = rawtype(X)
if isprimitivetype(T) && !(X <: FixedPoint{Union{}}) # The latter is a hint for JET.
nbits = bitwidth(T)
print(io, " is ")
print(io, nbits == 8 ? "an " : "a ", nbits, "-bit type representing ")
print(io, nbits <= 16 ? string(2^nbits) : "2^$nbits", " values from ")
print(IOContext(io, :compact=>true), typemin(X), " to ")
print(IOContext(io, :compact=>true), typemax(X), ";")
end
print(io, " cannot represent ", x)
throw(ArgumentError(String(take!(io))))
end

Expand Down
3 changes: 3 additions & 0 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,9 @@ end
show(iob, Fixed{Int128,64}(-1.2345e6))
@test String(take!(iob)) == "Fixed{Int128,$(SP)64}(-1.2345e6)"

FixedPointNumbers.showtype(iob, Fixed{Union{}, 7})
@test String(take!(iob)) == "Fixed{Union{},$(SP)7}"

# TODO: remove this test
show(iob, reinterpret(Fixed{Int8,8}, signed(0xaa)))
@test String(take!(iob)) == "Fixed{Int8,$(SP)8}(-0.336)"
Expand Down
3 changes: 3 additions & 0 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,9 @@ end

show(iob, Normed{UInt128,64}(1.2345e6))
@test String(take!(iob)) == "Normed{UInt128,$(SP)64}(1.2345e6)"

FixedPointNumbers.showtype(iob, Normed{Union{}, 8})
@test String(take!(iob)) == "Normed{Union{},$(SP)8}"
end

@testset "summary" begin
Expand Down
Loading