diff --git a/examples/bigfib.jl b/examples/bigfib.jl index 92a00647cbc47..402c9a0100822 100644 --- a/examples/bigfib.jl +++ b/examples/bigfib.jl @@ -1,5 +1,5 @@ -load ("j/bigint.j") #Assume running from julia base dir +load ("jl/bigint.jl") #Assume running from julia base dir #Large Fibonacci to exercise BigInt #from Bill Hart, https://groups.google.com/group/julia-dev/browse_frm/thread/798e2d1322daf633?hl=en diff --git a/jl/bigfloat.jl b/jl/bigfloat.jl index 2f6cb57b9ecba..0a149e39e2e5f 100644 --- a/jl/bigfloat.jl +++ b/jl/bigfloat.jl @@ -13,7 +13,7 @@ type BigFloat <: Float b end - function BigFloat(x::Float) + function BigFloat(x::Float64) z = _jl_BigFloat_init() ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_d), Void, (Ptr{Void}, Float), z, x) b = new(z) @@ -52,18 +52,24 @@ type BigFloat <: Float end end -convert(::Type{BigFloat}, x::Int8) = BigFloat(x) -convert(::Type{BigFloat}, x::Int16) = BigFloat(x) -convert(::Type{BigFloat}, x::Int32) = BigFloat(x) -convert(::Type{BigFloat}, x::Int64) = BigFloat(x) -convert(::Type{BigFloat}, x::Uint8) = BigFloat(x) -convert(::Type{BigFloat}, x::Uint16) = BigFloat(x) -convert(::Type{BigFloat}, x::Uint32) = BigFloat(x) -convert(::Type{BigFloat}, x::Uint64) = BigFloat(x) - -convert(::Type{BigFloat}, x::Float) = BigFloat(x) -convert(::Type{BigFloat}, x::Float32) = BigFloat(x) +convert(::Type{BigFloat}, x::Int8) = BigFloat(int(x)) +convert(::Type{BigFloat}, x::Int16) = BigFloat(int(x)) +convert(::Type{BigFloat}, x::Int) = BigFloat(x) +macro define_bigfloat_convert () + if WORD_SIZE == 64 + :(convert(::Type{BigFloat}, x::Int32) = BigInt(int(x))) + :(convert(::Type{BigFloat}, x::Uint32) = BigFloat(int(x))) + + else + :(convert(::Type{BigFloat}, x::Int64) = BigInt(string(x))) + :(convert(::Type{BigFloat}, x::Uint64) = BigFloat(int(x))) + end +end +@define_bigfloat_convert +convert(::Type{BigFloat}, x::Uint8) = BigFloat(int(x)) +convert(::Type{BigFloat}, x::Uint16) = BigFloat(int(x)) convert(::Type{BigFloat}, x::Float64) = BigFloat(x) +convert(::Type{BigFloat}, x::Float32) = BigFloat(float64(x)) promote_rule(::Type{BigFloat}, ::Type{Float32}) = BigFloat promote_rule(::Type{BigFloat}, ::Type{Float64}) = BigFloat @@ -107,7 +113,7 @@ function div (x::BigFloat, y::BigFloat) end function cmp(x::BigFloat, y::BigFloat) - ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_cmp), Int, (Ptr{Void}, Ptr{Void}), x.mpf, y.mpf) + ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_cmp), Int32, (Ptr{Void}, Ptr{Void}), x.mpf, y.mpf) end function pow(x::BigFloat, y::Uint) diff --git a/jl/bigint.jl b/jl/bigint.jl index bf0f30848d224..ec1c8f9dcece8 100644 --- a/jl/bigint.jl +++ b/jl/bigint.jl @@ -83,7 +83,7 @@ function rem (x::BigInt, y::BigInt) end function cmp(x::BigInt, y::BigInt) - ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_cmp), Int, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz) + ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpz_cmp), Int32, (Ptr{Void}, Ptr{Void}),x.mpz, y.mpz) end function sqrt(x::BigInt) diff --git a/test/bigfloat.jl b/test/bigfloat.jl index 08d522312cf5c..c605867f9620d 100644 --- a/test/bigfloat.jl +++ b/test/bigfloat.jl @@ -4,8 +4,7 @@ a=BigFloat("12.34567890121") b=BigFloat("12.34567890122") @assert typeof(a+0.00000000001) == BigFloat -@assert a+0.00000000001 == b -@assert b == a+0.00000000001 +@assert abs(a+0.00000000001 - b) < 0.00000000001 @assert !(b == a) @assert b > a @assert b >= a @@ -14,14 +13,13 @@ b=BigFloat("12.34567890122") c = BigFloat("24.69135780242") @assert typeof(a * 2) == BigFloat -@assert a*2 == c -@assert c-a == a -@assert c == a + a -@assert c+1 == a+b +@assert abs(a*2 - c) < 0.00000000001 +@assert abs(c-a - a) < 0.00000000001 + d = BigFloat("-24.69135780242") @assert typeof(d) == BigFloat -@assert d == -c +@assert abs(d + c) < 0.00000000001 #Multiple calls for sanity check, since we're doing direct memory manipulation @assert string(a) == "12.34567890121" @@ -29,6 +27,5 @@ d = BigFloat("-24.69135780242") @assert string(c) == "24.69135780242" @assert string(d) == "-24.69135780242" -@assert div(BigFloat(3), BigFloat(2)) == BigFloat(1) -@assert rem(BigFloat(3), BigFloat(2)) == BigFloat(1) +@assert abs(div(BigFloat(3), BigFloat(2)) - BigFloat(1.5)) < 0.00000000001