Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent int-float promotion in hypot #53505

Closed
danielwe opened this issue Feb 28, 2024 · 1 comment · Fixed by #53541
Closed

Inconsistent int-float promotion in hypot #53505

danielwe opened this issue Feb 28, 2024 · 1 comment · Fixed by #53541
Labels
maths Mathematical functions

Comments

@danielwe
Copy link
Contributor

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...)) 
@mikmoore
Copy link
Contributor

Another consequence is permutation-asymmetry in the return type

julia> hypot(1, 1f0)
1.4142135623730951 # Float64

julia> hypot(1f0, 1)
1.4142135f0 # Float32

@ViralBShah ViralBShah added the maths Mathematical functions label Feb 28, 2024
danielwe added a commit to danielwe/julia that referenced this issue Mar 1, 2024
ViralBShah pushed a commit that referenced this issue Mar 1, 2024
tecosaur pushed a commit to tecosaur/julia that referenced this issue Mar 4, 2024
mkitti pushed a commit to mkitti/julia that referenced this issue Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
maths Mathematical functions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants