From 377fe4c984c729754a0169500e1ee406b88e72d3 Mon Sep 17 00:00:00 2001 From: Simon Byrne Date: Mon, 29 Jun 2015 17:11:04 +0100 Subject: [PATCH] add div function for BigFloats --- base/mpfr.jl | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++- test/mpfr.jl | 9 ++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/base/mpfr.jl b/base/mpfr.jl index 5d6aaf88ce6b7..9f449c6747721 100644 --- a/base/mpfr.jl +++ b/base/mpfr.jl @@ -12,7 +12,7 @@ export import Base: (*), +, -, /, <, <=, ==, >, >=, ^, besselj, besselj0, besselj1, bessely, - bessely0, bessely1, ceil, cmp, convert, copysign, deg2rad, + bessely0, bessely1, ceil, cmp, convert, copysign, deg2rad, div, exp, exp2, exponent, factorial, floor, fma, hypot, isinteger, isfinite, isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat, prevfloat, promote_rule, rad2deg, rem, round, show, @@ -309,6 +309,65 @@ function fma(x::BigFloat, y::BigFloat, z::BigFloat) return r end +# div +# BigFloat +function div(x::BigFloat, y::BigFloat) + z = BigFloat() + ccall((:mpfr_div,:libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Ptr{BigFloat}, Int32), &z, &x, &y, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end + +# Unsigned Int +function div(x::BigFloat, c::CulongMax) + z = BigFloat() + ccall(($(string(:mpfr_div_ui)), :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Culong, Int32), &z, &x, c, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end +function div(c::CulongMax, x::BigFloat) + z = BigFloat() + ccall((:mpfr_ui_div, :libmpfr), Int32, (Ptr{BigFloat}, Culong, Ptr{BigFloat}, Int32), &z, c, &x, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end + +# Signed Integer +function div(x::BigFloat, c::ClongMax) + z = BigFloat() + ccall((:mpfr_div_si, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Clong, Int32), &z, &x, c, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end +function div(c::ClongMax, x::BigFloat) + z = BigFloat() + ccall((:mpfr_si_div, :libmpfr), Int32, (Ptr{BigFloat}, Clong, Ptr{BigFloat}, Int32), &z, c, &x, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end + +# Float32/Float64 +function div(x::BigFloat, c::CdoubleMax) + z = BigFloat() + ccall((:mpfr_div_d, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Cdouble, Int32), &z, &x, c, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end +function div(c::CdoubleMax, x::BigFloat) + z = BigFloat() + ccall((:mpfr_d_div, :libmpfr), Int32, (Ptr{BigFloat}, Cdouble, Ptr{BigFloat}, Int32), &z, c, &x, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end + +# BigInt +function div(x::BigFloat, c::BigInt) + z = BigFloat() + ccall((:mpfr_div_z, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Ptr{BigInt}, Int32), &z, &x, &c, to_mpfr(RoundToZero)) + ccall((:mpfr_trunc, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &z) + return z +end + # More efficient commutative operations diff --git a/test/mpfr.jl b/test/mpfr.jl index d24ebe0e23cc3..457a8ffba892f 100644 --- a/test/mpfr.jl +++ b/test/mpfr.jl @@ -729,6 +729,15 @@ f = parse(BigFloat,"6.2230152778611417071440640537801242405902521687211671331011 # BigInt division @test a / BigInt(2) == c +# div +@test div(big"1.0",big"0.1") == 9 +@test div(1,big"0.1") == 9 +@test div(1.0,big"0.1") == 9 +@test div(big"1.0",0.1) == 9 +@test div(big"1",big"0.1") == 9 +@test div(big"1",0.1) == 9 + + # old tests tol = 1e-12