Skip to content
Closed
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
10 changes: 1 addition & 9 deletions src/FixedPointNumbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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)
Comment on lines -370 to -371

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Julia 1.3, this breaks the code (no surprise here):

julia> using FixedPointNumbers; signed(1N0f8)
1

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Making code that was working stop working is a breaking change, but I’m skeptical about whether making code that wasn’t working start working in an undesirable way counts as a breaking change.

As far as these two methods are concerned, I don't have a strong opinion.
However, if we interpret “breaking changes” too strictly, we won't be able to fix bugs.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point.

I still like the "more honest" approach of #350 better (and I would suggest that we avoid spending time on compatibility with versions no one uses with upgraded package versions anyway – sorry for triggering this), but I can follow your argument here: It wasn't working before and in that sense it's not too bad that it is not working now. Nevertheless, the question remains why it's better to return an incorrect result instead of throwing.


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
Expand Down
14 changes: 14 additions & 0 deletions test/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions test/fixed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
10 changes: 4 additions & 6 deletions test/normed.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading