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

extend iseven/isodd to Number #38976

Merged
merged 2 commits into from
Dec 25, 2020
Merged

extend iseven/isodd to Number #38976

merged 2 commits into from
Dec 25, 2020

Conversation

stevengj
Copy link
Member

@stevengj stevengj commented Dec 23, 2020

Similar to #37635 for ispow2, it seems like there is no reason to restrict these functions to Integer types.

I ran into this recently in SpecialFunctions.jl when I was trying to compute something like (-z)^a Γ(z) by sign * exp(a*log(abs(z)) + logabsgamma(z)) for isinteger(a), but where a is not necessarily an Integer type. In order to compute the sign, I wanted to check iseven(a), but it didn't work for floating-point types. And iseven(Int(a)) could overflow, so I couldn't use that without an additional guard.

@stevengj stevengj added the maths Mathematical functions label Dec 23, 2020
Copy link
Member

@Sacha0 Sacha0 left a 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))
Copy link
Member

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

Copy link
Member Author

@stevengj stevengj Dec 25, 2020

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}.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, right, thanks

@Keno Keno merged commit 570384e into master Dec 25, 2020
@Keno Keno deleted the sgj/iseven branch December 25, 2020 01:59
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
* extend iseven/isodd to Number

* add PR num to NEWS
rfourquet added a commit that referenced this pull request May 17, 2021
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.
JeffBezanson pushed a commit that referenced this pull request May 18, 2021
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.
shirodkara pushed a commit to shirodkara/julia that referenced this pull request Jun 9, 2021
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.
johanmon pushed a commit to johanmon/julia that referenced this pull request Jul 5, 2021
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants