Skip to content

Commit

Permalink
use iszero in isinteger(::AbstractFloat) (#55276)
Browse files Browse the repository at this point in the history
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.

---------

Co-authored-by: Sukera <11753998+Seelengrab@users.noreply.github.com>
  • Loading branch information
stevengj and Seelengrab authored Jul 29, 2024
1 parent 079b69e commit 5da9468
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion base/floatfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ it is the minimum of `maxintfloat(T)` and [`typemax(S)`](@ref).
maxintfloat(::Type{S}, ::Type{T}) where {S<:AbstractFloat, T<:Integer} = min(maxintfloat(S), S(typemax(T)))
maxintfloat() = maxintfloat(Float64)

isinteger(x::AbstractFloat) = (x - trunc(x) == 0)
isinteger(x::AbstractFloat) = iszero(x - trunc(x)) # note: x == trunc(x) would be incorrect for x=Inf

# See rounding.jl for docstring.

Expand Down

0 comments on commit 5da9468

Please sign in to comment.