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

define round/trunc/ceil/floor to Bool #25085

Merged
merged 1 commit into from
Feb 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions base/float.jl
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,12 @@ floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDo
ceil(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundUp))
round(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundNearest))

# Bool
trunc(::Type{Bool}, x::AbstractFloat) = (-1 < x < 2) ? 1 <= x : throw(InexactError(:trunc, Bool, x))
floor(::Type{Bool}, x::AbstractFloat) = (0 <= x < 2) ? 1 <= x : throw(InexactError(:floor, Bool, x))
ceil(::Type{Bool}, x::AbstractFloat) = (-1 < x <= 1) ? 0 < x : throw(InexactError(:ceil, Bool, x))
round(::Type{Bool}, x::AbstractFloat) = (-0.5 <= x < 1.5) ? 0.5 < x : throw(InexactError(:round, Bool, x))

round(x::IEEEFloat, r::RoundingMode{:ToZero}) = trunc_llvm(x)
round(x::IEEEFloat, r::RoundingMode{:Down}) = floor_llvm(x)
round(x::IEEEFloat, r::RoundingMode{:Up}) = ceil_llvm(x)
Expand Down
42 changes: 42 additions & 0 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2813,3 +2813,45 @@ end
@test_throws MethodError fld(a, b)
@test_throws MethodError cld(a, b)
end

@testset "Bool rounding (#25074)" begin
@testset "round Bool" begin
@test_throws InexactError round(Bool, -4.1)
@test_throws InexactError round(Bool, 1.5)
@test true == round(Bool, 1.0)
@test false == round(Bool, 0.0)
@test true == round(Bool, 0.6)
@test false == round(Bool, 0.4)
@test false == round(Bool, 0.5)
@test false == round(Bool, -0.5)
end

@testset "trunc Bool" begin
@test_throws InexactError trunc(Bool, -4.1)
@test_throws InexactError trunc(Bool, 2.5)
@test true == trunc(Bool, 1.0)
@test false == trunc(Bool, 0.0)
@test false == trunc(Bool, 0.6)
@test false == trunc(Bool, 0.4)
@test true == trunc(Bool, 1.8)
@test false == trunc(Bool, -0.5)
end

@testset "floor Bool" begin
@test_throws InexactError floor(Bool, -0.1)
@test_throws InexactError floor(Bool, 2.5)
@test true == floor(Bool, 1.0)
@test false == floor(Bool, 0.0)
@test false == floor(Bool, 0.6)
@test true == floor(Bool, 1.8)
end

@testset "ceil Bool" begin
@test_throws InexactError ceil(Bool, -1.4)
@test_throws InexactError ceil(Bool, 1.5)
@test true == ceil(Bool, 1.0)
@test false == ceil(Bool, 0.0)
@test true == ceil(Bool, 0.6)
@test false == ceil(Bool, -0.7)
end
end