Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support unsafe_trunc(T, ::Integer) #18629

Merged
merged 1 commit into from
Sep 24, 2016
Merged

Support unsafe_trunc(T, ::Integer) #18629

merged 1 commit into from
Sep 24, 2016

Conversation

timholy
Copy link
Member

@timholy timholy commented Sep 22, 2016

The input types supported by unsafe_trunc and rem are currently disjoint:

julia> unsafe_trunc(Int32, 3.2)
3

julia> 3.2 % Int32
ERROR: MethodError: no method matching rem(::Float64, ::Type{Int32})
Closest candidates are:
  rem(::Bool, ::Type{Int32}) at int.jl:221
  rem(::Int16, ::Type{Int32}) at int.jl:218
  rem(::Int64, ::Type{Int32}) at int.jl:208
  ...

julia> unsafe_trunc(Int32, 3)
ERROR: MethodError: no method matching unsafe_trunc(::Type{Int32}, ::Int64)
Closest candidates are:
  unsafe_trunc(::Type{Int32}, ::Float32) at float.jl:159
  unsafe_trunc(::Type{Int32}, ::Float64) at float.jl:160
  unsafe_trunc{T<:Integer}(::Type{T<:Integer}, ::BigFloat) at mpfr.jl:130

julia> 3 % Int32
3

I'm not sure this is a bug, but it seems a bit inconvenient. Since trunc(Int32, 3) works, I thought it would be best to define unsafe_trunc for integer inputs.

@@ -195,3 +195,9 @@ end
@test true << 2 === 1 << 2
@test true >> 2 === 1 >> 2
@test true >>> 2 === 1 >>> 2

@test unsafe_trunc(Int8, 127) == Int8(127)
Copy link
Contributor

Choose a reason for hiding this comment

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

check type equality too with === ?

@timholy timholy merged commit ca16ac6 into master Sep 24, 2016
@timholy timholy deleted the teh/unsafe_trunc branch September 24, 2016 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants