Dispatch of round()

hi here, I have a problem regarding the dispatching of round():

julia> @enter round(2.2)
[ Info: tracking Base
In round(x, r) at float.jl:380
>380  round(x::Float64, r::RoundingMode{:Nearest}) = rint_llvm(x)

julia> @enter round(2.2; digits = 2)
In #round#708(digits, sigdigits, base, , x, r) at floatfuncs.jl:129
 129  function round(x::Real, r::RoundingMode=RoundNearest;
 130                 digits::Union{Nothing,Integer}=nothing, sigdigits::Union{Nothing,Integer}=nothing, base::Union{Nothing,Integer}=nothing)

in particular, I wonder why calling round(2.2) would not give an error as the second parameter RoundingMode is not specified.

in float.jl see the following definitions:

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

round(x::Float64, r::RoundingMode{:ToZero})  = trunc_llvm(x)
round(x::Float32, r::RoundingMode{:ToZero})  = trunc_llvm(x)
round(x::Float16, r::RoundingMode{:ToZero})  = trunc_llvm(x)
round(x::Float64, r::RoundingMode{:Down})    = floor_llvm(x)
round(x::Float32, r::RoundingMode{:Down})    = floor_llvm(x)
round(x::Float16, r::RoundingMode{:Down})    = floor_llvm(x)
round(x::Float64, r::RoundingMode{:Up})      = ceil_llvm(x)
round(x::Float32, r::RoundingMode{:Up})      = ceil_llvm(x)
round(x::Float16, r::RoundingMode{:Up})      = ceil_llvm(x)
round(x::Float64, r::RoundingMode{:Nearest}) = rint_llvm(x)
round(x::Float32, r::RoundingMode{:Nearest}) = rint_llvm(x)
round(x::Float16, r::RoundingMode{:Nearest}) = rint_llvm(x)

whereas in floatfuncs.jl see:

function round(::Type{T}, x::AbstractFloat, r::RoundingMode) where {T<:Integer}

function round(x::Real, r::RoundingMode=RoundNearest;
               digits::Union{Nothing,Integer}=nothing, sigdigits::Union{Nothing,Integer}=nothing, base::Union{Nothing,Integer}=nothing)

again, no definition of round(x::Float64) = xxx is found.

thanks

This defines the method you’re looking for. The second argument has a default value assigned to it.

it’s NOT what @enter tells me???

julia> @enter round(2.2)
[ Info: tracking Base
In round(x, r) at float.jl:380
>380  round(x::Float64, r::RoundingMode{:Nearest}) = rint_llvm(x)

it says round(2.2) involves the definition in float.jl, not that in floatfuncs.jl as u mentioned.

Maybe @enter is getting confused by inlining?

julia> @which round(2.2)
round(x::Real) in Base at floatfuncs.jl:129
2 Likes