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

Clarify that the unsigned type is promoted to, if the types differ only in signedness #48973

Merged
merged 5 commits into from
Mar 28, 2023
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions doc/src/manual/conversion-and-promotion.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ julia> promote(1 + 2im, 3//4)
```

Floating-point values are promoted to the largest of the floating-point argument types. Integer
values are promoted to the larger of either the native machine word size or the largest integer
argument type. Mixtures of integers and floating-point values are promoted to a floating-point
type big enough to hold all the values. Integers mixed with rationals are promoted to rationals.
Rationals mixed with floats are promoted to floats. Complex values mixed with real values are
promoted to the appropriate kind of complex value.
values are promoted to the largest of the integer argument types. If the types are the same size
but differ in signedness, the unsigned type is chosen. Mixtures of integers and floating-point
values are promoted to a floating-point type big enough to hold all the values. Integers mixed
Copy link
Member

@vtjnash vtjnash Mar 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The (pre-existing) floating point comment doesn't seem particularly accurate: it is just promoted to the floating point type, whatever that is. For example:

julia> promote(Float16(1.0f0), typemax(Int))
(Float16(1.0), Inf16)

julia> promote(Float32(1), typemax(Int32))[2] |> Int 
2147483648

julia> typemax(Int32)
2147483647

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct @vtjnash. I can make the change to "Mixtures of integers and floating-point values are promoted to the floating-point type." if this is the wanted behavior and the current implementation is not the problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point is that some people might find this quite unexpected:

julia> max(Float16(1), typemax(Int64))
Inf16

julia> versioninfo()
Julia Version 1.8.5
Commit 17cfb8e65ea (2023-01-08 06:45 UTC)
Platform Info:
  OS: macOS (arm64-apple-darwin21.5.0)
  CPU: 10 × Apple M1 Max
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-13.0.1 (ORCJIT, apple-m1)
  Threads: 8 on 8 virtual cores
Environment:
  JULIA_NUM_THREADS = 8

with rationals are promoted to rationals. Rationals mixed with floats are promoted to floats.
Complex values mixed with real values are promoted to the appropriate kind of complex value.

That is really all there is to using promotions. The rest is just a matter of clever application,
the most typical "clever" application being the definition of catch-all methods for numeric operations
Expand Down