Skip to content

Commit 6202475

Browse files
authored
add trunc(BigInt, ::Float16) (#40849)
1 parent 5754473 commit 6202475

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

base/gmp.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,14 +292,14 @@ BigInt(x::Union{Clong,Int32}) = MPZ.set_si(x)
292292
BigInt(x::Union{Culong,UInt32}) = MPZ.set_ui(x)
293293
BigInt(x::Bool) = BigInt(UInt(x))
294294

295-
unsafe_trunc(::Type{BigInt}, x::Union{Float32,Float64}) = MPZ.set_d(x)
295+
unsafe_trunc(::Type{BigInt}, x::Union{Float16,Float32,Float64}) = MPZ.set_d(x)
296296

297-
function BigInt(x::Union{Float32,Float64})
297+
function BigInt(x::Float64)
298298
isinteger(x) || throw(InexactError(:BigInt, BigInt, x))
299299
unsafe_trunc(BigInt,x)
300300
end
301301

302-
function trunc(::Type{BigInt}, x::Union{Float32,Float64})
302+
function trunc(::Type{BigInt}, x::Union{Float16,Float32,Float64})
303303
isfinite(x) || throw(InexactError(:trunc, BigInt, x))
304304
unsafe_trunc(BigInt,x)
305305
end

test/gmp.jl

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -366,28 +366,24 @@ end
366366
@test_throws InexactError convert(BigInt, 2.1)
367367
@test_throws InexactError convert(BigInt, big(2.1))
368368
end
369-
@testset "issue #13367" begin
370-
@test trunc(BigInt,2.1) == 2
371-
@test round(BigInt,2.1) == 2
372-
@test floor(BigInt,2.1) == 2
373-
@test ceil(BigInt,2.1) == 3
374-
375-
@test trunc(BigInt,2.1f0) == 2
376-
@test round(BigInt,2.1f0) == 2
377-
@test floor(BigInt,2.1f0) == 2
378-
@test ceil(BigInt,2.1f0) == 3
379-
380-
@test_throws InexactError trunc(BigInt,Inf)
381-
@test_throws InexactError round(BigInt,Inf)
382-
@test_throws InexactError floor(BigInt,Inf)
383-
@test_throws InexactError ceil(BigInt,Inf)
384-
385-
@test string(big(3), base = 2) == "11"
386-
@test string(big(9), base = 8) == "11"
387-
@test string(-big(9), base = 8) == "-11"
388-
@test string(big(12), base = 16) == "c"
369+
@testset "truncation" begin
370+
# cf. issue #13367
371+
for T = (Float16, Float32, Float64)
372+
@test trunc(BigInt, T(2.1)) == 2
373+
@test unsafe_trunc(BigInt, T(2.1)) == 2
374+
@test round(BigInt, T(2.1)) == 2
375+
@test floor(BigInt, T(2.1)) == 2
376+
@test ceil(BigInt, T(2.1)) == 3
377+
378+
@test_throws InexactError trunc(BigInt, T(Inf))
379+
@test_throws InexactError round(BigInt, T(Inf))
380+
@test_throws InexactError floor(BigInt, T(Inf))
381+
@test_throws InexactError ceil(BigInt, T(Inf))
382+
end
389383
end
390-
@testset "Issue #18849" begin
384+
385+
@testset "string(::BigInt)" begin
386+
# cf. issue #18849"
391387
# bin, oct, dec, hex should not call sizeof on BigInts
392388
# when padding is desired
393389
padding = 4
@@ -412,14 +408,19 @@ end
412408
@test string(-high, pad = padding, base = 8) == "-4000000"
413409
@test string(-high, pad = padding, base = 10) == "-1048576"
414410
@test string(-high, pad = padding, base = 16) == "-100000"
415-
end
416411

417-
# respect 0-padding on big(0)
418-
for base in (2, 8, 10, 16)
419-
local base
420-
@test string(big(0), base=base, pad=0) == ""
412+
# cf. issue #13367
413+
@test string(big(3), base = 2) == "11"
414+
@test string(big(9), base = 8) == "11"
415+
@test string(-big(9), base = 8) == "-11"
416+
@test string(big(12), base = 16) == "c"
417+
418+
# respect 0-padding on big(0)
419+
for base in (2, 8, 10, 16)
420+
@test string(big(0), base=base, pad=0) == ""
421+
end
422+
@test string(big(0), base = rand(2:62), pad = 0) == ""
421423
end
422-
@test string(big(0), base = rand(2:62), pad = 0) == ""
423424

424425
@test isqrt(big(4)) == 2
425426
@test isqrt(big(5)) == 2

0 commit comments

Comments
 (0)