Definition of floor

Hi,
In base/float.jl I don’t understand the definition of floor: Here

floor(::Type{T}, x::AbstractFloat) where {T<:Integer} = trunc(T,round(x, RoundDown))

I don’t get the round. What I understood is: trunc(.) = round(., RoundDown) but I am probably missing something.
Thank you for the help.

julia> round(1.5, RoundDown)  # You do not want the default: r::RoundingMode=RoundNearest, would get you 2.0
1.0

julia> trunc(Int64, 1.0)  # to get you an integer, i.e. get rid of ".0"
1

You can do similar to this in most contexts, but the rest, e.g. “where” is to work for any type T (that applies, i.e. subtype of Integer), e.g. Int8, not just Int64. This is good to know for generic code, and the standard library wants to be fully generic.

1 Like

No, trunc uses RoundToZero.

julia> floor(-1.4)
-2.0

julia> trunc(-1.4)
-1.0
3 Likes

Thanks for the precision, it took me some time to be familiar with where T notations.

1 Like

Now that you say it… I forgot the negative numbers :man_facepalming:t2:. Whoops. Thank you!