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

Make round(::Type{Bool}, ::AbstractFloat) work #25074

Closed
oxinabox opened this issue Dec 14, 2017 · 2 comments · Fixed by #25085
Closed

Make round(::Type{Bool}, ::AbstractFloat) work #25074

oxinabox opened this issue Dec 14, 2017 · 2 comments · Fixed by #25085

Comments

@oxinabox
Copy link
Contributor

Bool is the only integer subtype that never works with round, with a Float.

From #gripes on slack:

Lyndon White

I feel like round(Bool, 0.7) should return true. Not give an error

Alex Arslan

Why?
An error seems much more reasonable IMO

Lyndon White

Because Bool <: Number
we basically use bools as UInt1

Alex Arslan

Bool as a Number type is like the 2 of prime numbers

Lyndon White

accuracy = mean(predictions==ground_truth)
works great
Bool(round(UInt8, 0.7)) works without error
as round(Bool, 1) works (edited)
Bool is like the only integer subtype we are apparently never allowed to round a float to.

Simon Byrne

I'd be on board with that
We generally take round(T, x) to be equivalent to convert(T, round(x)).

Jacob Quinn

yeah, seems reasonable to me.

Stefan Karpinski

As long as Bool is an integer type it should

@ararslan
Copy link
Member

round(::Type{Bool}, x::AbstractFloat) = Bool(round(UInt8, x))

seems like a fairly reasonable definition.

@oxinabox
Copy link
Contributor Author

Need to pass:

@testset "Round Bool" begin
    @test_throws InexactError round(Bool, -4.1)
    @test_throws InexactError round(Bool, 7.3)
    @test true == round(Bool, 1.0)
    @test false == round(Bool, 0.0)
    @test true == round(Bool, 0.6)
    @test false == round(Bool, 0.4)
end

Which

round(::Type{Bool}, x::AbstractFloat) = Bool(round(UInt8, x))`

Does do so.
Seems best.

Base.round(::Type{Bool}, x::AbstractFloat) = x>0.5 fails the first two tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants