-
-
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
extend iseven/isodd to Number #38976
Conversation
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.
Looks great! Thanks Steven! :)
@@ -122,7 +123,8 @@ julia> iseven(10) | |||
true | |||
``` | |||
""" | |||
iseven(n::Integer) = !isodd(n) | |||
iseven(n::Number) = isreal(n) && iseven(real(n)) | |||
iseven(n::Real) = isinteger(n) && iszero(rem(Integer(n), 2)) |
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.
Doesn't the cast to Integer here defeat the purpose?
julia> Integer(2.0^65)
ERROR: InexactError: Int64(3.6893488147419103e19)
Stacktrace:
[1] Int64
@ ./float.jl:747 [inlined]
[2] Integer(x::Float64)
@ Core ./boot.jl:789
[3] top-level scope
@ REPL[2]:1
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.
No, because there is a specialized method for AbstractFloat
in float.jl
.
This is just the fallback definition, which works for things like Complex{Int}
and Rational{Int}
.
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.
Ah, right, thanks
* extend iseven/isodd to Number * add PR num to NEWS
This method was depending on the generic implementation `iseven(x::Integer) = !isodd(x)` for its efficiency, but this was removed in #38976, making `iseven(::BigInt)` 30 times slower.
This method was depending on the generic implementation `iseven(x::Integer) = !isodd(x)` for its efficiency, but this was removed in JuliaLang#38976, making `iseven(::BigInt)` 30 times slower.
This method was depending on the generic implementation `iseven(x::Integer) = !isodd(x)` for its efficiency, but this was removed in JuliaLang#38976, making `iseven(::BigInt)` 30 times slower.
Similar to #37635 for
ispow2
, it seems like there is no reason to restrict these functions toInteger
types.I ran into this recently in SpecialFunctions.jl when I was trying to compute something like
(-z)^a Γ(z)
bysign * exp(a*log(abs(z)) + logabsgamma(z))
forisinteger(a)
, but wherea
is not necessarily anInteger
type. In order to compute thesign
, I wanted to checkiseven(a)
, but it didn't work for floating-point types. Andiseven(Int(a))
could overflow, so I couldn't use that without an additional guard.