Skip to content

Commit 67c02de

Browse files
JeffreySarnoffsimonbyrne
authored andcommitted
add modf, BigInt conversion (#13)
1 parent 973520e commit 67c02de

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/Quadmath.jl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ import Base: (*), +, -, /, <, <=, ==, ^, convert,
1313
tan, tanh,
1414
ceil, floor, trunc, round, fma,
1515
copysign, flipsign, max, min, hypot, abs,
16-
ldexp, frexp, nextfloat,
17-
eps, isinf, isnan, isfinite, floatmin, floatmax, precision, signbit,
18-
Int32,Int64,Float64,BigFloat
16+
ldexp, frexp, modf, nextfloat, eps,
17+
isinf, isnan, isfinite, isinteger,
18+
floatmin, floatmax, precision, signbit,
19+
Int32, Int64, Float64, BigFloat, BigInt
1920

2021
if Sys.isapple()
2122
const quadoplib = "libquadmath.0"
@@ -186,6 +187,7 @@ isinf(x::Float128) =
186187
0 != ccall((:isinfq,libquadmath), Cint, (Cfloat128, ), x)
187188
isfinite(x::Float128) =
188189
0 != ccall((:finiteq,libquadmath), Cint, (Cfloat128, ), x)
190+
isinteger(x::Float128) = isfinite(x) && x === trunc(x)
189191

190192
signbit(x::Float128) = signbit(reinterpret(Int128, x))
191193
precision(::Type{Float128}) = 113
@@ -194,7 +196,6 @@ eps(::Type{Float128}) = reinterpret(Float128, 0x3f8f_0000_0000_0000_0000_0000_00
194196
floatmin(::Type{Float128}) = reinterpret(Float128, 0x0001_0000_0000_0000_0000_0000_0000_0000)
195197
floatmax(::Type{Float128}) = reinterpret(Float128, 0x7ffe_ffff_ffff_ffff_ffff_ffff_ffff_ffff)
196198

197-
198199
ldexp(x::Float128, n::Cint) =
199200
Float128(ccall((:ldexpq, libquadmath), Cfloat128, (Cfloat128, Cint), x, n))
200201
ldexp(x::Float128, n::Integer) =
@@ -206,6 +207,13 @@ function frexp(x::Float128)
206207
return y, Int(r[])
207208
end
208209

210+
function modf(x::Float128)
211+
isinf(x) && return (zero(Float128), x)
212+
ipart = trunc(x)
213+
fpart = x - ipart
214+
return fpart, ipart
215+
end
216+
209217
significand(x::Float128) = frexp(x)[1] * 2
210218
function exponent(x::Float128)
211219
!isfinite(x) && throw(DomainError("Cannot be NaN or Inf."))
@@ -224,7 +232,7 @@ function nextfloat(f::Float128, d::Integer)
224232
fu = unsigned(fi & typemax(fi))
225233

226234
dneg = d < 0
227-
da = uabs(d)
235+
da = Base.uabs(d)
228236
if da > typemax(U)
229237
fneg = dneg
230238
fu = fumax
@@ -344,7 +352,11 @@ function Float128(x::BigFloat)
344352
return copysign(z,x)
345353
end
346354

347-
355+
function BigInt(x::Float128)
356+
!isinteger(x) && throw(InexactError(:BigInt, x))
357+
BigInt(BigFloat(x, precision=precision(Float128)))
358+
end
359+
Float128(x::BigInt) = Float128(BigFloat(x, precision=precision(Float128)))
348360

349361
promote_rule(::Type{Float128}, ::Type{Float16}) = Float128
350362
promote_rule(::Type{Float128}, ::Type{Float32}) = Float128

0 commit comments

Comments
 (0)