diff --git a/src/FixedPointNumbers.jl b/src/FixedPointNumbers.jl index 0e394c0..35284cc 100644 --- a/src/FixedPointNumbers.jl +++ b/src/FixedPointNumbers.jl @@ -6,7 +6,7 @@ import Base: ==, <, <=, -, +, *, /, ~, isapprox, zero, oneunit, one, typemin, typemax, floatmin, floatmax, eps, reinterpret, big, rationalize, float, trunc, round, floor, ceil, bswap, clamp, div, fld, cld, rem, mod, mod1, fld1, min, max, minmax, - signed, unsigned, copysign, flipsign, signbit, + copysign, flipsign, signbit, length, widen, widemul import Random: Random, AbstractRNG, SamplerType, rand! @@ -362,14 +362,6 @@ clamp(x::X, lo::X, hi::X) where {X <: FixedPoint} = X(clamp(x.i, lo.i, hi.i), 0) clamp(x, ::Type{X}) where {X <: FixedPoint} = clamp(x, typemin(X), typemax(X)) % X -# Since `FixedPoint` is not an integer type, it is not clear in what type -# `signed` and `unsigned` for `FixedPoint` should return values. They should -# currently throw errors in case we support "unsigned Fixed" or "signed Normed" -# in the future. The following "incomplete" code is necessary for Julia v1.0 -# etc. to prevent accidental conversion to an integer type. -signed(x::X) where {X <: FixedPoint} = signed(X)(signed(x.i), 0) -unsigned(x::X) where {X <: FixedPoint} = unsigned(X)(unsigned(x.i), 0) - function copysign(x::X, y::Real) where {T, X <: FixedPoint{T}} T <: Signed ? X(copysign(x.i, y), 0) : throw_not_a_signed_number_error(x) end diff --git a/test/common.jl b/test/common.jl index 3a04608..5f02a01 100644 --- a/test/common.jl +++ b/test/common.jl @@ -375,6 +375,20 @@ function test_clamp_nan(TX::Type) end end +function test_sign_related(TX::Type) + for X in target(TX, :i8; ex = :thin) + if VERSION >= v"1.4" + # `FixedPoint` is not a signed or unsigned integer type + @test_throws Exception signed(X) + @test_throws Exception signed(X(0.5)) + @test_throws Exception unsigned(X) + @test_throws Exception unsigned(X(0.5)) + end + + @test signbit(X(0.5)) === signbit(X(0.0)) === false + end +end + function test_isinteger(TX::Type) @testset "isinteger(::$X)" for X in target(TX, :i8, :i16) xs = typemin(X):eps(X):typemax(X) diff --git a/test/fixed.jl b/test/fixed.jl index bcfce6d..baa4c3a 100644 --- a/test/fixed.jl +++ b/test/fixed.jl @@ -614,19 +614,17 @@ end end @testset "sign-related functions" begin - @test_throws Exception signed(Q0f7) - @test_throws Exception signed(0.5Q0f7) - @test_throws Exception unsigned(Q0f7) - @test_throws Exception unsigned(0.5Q0f7) + test_sign_related(Fixed) + @test copysign(0.5Q0f7, 0x1) === 0.5Q0f7 @test copysign(0.5Q0f7, -1) === -0.5Q0f7 @test flipsign(0.5Q0f7, 0x1) === 0.5Q0f7 @test flipsign(0.5Q0f7, -1) === -0.5Q0f7 - @test_throws ArgumentError sign(0Q0f7) + @test_broken sign(0Q0f7) === zero(Q0f7) + @test_throws ArgumentError sign(0.5Q0f7) @test sign(0Q1f6) === 0Q1f6 @test sign(0.5Q1f6) === 1Q1f6 @test sign(-0.5Q1f6) === -1Q1f6 - @test signbit(0.5Q0f7) === false @test signbit(-0.5Q0f7) === true end diff --git a/test/normed.jl b/test/normed.jl index 03f73fa..8c1010a 100644 --- a/test/normed.jl +++ b/test/normed.jl @@ -596,16 +596,14 @@ end end @testset "sign-related functions" begin - @test_throws Exception signed(N0f8) - @test_throws Exception signed(1N0f8) - @test_throws Exception unsigned(N0f8) - @test_throws Exception unsigned(1N0f8) + test_sign_related(Normed) + @test_throws ArgumentError copysign(1N0f8, 0x1) @test_throws ArgumentError copysign(1N0f8, -1) @test_throws ArgumentError flipsign(1N0f8, 0x1) @test_throws ArgumentError flipsign(1N0f8, -1) - @test_throws ArgumentError sign(0N0f8) - @test signbit(1N0f8) === false + @test_broken sign(0N0f8) === zero(N0f8) + @test_broken sign(0.5N0f8) === oneunit(N0f8) end @testset "bitwise" begin