Skip to content

Commit

Permalink
Merge pull request JuliaLang#14302 from eschnett/eschnett/flipsign2
Browse files Browse the repository at this point in the history
Don't promote flipsign arguments for signed integers
  • Loading branch information
jakebolewski committed Dec 9, 2015
2 parents 53b3783 + 3181bd9 commit e7a2986
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ for T in (Int8,Int16,Int32,Int64,Int128)
@eval flipsign(x::$T, y::$T) = box($T,flipsign_int(unbox($T,x),unbox($T,y)))
end

flipsign(x::Signed, y::Signed) = flipsign(promote(x,y)...)
flipsign(x::Signed, y::Signed) = convert(typeof(x), flipsign(promote(x,y)...))
flipsign(x::Signed, y::Float16) = flipsign(x, reinterpret(Int16,y))
flipsign(x::Signed, y::Float32) = flipsign(x, reinterpret(Int32,y))
flipsign(x::Signed, y::Float64) = flipsign(x, reinterpret(Int64,y))
Expand Down
14 changes: 14 additions & 0 deletions test/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ for y in (4, Float32(4), 4.0, big(4.0))
@test copysign(-3, y) == 3
end

# Result type must be type of first argument
for T in (Int8,Int16,Int32,Int64,Int128,BigInt,
UInt8,UInt16,UInt32,UInt64,UInt128,
Rational{Int},Rational{BigInt},
Float16,Float32,Float64)
for U in (Int8,Int16,Int32,Int64,Int128,BigInt,
Rational{Int},Rational{BigInt},
UInt8,UInt16,UInt32,UInt64,UInt128,
Float16,Float32,Float64)
@test typeof(copysign(T(3), U(4))) === T
@test typeof(flipsign(T(3), U(4))) === T
end
end

for s1 in (-1,+1), s2 in (-1,+1)
@test flipsign(Int16(3s1), Float16(3s2)) === Int16(3s1*s2)
@test flipsign(Int32(3s1), Float32(3s2)) === Int32(3s1*s2)
Expand Down

0 comments on commit e7a2986

Please sign in to comment.