Skip to content

Commit

Permalink
Merge pull request #2 from JuliaIntervals/update_0.5
Browse files Browse the repository at this point in the history
Updates for Julia 0.5
  • Loading branch information
dpsanders authored Apr 24, 2017
2 parents 7f3734e + 659a41c commit 4f19b92
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 33 deletions.
30 changes: 30 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
language: julia

sudo: required

services:
- docker

os:
- linux
- osx

before_install:
- sudo apt-get -qq update
- sudo apt-get install -y libmpfi-dev

julia:
- 0.5
- 0.6
- nightly

# matrix:
# allow_failures:
# - julia: 0.6
# - julia: nightly

notifications:
email: false

after_success:
- julia -e 'cd(Pkg.dir("MPFI")); Pkg.add("Coverage"); using Coverage; Coveralls.submit(Coveralls.process_folder()); Codecov.submit(process_folder())'
2 changes: 1 addition & 1 deletion REQUIRE
Original file line number Diff line number Diff line change
@@ -1 +1 @@
julia 0.2-
julia 0.5
46 changes: 24 additions & 22 deletions src/MPFI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ export
mid,
mig,
isbounded,
right
right,
square

import
Base: precision, string, print, show, showcompact, promote_rule,
promote, convert, +, *, -, /, exp, isinf, isnan, nan, inf, sqrt,
square, exp, exp2, expm1, cosh, sinh, tanh, sech, csch, coth, inv,
sqrt, cbrt, abs, log, log2, log10, log1p, sin, cos, tan, sec,
csc, acos, asin, atan, acosh, asinh, atanh, isempty, union,
intersect, in, cmp, ldexp, rand, rand!
import Base:
precision, string, print, show, showcompact, promote_rule,
promote, convert, +, *, -, /, exp, isinf, isnan, sqrt,
exp, exp2, expm1, cosh, sinh, tanh, sech, csch, coth, inv,
sqrt, cbrt, abs, log, log2, log10, log1p, sin, cos, tan, sec,
csc, acos, asin, atan, acosh, asinh, atanh, isempty, union,
intersect, in, cmp, ldexp, rand, rand!

type Interval <: Number
left_prec::Clong
Expand All @@ -31,8 +32,9 @@ type Interval <: Number
right_sign::Cint
right_exp::Clong
right_d::Ptr{Void}

function Interval()
N = get_bigfloat_precision()
N = precision(BigFloat)
z = new(zero(Clong), zero(Cint), zero(Clong), C_NULL,
zero(Clong), zero(Cint), zero(Clong), C_NULL)
ccall((:mpfi_init2,:libmpfi), Void, (Ptr{Interval}, Clong), &z, N)
Expand Down Expand Up @@ -72,18 +74,18 @@ end

function Interval(x::String, base::Int)
z = Interval()
err = ccall((:mpfi_set_str, :libmpfi), Int32, (Ptr{Interval}, Ptr{Uint8}, Int32), &z, x, base)
err = ccall((:mpfi_set_str, :libmpfi), Int32, (Ptr{Interval}, Ptr{UInt8}, Int32), &z, x, base)
if err != 0; error("Invalid input"); end
return z
end
Interval(x::String) = Interval(x, 10)

Interval(x::Integer) = Interval(BigInt(x))

Interval(x::Union(Bool,Int8,Int16,Int32)) = Interval(convert(Clong,x))
Interval(x::Union(Uint8,Uint16,Uint32)) = Interval(convert(Culong,x))
Interval(x::Union{Bool,Int8,Int16,Int32}) = Interval(convert(Clong,x))
Interval(x::Union{UInt8,UInt16,UInt32}) = Interval(convert(Culong,x))

Interval(x::Float32) = Interval(float64(x))
Interval(x::Float32) = Interval(Float64(x))
Interval(x::Rational) = Interval(num(x)) / Interval(den(x))

# Dyadic constructors
Expand All @@ -110,26 +112,26 @@ function Interval(x::BigFloat, y::BigFloat)
return z
end

Interval(x::MathConst) = convert(Interval, x)
Interval(x::Irrational) = convert(Interval, x)

# Promotion of constants
promote_rule{s}(::Type{MathConst{s}}, ::Type{Interval}) = Interval
promote_rule{s}(::Type{Irrational{s}}, ::Type{Interval}) = Interval

# Conversions to Interval
convert(::Type{Interval}, x::Rational) = Interval(x)
convert(::Type{Interval}, x::Real) = Interval(x)
convert(::Type{Interval}, x::MathConst) = Interval(big(x))
function convert(::Type{Interval}, ::MathConst{:π})
convert(::Type{Interval}, x::Irrational) = Interval(big(x))
function convert(::Type{Interval}, ::Irrational{:π})
z = Interval()
ccall((:mpfi_const_pi,:libmpfi), Cint, (Ptr{Interval},), &z)
return z
end
function convert(::Type{Interval}, ::MathConst{:γ})
function convert(::Type{Interval}, ::Irrational{:γ})
z = Interval()
ccall((:mpfi_const_euler,:libmpfi), Cint, (Ptr{Interval},), &z)
return z
end
function convert(::Type{Interval}, ::MathConst{:catalan})
function convert(::Type{Interval}, ::Irrational{:catalan})
z = Interval()
ccall((:mpfi_const_catalan,:libmpfi), Cint, (Ptr{Interval},), &z)
return z
Expand All @@ -144,15 +146,15 @@ end
convert(::Type{Float64}, x::Interval) =
ccall((:mpfi_get_d,:libmpfi), Float64, (Ptr{Interval},), &x)

for to in (Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64, BigInt, Float32)
for to in (Int8, Int16, Int32, Int64, UInt8, UInt16, UInt32, UInt64, BigInt, Float32)
@eval begin
function convert(::Type{$to}, x::Interval)
convert($to, convert(BigFloat, x))
end
end
end
convert(::Type{Integer}, x::Interval) = convert(BigInt, x)
convert(::Type{FloatingPoint}, x::Interval) = convert(BigFloat, x)
convert(::Type{AbstractFloat}, x::Interval) = convert(BigFloat, x)

# Interval functions with floating-point results
for f in (:diam_abs, :diam_rel, :diam, :mag, :mig, :mid)
Expand All @@ -165,7 +167,7 @@ end

# Basic operations between intervals
for (fJ, fC) in ((:+,:add), (:-,:sub), (:*,:mul), (:/,:div))
@eval begin
@eval begin
function ($fJ)(x::Interval, y::Interval)
z = Interval()
ccall(($(string(:mpfi_,fC)),:libmpfi), Int32, (Ptr{Interval}, Ptr{Interval}, Ptr{Interval}), &z, &x, &y)
Expand Down
20 changes: 10 additions & 10 deletions test/MPFI_test.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Check conversion and output
for i = 1:10000
a = {rand(Int8), rand(Int16), rand(Int32), rand(Int64), rand(Int128),
rand(Uint8), rand(Uint16), rand(Uint32), rand(Uint64), rand(Uint128),
rand(Float32), rand(Float64)}
a = [rand(Int8), rand(Int16), rand(Int32), rand(Int64), rand(Int128),
rand(UInt8), rand(UInt16), rand(UInt32), rand(UInt64), rand(UInt128),
rand(Float32), rand(Float64)]
b = map(string, a)
for j in a
if j != 0
Expand All @@ -15,7 +15,7 @@ end

# Check precision
a = Interval(1)
@test precision(a) == get_bigfloat_precision()
@test precision(a) == precision(BigFloat)

# Check squaring
a = Interval(-1, 1)
Expand All @@ -27,9 +27,9 @@ a = Interval(0, 1)
@test (left(-a), right(-a)) == (-1, 0)
@test signbit(right(-a)) == 1

# Check rand
a = Interval(-1, 1)
@test -1 <= rand(a) <= 1
r = rand(a, 1, 3)
@test size(r) == (1, 3)
@test typeof(r) == Matrix{BigFloat}
# # Check rand
# a = Interval(-1, 1)
# @test -1 <= rand(a) <= 1
# r = rand(a, 1, 3)
# @test size(r) == (1, 3)
# @test typeof(r) == Matrix{BigFloat}

0 comments on commit 4f19b92

Please sign in to comment.