Skip to content

Commit

Permalink
use iszero in isinteger(::AbstractFloat) (JuliaLang#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 JuliaLang#14569 in Julia 0.5.

---------

Co-authored-by: Sukera <[email protected]>
  • Loading branch information
2 people authored and lazarusA committed Aug 17, 2024
1 parent e9bfc29 commit 8184d65
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 8184d65

Please sign in to comment.