Skip to content

Commit

Permalink
Merge pull request #533 from aviks/bigint
Browse files Browse the repository at this point in the history
Bigfloat changes -- make all tests pass
  • Loading branch information
ViralBShah committed Mar 7, 2012
2 parents 5b8a93b + a84a997 commit ab57179
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 24 deletions.
2 changes: 1 addition & 1 deletion examples/bigfib.jl
Original file line number Diff line number Diff line change
@@ -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
Expand Down
32 changes: 19 additions & 13 deletions jl/bigfloat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion jl/bigint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
15 changes: 6 additions & 9 deletions test/bigfloat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,21 +13,19 @@ 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"
@assert string(b) == "12.34567890122"
@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

0 comments on commit ab57179

Please sign in to comment.