From bfacc9f3c1e7f52afa464ea5356ee4440ffd4cae Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 22 Sep 2016 07:29:41 -0500 Subject: [PATCH] Support unsafe_trunc(T, ::Integer) --- base/int.jl | 2 ++ test/int.jl | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/base/int.jl b/base/int.jl index cfb6320d724ac..085e5023f5a40 100644 --- a/base/int.jl +++ b/base/int.jl @@ -327,6 +327,8 @@ rem{T<:Integer}(x::T, ::Type{T}) = x rem(x::Integer, ::Type{Bool}) = ((x&1)!=0) mod{T<:Integer}(x::Integer, ::Type{T}) = rem(x, T) +unsafe_trunc{T<:Integer}(::Type{T}, x::Integer) = rem(x, T) + convert{Tf<:Union{Float32,Float64}}(T::BitSigned64T, x::Tf) = box(T,checked_fptosi(T,unbox(Tf,x))) convert{Tf<:Union{Float32,Float64}}(T::BitUnsigned64T, x::Tf) = diff --git a/test/int.jl b/test/int.jl index cc724bac92173..1ae1922694cbc 100644 --- a/test/int.jl +++ b/test/int.jl @@ -195,3 +195,9 @@ end @test true << 2 === 1 << 2 @test true >> 2 === 1 >> 2 @test true >>> 2 === 1 >>> 2 + +@test @inferred(unsafe_trunc(Int8, 127)) === Int8(127) +@test unsafe_trunc(Int8, 128) === Int8(-128) +@test unsafe_trunc(Int8, -127) === Int8(-127) +@test unsafe_trunc(Int8, -128) === Int8(-128) +@test unsafe_trunc(Int8, -129) === Int8(127)