JuliaPhysics / JuliaPhysics/Unitful.jl
Arithmetic with affine units results in rational when integer would suffice
- Dominant language
- Julia
- Stars
- 675
- Forks
- 124
- Avg merge
- 3h 38m
- Merged PRs (30d)
- 1
Description
When adding a temperature difference in `K` to a temperature in `°C`, one often wants to have the result in `°C` as well. `ContextUnits` can be used to achieve this.
```julia
julia> x = 5 * ContextUnits(°C, °C)
5 °C
julia> x + 3K
8//1 °C
```
However, the result has numerical type `Rational{Int}` even though `Int` would be just fine. The reason for this is that the `+` method in this case converts both quantities to `K` to add them before converting the result back to `°C`.
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by tracing the + method used for ContextUnits and the conversion back from K to °C. Reproduce the shown Julia example and inspect how numeric types are preserved during affine-unit arithmetic. Done means adding 3K to 5 °C produces 8 °C with an integer numeric type rather than Rational{Int}.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- julia
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100