Skip to content

Commit

Permalink
Added test for big float
Browse files Browse the repository at this point in the history
  • Loading branch information
Corey M. Hoffstein committed Mar 6, 2012
1 parent c1dbf9b commit 9d4412d
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 21 deletions.
30 changes: 21 additions & 9 deletions external/gmp_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,18 @@ extern void _jl_mpf_set_ui(mpf_t* rop, unsigned long int op) {
mpf_set_ui(*rop, op);
}

extern void _jl_mpf_set_si(mpf_t* rop, signed long int op) {
mpf_set_si(*rop, op);
}

extern void _jl_mpf_set_d(mpf_t* rop, double op) {
mpf_set_d(*rop, op);
}

extern void _jl_mpf_set_z(mpf_t* rop, mpz_t* op) {
mpf_set_z(*rop, *op);
}

extern void _jl_mpf_add(mpf_t* rop, mpf_t* op1, mpf_t* op2) {
mpf_add(*rop, *op1, *op2);
}
Expand Down Expand Up @@ -126,27 +138,27 @@ extern void _jl_mpf_sqrt(mpf_t* rop, mpf_t* op) {

extern char* _jl_mpf_printf(mpf_t* rop) {
char* pp;
gmp_asprintf(&pp, "%Zd", *rop);
gmp_asprintf(&pp, "%.Ff", *rop);
return pp;
}


//Quick and dirty test of the gmp wrapper code
int main( int argc, const char* argv[] )
{
void* rop = _jl_mpz_init();
void* op1 = _jl_mpz_init();
void* rop = _jl_mpf_init();
void* op1 = _jl_mpf_init();

_jl_mpz_set_string(op1, "123456789123456789123456789123456789");
_jl_mpf_set_string(op1, "123456789123456789123456789123456789");

void* op2 = _jl_mpz_init();
_jl_mpz_set_string(op2, "12345");
void* op2 = _jl_mpf_init();
_jl_mpf_set_string(op2, "12345");

_jl_mpz_add(rop, op1, op2);
_jl_mpf_add(rop, op1, op2);

printf("The sum is %s\n", _jl_mpz_printf(rop));
printf("The sum is %s\n", _jl_mpf_printf(rop));

_jl_mpz_clear(rop);
_jl_mpf_clear(rop);
}


Expand Down
51 changes: 39 additions & 12 deletions jl/bigfloat.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
_jl_libgmp_wrapper = dlopen("libgmp_wrapper")

load("bigint.jl")

type BigFloat <: Float
mpf::Ptr{Void}

Expand All @@ -13,15 +15,31 @@ type BigFloat <: Float

function BigFloat(x::Float)
z = _jl_BigFloat_init()
ccall(dlsym(_jl_libgmp_wrapper, :_jl_mpf_set_ui), Void, (Ptr{Void}, Float), z, x)
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_ui), Void, (Ptr{Void}, Float), z, float(x))
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
Expand All @@ -34,20 +52,29 @@ type BigFloat <: Float
end
end

convert(::Type{BigFloat}, x::Float) = BigFloat(x)
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)

macro define_bigfloat_convert ()
if WORD_SIZE == 64
:(convert(::Type{BigFloat}, x::Float32) = BigFloat(float(x)))
else
:(convert(::Type{BigFloat}, x::Float64) = BigFloat(string(x)))
end
end

@define_bigfloat_convert
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()
Expand Down
34 changes: 34 additions & 0 deletions test/bigfloat.jl
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)

0 comments on commit 9d4412d

Please sign in to comment.