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

use iszero in isinteger(::AbstractFloat) #55276

Merged
merged 2 commits into from
Jul 29, 2024
Merged

use iszero in isinteger(::AbstractFloat) #55276

merged 2 commits into from
Jul 29, 2024

Conversation

stevengj
Copy link
Member

This is a very slight tweak to the implementation of isinteger(::AbstractFloat) to use iszero rather than == 0. It shouldn't make any difference with any of the built-in floating-point types, but iszero might conceivably be faster for some user-defined types.

I also added a comment to indicate why it's using iszero(x - trunc(x)) rather than x == trunc(x) (due to non-finite values); this code dates back to #14569 in Julia 0.5.

@stevengj stevengj added the maths Mathematical functions label Jul 27, 2024
Copy link
Contributor

@giordano giordano left a comment

Choose a reason for hiding this comment

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

It shouldn't make any difference with any of the built-in floating-point types

It may be marginally faster for BigFloat since it has a specialised iszero method not to run a ccall:

iszero(x::BigFloat) = x.exp == mpfr_special_exponent_zero

Bug regardless of the magnitude of the speedup for BigFloat, the change looks good to me.

base/floatfuncs.jl Outdated Show resolved Hide resolved
@jishnub jishnub merged commit 5da9468 into master Jul 29, 2024
7 checks passed
@jishnub jishnub deleted the stevengj-patch-3 branch July 29, 2024 04:07
lazarusA pushed a commit to lazarusA/julia that referenced this pull request Aug 17, 2024
This is a very slight tweak to the implementation of
`isinteger(::AbstractFloat)` to use `iszero` rather than `== 0`. It
shouldn't make any difference with any of the built-in floating-point
types, but `iszero` might conceivably be faster for some user-defined
types.

I also added a comment to indicate why it's using `iszero(x - trunc(x))`
rather than `x == trunc(x)` (due to non-finite values); this code dates
back to JuliaLang#14569 in Julia 0.5.

---------

Co-authored-by: Sukera <[email protected]>
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.

4 participants