Skip to content

Commit

Permalink
Support round/trunc/ceil/floor for Bool (#799)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtfishman authored Jun 23, 2023
1 parent 16043df commit 70ab162
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "4.6.1"
version = "4.7.0"

[deps]
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ changes in `julia`.

## Supported features

* `trunc`, `floor`, `ceil`, and `round` to `Bool`. ([#25085]) (since Compat 4.7.0)

* `splat(f)` which is equivalent to `args -> f(args...)`. ([#42717], [#48038]) (since Compat 4.6.0) (Note: for historical reasons, `Compat` on Julia before v1.9 also exports `Splat`; its usage is discouraged, however.)

* `Compat.@assume_effects setting... ex` overrides the compiler's effect modeling for the method definition `ex` on Julia versions that support this feature. Julia version without support just pass back `ex`. ([#43852]) (since Compat 4.4.0)
Expand Down Expand Up @@ -143,6 +145,7 @@ Note that you should specify the correct minimum version for `Compat` in the
`[compat]` section of your `Project.toml`, as given in above list.

[`@bdf9ead9`]: https://github.com/JuliaLang/julia/commit/bdf9ead91e5a8dfd91643a17c1626032faada329
[#25085]: https://github.com/JuliaLang/julia/issues/25085
[#29901]: https://github.com/JuliaLang/julia/issues/29901
[#35316]: https://github.com/JuliaLang/julia/issues/35316
[#36229]: https://github.com/JuliaLang/julia/issues/36229
Expand Down
9 changes: 9 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,15 @@ if v"1.10.0-" <= VERSION < v"1.10.0-DEV.360" || VERSION < v"1.9.0-beta3"
splat(f) = Splat(f)
end

# https://github.com/JuliaLang/julia/pull/25085
if VERSION < v"1.8.0-beta2.17" || v"1.9.0-" <= VERSION < v"1.9.0-DEV.94"
import Base: trunc, floor, ceil, round
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))
end

include("deprecated.jl")

end # module Compat
43 changes: 43 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,3 +591,46 @@ end
@test repr(MIME"text/plain"(), splat(+)) == "splat(+)"
end
end

# https://github.com/JuliaLang/julia/pull/25085
@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

2 comments on commit 70ab162

@martinholters
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/86120

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v4.7.0 -m "<description of version>" 70ab162f00c1ca8552b9af6a40a7a267072c34c4
git push origin v4.7.0

Please sign in to comment.