Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combine signif into round, use keyword args for digits/sigdigits #26670

Merged
merged 20 commits into from
Apr 6, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
modify tests
  • Loading branch information
simonbyrne committed Mar 30, 2018
commit 3ffc9466fcca44d0ad71b674108cf079b169e03d
40 changes: 20 additions & 20 deletions test/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,24 +79,24 @@ end
@testset "significant digits" begin
# (would be nice to have a smart vectorized
# version of signif)
@test signif(123.456, 1) ≈ 100.
@test signif(123.456, 3) ≈ 123.
@test signif(123.456, 5) ≈ 123.46
@test signif(123.456, 8, base = 2) ≈ 123.5
@test signif(123.456, 2, base = 4) ≈ 128.0
@test signif(0.0, 1) === 0.0
@test signif(-0.0, 1) === -0.0
@test signif(1.2, 2) === 1.2
@test signif(1.0, 6) === 1.0
@test signif(0.6, 1) === 0.6
@test signif(7.262839104539736, 2) === 7.3
@test isinf(signif(Inf, 3))
@test isnan(signif(NaN, 3))
@test signif(1.12312, 1000) === 1.12312
@test signif(Float32(7.262839104539736), 3) === Float32(7.26)
@test signif(Float32(7.262839104539736), 4) === Float32(7.263)
@test signif(Float32(1.2), 3) === Float32(1.2)
@test signif(Float32(1.2), 5) === Float32(1.2)
@test signif(Float16(0.6), 2) === Float16(0.6)
@test signif(Float16(1.1), 70) === Float16(1.1)
@test round(123.456, sigdigits=1) ≈ 100.
@test round(123.456, sigdigits=3) ≈ 123.
@test round(123.456, sigdigits=5) ≈ 123.46
@test round(123.456, sigdigits=8, base = 2) ≈ 123.5
@test round(123.456, sigdigits=2, base = 4) ≈ 128.0
@test round(0.0, sigdigits=1) === 0.0
@test round(-0.0, sigdigits=1) === -0.0
@test round(1.2, sigdigits=2) === 1.2
@test round(1.0, sigdigits=6) === 1.0
@test round(0.6, sigdigits=1) === 0.6
@test round(7.262839104539736, sigdigits=2) === 7.3
@test isinf(round(Inf, sigdigits=3))
@test isnan(round(NaN, sigdigits=3))
@test round(1.12312, sigdigits=1000) === 1.12312
@test round(Float32(7.262839104539736), sigdigits=3) === Float32(7.26)
@test round(Float32(7.262839104539736), sigdigits=4) === Float32(7.263)
@test round(Float32(1.2), sigdigits=3) === Float32(1.2)
@test round(Float32(1.2), sigdigits=5) === Float32(1.2)
@test round(Float16(0.6), sigdigits=2) === Float16(0.6)
@test round(Float16(1.1), sigdigits=70) === Float16(1.1)
end
82 changes: 41 additions & 41 deletions test/rounding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -265,65 +265,65 @@ end

# custom rounding and significant-digit ops
@testset "rounding to digits relative to the decimal point" begin
@test round(pi,0) ≈ 3.
@test round(pi,1) ≈ 3.1
@test round(10*pi,-1) ≈ 30.
@test round(.1,0) == 0.
@test round(-.1,0) == -0.
@test isnan(round(NaN, 2))
@test isinf(round(Inf,2))
@test isinf(round(-Inf,2))
@test round(pi, digits=0) ≈ 3.
@test round(pi, digits=1) ≈ 3.1
@test round(10*pi, digits=-1) ≈ 30.
@test round(.1, digits=0) == 0.
@test round(-.1, digits=0) == -0.
@test isnan(round(NaN, digits=2))
@test isinf(round(Inf, digits=2))
@test isinf(round(-Inf, digits=2))
end
@testset "round vs trunc vs floor vs ceil" begin
@test round(123.456,1) ≈ 123.5
@test round(-123.456,1) ≈ -123.5
@test trunc(123.456,1) ≈ 123.4
@test trunc(-123.456,1) ≈ -123.4
@test ceil(123.456,1) ≈ 123.5
@test ceil(-123.456,1) ≈ -123.4
@test floor(123.456,1) ≈ 123.4
@test floor(-123.456,1) ≈ -123.5
@test round(123.456, digits=1) ≈ 123.5
@test round(-123.456, digits=1) ≈ -123.5
@test trunc(123.456, digits=1) ≈ 123.4
@test trunc(-123.456, digits=1) ≈ -123.4
@test ceil(123.456, digits=1) ≈ 123.5
@test ceil(-123.456, digits=1) ≈ -123.4
@test floor(123.456, digits=1) ≈ 123.4
@test floor(-123.456, digits=1) ≈ -123.5
end
@testset "rounding with too much (or too few) precision" begin
for x in (12345.6789, 0, -12345.6789)
y = float(x)
@test y == trunc(x, 1000)
@test y == round(x, 1000)
@test y == floor(x, 1000)
@test y == ceil(x, 1000)
@test y == trunc(x, digits=1000)
@test y == round(x, digits=1000)
@test y == floor(x, digits=1000)
@test y == ceil(x, digits=1000)
end
let x = 12345.6789
@test 0.0 == trunc(x, -1000)
@test 0.0 == round(x, -1000)
@test 0.0 == floor(x, -1000)
@test Inf == ceil(x, -1000)
@test 0.0 == trunc(x, digits=-1000)
@test 0.0 == round(x, digits=-1000)
@test 0.0 == floor(x, digits=-1000)
@test Inf == ceil(x, digits=-1000)
end
let x = -12345.6789
@test -0.0 == trunc(x, -1000)
@test -0.0 == round(x, -1000)
@test -Inf == floor(x, -1000)
@test -0.0 == ceil(x, -1000)
@test -0.0 == trunc(x, digits=-1000)
@test -0.0 == round(x, digits=-1000)
@test -Inf == floor(x, digits=-1000)
@test -0.0 == ceil(x, digits=-1000)
end
let x = 0.0
@test 0.0 == trunc(x, -1000)
@test 0.0 == round(x, -1000)
@test 0.0 == floor(x, -1000)
@test 0.0 == ceil(x, -1000)
@test 0.0 == trunc(x, digits=-1000)
@test 0.0 == round(x, digits=-1000)
@test 0.0 == floor(x, digits=-1000)
@test 0.0 == ceil(x, digits=-1000)
end
end
@testset "rounding in other bases" begin
@test round(pi, 2, base = 2) ≈ 3.25
@test round(pi, 3, base = 2) ≈ 3.125
@test round(pi, 3, base = 5) ≈ 3.144
@test round(pi, digits = 2, base = 2) ≈ 3.25
@test round(pi, digits = 3, base = 2) ≈ 3.125
@test round(pi, digits = 3, base = 5) ≈ 3.144
end
@testset "vectorized trunc/round/floor/ceil with digits/base argument" begin
a = rand(2, 2, 2)
for f in (round, trunc, floor, ceil)
@test f.(a[:, 1, 1], 2) == map(x->f(x, 2), a[:, 1, 1])
@test f.(a[:, :, 1], 2) == map(x->f(x, 2), a[:, :, 1])
@test f.(a, 9, base = 2) == map(x->f(x, 9, base = 2), a)
@test f.(a[:, 1, 1], 9, base = 2) == map(x->f(x, 9, base = 2), a[:, 1, 1])
@test f.(a[:, :, 1], 9, base = 2) == map(x->f(x, 9, base = 2), a[:, :, 1])
@test f.(a, 9, base = 2) == map(x->f(x, 9, base = 2), a)
@test f.(a[:, 1, 1], digits=2) == map(x->f(x, digits=2), a[:, 1, 1])
@test f.(a[:, :, 1], digits=2) == map(x->f(x, digits=2), a[:, :, 1])
@test f.(a, digits=9, base = 2) == map(x->f(x, digits=9, base = 2), a)
@test f.(a[:, 1, 1], digits=9, base = 2) == map(x->f(x, digits=9, base = 2), a[:, 1, 1])
@test f.(a[:, :, 1], digits=9, base = 2) == map(x->f(x, digits=9, base = 2), a[:, :, 1])
@test f.(a, digits=9, base = 2) == map(x->f(x, digits=9, base = 2), a)
end
end