Skip to content

Commit

Permalink
Merge pull request #8 from JuliaMath/teh/update
Browse files Browse the repository at this point in the history
Update to Julia 0.7+
  • Loading branch information
timholy authored Aug 22, 2018
2 parents 6325b91 + 150a837 commit ab35858
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 119 deletions.
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ os:
- linux
- osx
julia:
- 0.5
- 0.6
- 0.7
- 1.0
- nightly
notifications:
email: false
Expand All @@ -15,4 +15,4 @@ notifications:
# - julia -e 'Pkg.clone(pwd()); Pkg.build("RoundingIntegers"); Pkg.test("RoundingIntegers"; coverage=true)'
after_success:
# push coverage results to Codecov
- julia -e 'cd(Pkg.dir("RoundingIntegers")); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
- julia -e 'using RoundingIntegers, Pkg; cd(dirname(dirname(pathof(RoundingIntegers)))); Pkg.add("Coverage"); using Coverage; Codecov.submit(Codecov.process_folder())'
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ ERROR: InexactError()
julia> RInt(7.2) # but not with a rounding integer
7

julia> (map(RInt, 1.5:1:4.5)...) # rounds half integers to nearest even
julia> (map(RInt, 1.5:1:4.5)...,) # rounds half integers to nearest even
(2, 2, 4, 4)

julia> a = Vector{RUInt8}(2)
julia> a = Vector{RUInt8}(undef, 2)
2-element Array{RoundingIntegers.RUInt8,1}:
0x42
0x61
Expand Down
3 changes: 1 addition & 2 deletions REQUIRE
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
julia 0.5
Compat 0.17
julia 0.7
40 changes: 24 additions & 16 deletions appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
environment:
matrix:
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x86/0.5/julia-0.5-latest-win32.exe"
- JULIA_URL: "https://julialang-s3.julialang.org/bin/winnt/x64/0.5/julia-0.5-latest-win64.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x86/julia-latest-win32.exe"
- JULIA_URL: "https://julialangnightlies-s3.julialang.org/bin/winnt/x64/julia-latest-win64.exe"
- julia_version: 0.7
- julia_version: 1
- julia_version: nightly

platform:
- x86 # 32-bit
- x64 # 64-bit

# # Uncomment the following lines to allow failures on nightly julia
# # (tests will run but not make your overall status red)
# matrix:
# allow_failures:
# - julia_version: nightly

branches:
only:
Expand All @@ -17,19 +26,18 @@ notifications:
on_build_status_changed: false

install:
- ps: "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12"
# Download most recent Julia Windows binary
- ps: (new-object net.webclient).DownloadFile(
$env:JULIA_URL,
"C:\projects\julia-binary.exe")
# Run installer silently, output to C:\projects\julia
- C:\projects\julia-binary.exe /S /D=C:\projects\julia
- ps: iex ((new-object net.webclient).DownloadString("https://raw.githubusercontent.com/JuliaCI/Appveyor.jl/version-1/bin/install.ps1"))

build_script:
# Need to convert from shallow to complete for Pkg.clone to work
- IF EXIST .git\shallow (git fetch --unshallow)
- C:\projects\julia\bin\julia -e "versioninfo();
Pkg.clone(pwd(), \"RoundingIntegers\"); Pkg.build(\"RoundingIntegers\")"
- echo "%JL_BUILD_SCRIPT%"
- C:\julia\bin\julia -e "%JL_BUILD_SCRIPT%"

test_script:
- C:\projects\julia\bin\julia -e "Pkg.test(\"RoundingIntegers\")"
- echo "%JL_TEST_SCRIPT%"
- C:\julia\bin\julia -e "%JL_TEST_SCRIPT%"

# # Uncomment to support code coverage upload. Should only be enabled for packages
# # which would have coverage gaps without running on Windows
# on_success:
# - echo "%JL_CODECOV_SCRIPT%"
# - C:\julia\bin\julia -e "%JL_CODECOV_SCRIPT%"
146 changes: 61 additions & 85 deletions src/RoundingIntegers.jl
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
__precompile__(true)

module RoundingIntegers

using Compat
import Base: <, <=, +, -, *, ~, &, |, $, <<, >>, >>>
import Base: <, <=, +, -, *, ~, &, |, <<, >>, >>>, xor

export RSigned, RUnsigned, RInteger
export RInt8, RUInt8, RInt16, RUInt16, RInt32, RUInt32, RInt64, RUInt64,
RInt128, RUInt128, RInt, RUInt

@compat abstract type RSigned <: Signed end
@compat abstract type RUnsigned <: Unsigned end
abstract type RSigned <: Signed end
abstract type RUnsigned <: Unsigned end

const RInteger = Union{RSigned,RUnsigned}

@compat primitive type RInt8 <: RSigned 8 end
@compat primitive type RUInt8 <: RUnsigned 8 end
@compat primitive type RInt16 <: RSigned 16 end
@compat primitive type RUInt16 <: RUnsigned 16 end
@compat primitive type RInt32 <: RSigned 32 end
@compat primitive type RUInt32 <: RUnsigned 32 end
@compat primitive type RInt64 <: RSigned 64 end
@compat primitive type RUInt64 <: RUnsigned 64 end
@compat primitive type RInt128 <: RSigned 128 end
@compat primitive type RUInt128 <: RUnsigned 128 end
primitive type RInt8 <: RSigned 8 end
primitive type RUInt8 <: RUnsigned 8 end
primitive type RInt16 <: RSigned 16 end
primitive type RUInt16 <: RUnsigned 16 end
primitive type RInt32 <: RSigned 32 end
primitive type RUInt32 <: RUnsigned 32 end
primitive type RInt64 <: RSigned 64 end
primitive type RUInt64 <: RUnsigned 64 end
primitive type RInt128 <: RSigned 128 end
primitive type RUInt128 <: RUnsigned 128 end

if Sys.WORD_SIZE == 32
const RInt = RInt32
Expand Down Expand Up @@ -65,67 +62,45 @@ rtype(x::Union{Signed,Unsigned}) = rtype(typeof(x))
# RIntegers are largely about assignment; for usage, we convert to
# standard integers at the drop of a hat.

Base.promote_rule{T<:Number,RI<:RInteger}(::Type{T}, ::Type{RI}) =
Base.promote_rule(::Type{RI}, ::Type{T}) where {T<:Number,RI<:RInteger} =
promote_type(T, itype(RI))
# Resolve ambiguities
Base.promote_rule{RI<:RInteger}(::Type{Bool}, ::Type{RI}) =
promote_type(Bool, itype(RI))
Base.promote_rule{RI<:RInteger}(::Type{BigInt}, ::Type{RI}) =
promote_type(BigInt, itype(RI))
Base.promote_rule{RI<:RInteger}(::Type{BigFloat}, ::Type{RI}) =
promote_type(BigFloat, itype(RI))
Base.promote_rule{T<:Real,RI<:RInteger}(::Type{Complex{T}}, ::Type{RI}) =
promote_type(Complex{T}, itype(RI))
Base.promote_rule{T<:Integer,RI<:RInteger}(::Type{Rational{T}}, ::Type{RI}) =
promote_type(Rational{T}, itype(RI))
@compat Base.promote_rule{RI<:RInteger}(::Type{<:Irrational}, ::Type{RI}) =
promote_type(Float64, itype(RI))

(::Type{Signed})(x::RSigned) = reinterpret(itype(x), x)
(::Type{Unsigned})(x::RUnsigned) = reinterpret(itype(x), x)
(::Type{RSigned})(x::Signed) = reinterpret(rtype(x), x)
(::Type{RUnsigned})(x::Unsigned) = reinterpret(rtype(x), x)
(::Type{Integer})(x::RSigned) = Signed(x)
(::Type{Integer})(x::RUnsigned) = Unsigned(x)
(::Type{RInteger})(x::Signed) = RSigned(x)
(::Type{RInteger})(x::Unsigned) = RUnsigned(x)

# Basic conversions
# @inline Base.convert{T<:RSigned}(::Type{T}, x::T) = x
# @inline Base.convert{T<:RUnsigned}(::Type{T}, x::T) = x
@inline Base.convert{T<:RInteger}(::Type{T}, x::T) = x
@inline Base.convert{T<:RInteger}(::Type{T}, x::RInteger) =
RInteger(convert(itype(T), Integer(x)))
@inline Base.convert{T<:RInteger}(::Type{T}, x::Integer) = RInteger(convert(itype(T), x))
@inline Base.convert{T<:RInteger}(::Type{T}, x::AbstractFloat) =
RInteger(round(itype(T), x))
@inline Base.convert{T<:RInteger}(::Type{T}, x::Number) =
convert(T, convert(itype(T), x))

@inline Base.convert{T<:Number}(::Type{T}, x::RInteger) = convert(T, Integer(x))

# Resolve ambiguities
Base.convert(::Type{Integer}, x::RInteger) = Integer(x)
Base.convert(::Type{BigInt}, x::RInteger) = convert(BigInt, Integer(x))
Base.convert{T<:RInteger}(::Type{T}, x::BigInt) = RInteger(convert(itype(T), x))
Base.convert(::Type{BigFloat}, x::RInteger) = convert(BigFloat, Integer(x))
Base.convert{T<:RInteger}(::Type{T}, x::BigFloat) = RInteger(convert(itype(T), x))
Base.convert{T<:Real}(::Type{Complex{T}}, x::RInteger) = convert(Complex{T}, Integer(x))
Base.convert{T<:RInteger}(::Type{T}, z::Complex) = RInteger(convert(itype(T), z))
Base.convert(::Type{Complex}, x::RInteger) = Complex(x)
Base.convert{T<:RInteger}(::Type{T}, x::Rational) = RInteger(convert(itype(T)), x)
Base.convert{T<:Integer}(::Type{Rational{T}}, x::RInteger) =
convert(Rational{T}, Integer(x))
Base.convert(::Type{Rational}, x::RInteger) = convert(Rational{typeof(x)}, x)
Base.convert(::Type{Float16}, x::RInteger) = convert(Float16, Integer(x))
Base.convert{T<:RInteger}(::Type{T}, x::Float16) = RInteger(convert(itype(T), x))
Base.convert(::Type{Bool}, x::RInteger) = convert(Bool, Integer(x))

Base.Signed(x::RSigned) = reinterpret(itype(x), x)
Base.Unsigned(x::RUnsigned) = reinterpret(itype(x), x)
RSigned(x::Signed) = reinterpret(rtype(x), x)
RUnsigned(x::Unsigned) = reinterpret(rtype(x), x)
Base.Integer(x::RSigned) = Signed(x)
Base.Integer(x::RUnsigned) = Unsigned(x)
RInteger(x::Signed) = RSigned(x)
RInteger(x::Unsigned) = RUnsigned(x)

Base.AbstractFloat(x::RInteger) = AbstractFloat(Integer(x))

RInt8(x::Int8) = reinterpret(RInt8, x)
RUInt8(x::UInt8) = reinterpret(RUInt8, x)
RInt16(x::Int16) = reinterpret(RInt16, x)
RUInt16(x::UInt16) = reinterpret(RUInt16, x)
RInt32(x::Int32) = reinterpret(RInt32, x)
RUInt32(x::UInt32) = reinterpret(RUInt32, x)
RInt64(x::Int64) = reinterpret(RInt64, x)
RUInt64(x::UInt64) = reinterpret(RUInt64, x)
RInt128(x::Int128) = reinterpret(RInt128, x)
RUInt128(x::UInt128) = reinterpret(RUInt128, x)

(::Type{R})(x::Integer) where R<:RInteger = R(convert(itype(R), x))
(::Type{R})(x::Float16) where R<:RInteger = R(round(itype(R), x))
(::Type{R})(x::BigFloat) where R<:RInteger = R(round(itype(R), x))
(::Type{R})(x::Rational) where R<:RInteger = R(round(itype(R), x))
(::Type{R})(x::Complex) where R<:RInteger = R(round(itype(R), x))
(::Type{R})(x::AbstractFloat) where R<:RInteger = R(round(itype(R), x))

@inline Base.convert(::Type{T}, x::RInteger) where {T<:Number} = convert(T, Integer(x))

# rem conversions
@inline Base.rem{T<:RInteger}(x::T, ::Type{T}) = T
@inline Base.rem{T<:RInteger}(x::Integer, ::Type{T}) = RInteger(rem(x, itype(T)))
@inline Base.rem(x::T, ::Type{T}) where {T<:RInteger} = T
@inline Base.rem(x::Integer, ::Type{T}) where {T<:RInteger} = RInteger(rem(x, itype(T)))
# ambs
@inline Base.rem{T<:RInteger}(x::BigInt, ::Type{T}) = error("no rounding BigInt available")
@inline Base.rem(x::BigInt, ::Type{T}) where {T<:RInteger} = error("no rounding BigInt available")


Base.flipsign(x::RSigned, y::RSigned) = RInteger(flipsign(Integer(x), Integer(y)))
Expand All @@ -137,6 +112,8 @@ Base.count_ones(x::RInteger) = count_ones(Integer(x))
Base.leading_zeros(x::RInteger) = leading_zeros(Integer(x))
Base.trailing_zeros(x::RInteger) = trailing_zeros(Integer(x))
Base.ndigits0z(x::RInteger) = Base.ndigits0z(Integer(x))
Base.ndigits0zpb(x::RSigned, b::Integer) = Base.ndigits0zpb(abs(Integer(x)), Int(b))
Base.ndigits0zpb(x::RUnsigned, b::Integer) = Base.ndigits0zpb(Integer(x), Int(b))

# A few operations preserve the type
-(x::RInteger) = RInteger(-Integer(x))
Expand All @@ -153,23 +130,22 @@ Base.ndigits0z(x::RInteger) = Base.ndigits0z(Integer(x))
>>>(x::RInteger, y::Int) = RInteger(Integer(x) >>> y)
<<(x::RInteger, y::Int) = RInteger(Integer(x) << y)

+{T<:RInteger}(x::T, y::T) = RInteger(Integer(x) + Integer(y))
-{T<:RInteger}(x::T, y::T) = RInteger(Integer(x) - Integer(y))
*{T<:RInteger}(x::T, y::T) = RInteger(Integer(x) * Integer(y))
(&){T<:RInteger}(x::T, y::T) = RInteger(Integer(x) & Integer(y))
(|){T<:RInteger}(x::T, y::T) = RInteger(Integer(x) | Integer(y))
# ($){T<:RInteger}(x::T, y::T) = RInteger(Integer(x) $ Integer(y))
Compat.xor{T<:RInteger}(x::T, y::T) = RInteger(xor(Integer(x), Integer(y)))
+(x::T, y::T) where {T<:RInteger} = RInteger(Integer(x) + Integer(y))
-(x::T, y::T) where {T<:RInteger} = RInteger(Integer(x) - Integer(y))
*(x::T, y::T) where {T<:RInteger} = RInteger(Integer(x) * Integer(y))
(&)(x::T, y::T) where {T<:RInteger} = RInteger(Integer(x) & Integer(y))
(|)(x::T, y::T) where {T<:RInteger} = RInteger(Integer(x) | Integer(y))
xor(x::T, y::T) where {T<:RInteger} = RInteger(xor(Integer(x), Integer(y)))

Base.rem{T<:RInteger}(x::T, y::T) = RInteger(rem(Integer(x), Integer(y)))
Base.mod{T<:RInteger}(x::T, y::T) = RInteger(mod(Integer(x), Integer(y)))
Base.rem(x::T, y::T) where {T<:RInteger} = RInteger(rem(Integer(x), Integer(y)))
Base.mod(x::T, y::T) where {T<:RInteger} = RInteger(mod(Integer(x), Integer(y)))

Base.unsigned(x::RSigned) = RInteger(unsigned(Integer(x)))
Base.signed(x::RSigned) = RInteger(signed(Integer(x)))

# traits
Base.typemin{T<:RInteger}(::Type{T}) = RInteger(typemin(itype(T)))
Base.typemax{T<:RInteger}(::Type{T}) = RInteger(typemax(itype(T)))
Base.widen{T<:RInteger}(::Type{T}) = rtype(widen(itype(T)))
Base.typemin(::Type{T}) where {T<:RInteger} = RInteger(typemin(itype(T)))
Base.typemax(::Type{T}) where {T<:RInteger} = RInteger(typemax(itype(T)))
Base.widen(::Type{T}) where {T<:RInteger} = rtype(widen(itype(T)))

end # module
15 changes: 4 additions & 11 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
using Compat, Base.Test
if VERSION < v"0.6.0-pre.alpha.229"
using RoundingIntegers
@test isempty(detect_ambiguities(Base, Core, RoundingIntegers))
else
ambs0 = detect_ambiguities(Base, Core, Compat)
using RoundingIntegers
ambs1 = detect_ambiguities(Base, Core, Compat, RoundingIntegers)
@test isempty(setdiff(ambs1, ambs0))
end
using Test
using RoundingIntegers
@test isempty(detect_ambiguities(Base, Core, RoundingIntegers))

@testset "Basics" begin
r16 = RInt16(3)
Expand Down Expand Up @@ -62,7 +55,7 @@ end
@test !signbit(r)
@test copysign(r, r) === r
@test unsigned(r) === RUInt(5)
@test hex(r) == "5"
@test string(r, base=16) == "5"

@test string(RInt(7.2)) == "7"
end
Expand Down

0 comments on commit ab35858

Please sign in to comment.