Skip to content

Commit

Permalink
fix #3399, BigFloat-BigInt comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Jun 4, 2014
1 parent 182f791 commit 6ba4232
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
19 changes: 19 additions & 0 deletions base/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,25 @@ end
<(x::BigFloat, y::BigFloat) = ccall((:mpfr_less_p, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &x, &y) != 0
>(x::BigFloat, y::BigFloat) = ccall((:mpfr_greater_p, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigFloat}), &x, &y) != 0

function ==(i::BigInt, f::BigFloat)
!isnan(f) && ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigInt}), &f, &i) == 0
end

==(f::BigFloat, i::BigInt) = i == f

function <(i::BigInt, f::BigFloat)
# note: mpfr_cmp_z returns 0 if isnan(f)
ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigInt}), &f, &i) > 0
end

function <(f::BigFloat, i::BigInt)
# note: mpfr_cmp_z returns 0 if isnan(f)
ccall((:mpfr_cmp_z, :libmpfr), Int32, (Ptr{BigFloat}, Ptr{BigInt}), &f, &i) < 0
end

<=(i::BigInt, f::BigFloat) = !isnan(f) && !(f < i)
<=(f::BigFloat, i::BigInt) = !isnan(f) && !(i < f)

signbit(x::BigFloat) = ccall((:mpfr_signbit, :libmpfr), Int32, (Ptr{BigFloat},), &x) != 0

function precision(x::BigFloat)
Expand Down
16 changes: 16 additions & 0 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -766,3 +766,19 @@ tol = 1e-3
@test typemax(Int128) * big(1.0) == convert(BigFloat, typemax(Int128))
@test typemax(Uint64) * big(1.0) == big(typemax(Uint64))
@test typemax(Uint128) * big(1.0) == big(typemax(Uint128))

# issue #3399
i1 = BigInt(10)^1000
i2 = BigInt("10000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000")
f = BigFloat(10)^1000
@test i1 != i2
@test i1 != f
@test i2 != f

@test f > i1
@test f > i2

i3 = itrunc(f)
@test i3 == f
@test i3+1 > f
@test i3+1 >= f

0 comments on commit 6ba4232

Please sign in to comment.