Skip to content

Inconsistent int-float promotion in hypot #53505

Closed
@danielwe

Description

@danielwe

hypot(::Int64, ::Float32) promotes the arguments and result to Float64, contrary to the behavior of promote, which gives Float32 in this case.

julia> hypot(1, 3.0f0)
3.1622776601683795

julia> promote(1, 3.0f0)
(1.0f0, 3.0f0)

The culprits are the following method definitions:

julia/base/math.jl

Lines 798 to 799 in 71f68b4

hypot(x::Number, y::Number) = _hypot(promote(float(x), y)...)
hypot(x::Number, y::Number, xs::Number...) = _hypot(promote(float(x), y, xs...))

Seems like this could be rewritten as follows for consistency with promote:

hypot(x::Number, y::Number) = _hypot(float.(promote(x, y))...)
hypot(x::Number, y::Number, xs::Number...) = _hypot(float.(promote(x, y, xs...)) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    mathsMathematical functions

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions