Skip to content

Commit

Permalink
Add ifloor, iceil and itrunc to BigFloat (JuliaLang#3040)
Browse files Browse the repository at this point in the history
They convert to a Int64 if it fits, otherwise to BigInt.
  • Loading branch information
andrioni committed May 8, 2013
1 parent 22e6ba5 commit 78e653b
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import
isinf, isnan, ldexp, log, log2, log10, max, min, mod, modf, nextfloat,
prevfloat, promote_rule, rem, round, show, showcompact, sum, sqrt,
string, trunc, get_precision, exp10, expm1, gamma, lgamma, digamma,
erf, erfc, zeta, log1p, airyai,
erf, erfc, zeta, log1p, airyai, iceil, ifloor, itrunc,
# import trigonometric functions
sin, cos, tan, sec, csc, cot, acos, asin, atan, cosh, sinh, tanh,
sech, csch, coth, acosh, asinh, atanh, atan2
Expand Down Expand Up @@ -347,6 +347,20 @@ for f in (:ceil, :floor, :trunc)
end
end

for (f, g) in ((:iceil, :ceil), (:ifloor, :floor), (:itrunc, :trunc))
@eval begin
function ($f)(x::BigFloat)
fits = ccall((:mpfr_fits_slong_p, :libmpfr), Int32, (Ptr{BigFloat}, Int32), &x, RoundUp)
if fits != 0
z = BigFloat()
ccall(($(string(:mpfr_,g)), :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &z, &x)
return ccall((:mpfr_get_si, :libmpfr), Clong, (Ptr{BigFloat}, Int32), &z, ROUNDING_MODE[end])
end
return convert(BigInt, ($g)(x))
end
end
end

function ^(x::BigFloat, y::Unsigned)
z = BigFloat()
ccall((:mpfr_pow_ui, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}, Culong, Int32), &z, &x, y, ROUNDING_MODE[end])
Expand Down
26 changes: 26 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,32 @@ with_bigfloat_precision(53) do
@test ldexp(BigFloat(24.5), 0x48) == ldexp(24.5, 72)
end

# ceil / iceil / floor / ifloor / trunc / itrunc
x = BigFloat("28273.2312487489135135135")
y = BigInt(28273)
z = BigInt(28274)
a = BigFloat("123456789012345678901234567890.2414")
b = BigInt("123456789012345678901234567890")
c = BigInt("123456789012345678901234567891")
@test ceil(x) == z
@test typeof(ceil(x)) == BigFloat
@test floor(x) == y
@test typeof(floor(x)) == BigFloat
@test trunc(x) == y
@test typeof(trunc(x)) == BigFloat
@test iceil(x) == z
@test typeof(iceil(x)) == Int64
@test ifloor(x) == y
@test typeof(ifloor(x)) == Int64
@test itrunc(x) == y
@test typeof(itrunc(x)) == Int64
@test iceil(a) == c
@test typeof(iceil(a)) == BigInt
@test ifloor(a) == b
@test typeof(ifloor(a)) == BigInt
@test itrunc(a) == b
@test typeof(itrunc(a)) == BigInt

# basic arithmetic
# Signed addition
a = BigFloat("123456789012345678901234567890")
Expand Down

0 comments on commit 78e653b

Please sign in to comment.