-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #532 from choffstein/master
"Questionable Pull": Fixed BigInt bug & added BigFloat. Merging the whole thing for now, since it won't affect anything else.
- Loading branch information
Showing
5 changed files
with
278 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
_jl_libgmp_wrapper = dlopen("libgmp_wrapper") | ||
|
||
load("bigint.jl") | ||
|
||
type BigFloat <: Float | ||
mpf::Ptr{Void} | ||
|
||
function BigFloat(x::String) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_string), Void, (Ptr{Void}, Ptr{Uint8}), z, cstring(x)) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
end | ||
|
||
function BigFloat(x::Float) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_d), Void, (Ptr{Void}, Float), z, x) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
end | ||
|
||
function BigFloat(x::Uint) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_ui), Void, (Ptr{Void}, Uint), z, x) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
end | ||
|
||
function BigFloat(x::Int) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_si), Void, (Ptr{Void}, Int), z, x) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
end | ||
|
||
function BigFloat(x::BigInt) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_z), Void, (Ptr{Void}, Ptr{Void}), z, x.mpz) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
end | ||
|
||
function BigFloat(z::Ptr{Void}) | ||
b = new(z) | ||
finalizer(b, _jl_BigFloat_clear) | ||
b | ||
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::Float64) = BigFloat(x) | ||
|
||
promote_rule(::Type{BigFloat}, ::Type{Float32}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Float64}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Int8}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Int16}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Int32}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Int64}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Uint8}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Uint16}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Uint32}) = BigFloat | ||
promote_rule(::Type{BigFloat}, ::Type{Uint64}) = BigFloat | ||
|
||
function +(x::BigFloat, y::BigFloat) | ||
z= _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_add), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpf, y.mpf) | ||
BigFloat(z) | ||
end | ||
|
||
function -(x::BigFloat) | ||
z= _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_neg), Void, (Ptr{Void}, Ptr{Void}), z, x.mpf) | ||
BigFloat(z) | ||
end | ||
|
||
function -(x::BigFloat, y::BigFloat) | ||
z= _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_sub), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpf, y.mpf) | ||
BigFloat(z) | ||
end | ||
|
||
function *(x::BigFloat, y::BigFloat) | ||
z= _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_mul), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpf, y.mpf) | ||
BigFloat(z) | ||
end | ||
|
||
function div (x::BigFloat, y::BigFloat) | ||
z= _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_div), Void, (Ptr{Void}, Ptr{Void}, Ptr{Void}), z, x.mpf, y.mpf) | ||
BigFloat(z) | ||
end | ||
|
||
function cmp(x::BigFloat, y::BigFloat) | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_cmp), Int, (Ptr{Void}, Ptr{Void}), x.mpf, y.mpf) | ||
end | ||
|
||
function pow(x::BigFloat, y::Uint) | ||
z = _jl_BigFloat_init() | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_pow_ui), Void, (Ptr{Void}, Ptr{Void}, Uint), z, x.mpf, y) | ||
BigFloat(z) | ||
end | ||
|
||
==(x::BigFloat, y::BigFloat) = cmp(x,y) == 0 | ||
<=(x::BigFloat, y::BigFloat) = cmp(x,y) <= 0 | ||
>=(x::BigFloat, y::BigFloat) = cmp(x,y) >= 0 | ||
<(x::BigFloat, y::BigFloat) = cmp(x,y) < 0 | ||
>(x::BigFloat, y::BigFloat) = cmp(x,y) > 0 | ||
|
||
function string(x::BigFloat) | ||
s=ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_printf), Ptr{Uint8}, (Ptr{Void},), x.mpf) | ||
ret = cstring(s) #This copies s. | ||
_jl_free(convert(Ptr{Void},s)) | ||
ret | ||
end | ||
|
||
function show(x::BigFloat) | ||
print (string(x)) | ||
end | ||
|
||
function _jl_BigFloat_clear(x::BigFloat) | ||
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_clear), Void, (Ptr{Void},), x.mpf) | ||
end | ||
|
||
function _jl_BigFloat_init() | ||
return ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_init), Ptr{Void}, ()) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
load ("../jl/bigfloat.jl") | ||
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 !(b == a) | ||
@assert b > a | ||
@assert b >= a | ||
@assert !(b < a) | ||
@assert !(b <= a) | ||
|
||
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 | ||
|
||
d = BigFloat("-24.69135780242") | ||
@assert typeof(d) == BigFloat | ||
@assert d == -c | ||
|
||
#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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters