-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Add ispositive
, etc.
#53677
base: master
Are you sure you want to change the base?
Add ispositive
, etc.
#53677
Conversation
For additional context: JuliaRegistries/General#102561 |
I'll just remark on Needs NaN handling. For example, julia> ispositive(NaN) ⊻ ispositive(-NaN)
true A possible fix is in the flavor of Also needs proper Now for your benchmarks (which appear to implement a "isnonpositive" function that does not appear in this PR, but I'll roll with it for now): julia> using BenchmarkTools
julia> f(x) = x<=zero(typeof(x));
julia> g(x) = signbit(x)||iszero(x);
julia> g_fixed(x) = !isnan(x)&&(signbit(x)||iszero(x));
julia> h(x) = x <= false;
julia> h_int(x) = x <= 0;
julia> vi32 = rand(Int32,1000); vf64 = randn(Float64,1000); vbf = randn(BigFloat,1000);
julia> @btime count(f,$vi32); @btime count(g,$vi32); @btime count(g_fixed,$vi32); @btime count(h,$vi32); @btime count(h_int,$vi32)
84.528 ns (0 allocations: 0 bytes)
84.112 ns (0 allocations: 0 bytes)
84.495 ns (0 allocations: 0 bytes)
84.079 ns (0 allocations: 0 bytes)
82.622 ns (0 allocations: 0 bytes)
517
julia> @btime count(f,$vf64); @btime count(g,$vf64); @btime count(g_fixed,$vf64); @btime count(h,$vf64); @btime count(h_int,$vf64)
61.914 ns (0 allocations: 0 bytes)
90.900 ns (0 allocations: 0 bytes)
134.514 ns (0 allocations: 0 bytes)
59.470 ns (0 allocations: 0 bytes)
58.163 ns (0 allocations: 0 bytes)
489
julia> @btime count(f,$vbf); @btime count(g,$vbf); @btime count(g_fixed,$vbf); @btime count(h,$vbf); @btime count(h_int,$vbf)
25.100 μs (2000 allocations: 101.56 KiB)
12.900 μs (0 allocations: 0 bytes)
14.000 μs (0 allocations: 0 bytes)
54.900 μs (2000 allocations: 39.06 KiB)
14.200 μs (0 allocations: 0 bytes)
498 Don't get hung up on differences of a few ns, which are probably benchmarking artifacts. But notice that this function is notably slower for native floats (and much slower when correctly handling NaN). And it doesn't out-do simple comparisons for My preferred implementation would be |
Thanks for the investigation! Looks like the problem is more complex than what I've imagined. Are you on the dev version of Julia? My benchmark results are a little different. Anyway, the methods need different implementations for different types. I'm not sure which the fallback should be. |
One thought: rather than defining these, would it be better to define a function which returns a Real or Number, say |
How does that help? |
Don't forget to add a compatibility note for the added functions (they are likely to be a part of Julia 1.12.0). |
Would it make more sense for I still think that it might be worth exploring faster inequality checks for |
Co-authored-by: Mosè Giordano <giordano@users.noreply.github.com>
I can think of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the docstrings need to be added to the manual, no?
Should they be with |
Having to choose, I'd probably pick |
ispositive
&isnonnegative
? #35067It's faster for some complicated Real types.
If you think this is a good idea then I'll add docstrings and tests.
Edit: FAQs according to discussions below
ispositive(x::Bool) = x
?ispositive
, etc. #53677 (comment)ispositive(::Any)
?ispositive
&isnonnegative
? #35067TODO:
NEWS.md
.ispositive
, etc. #53677 (comment)