Commit 2e71e44
Fix gcdx and lcm with mixed signed/unsigned arguments (#59628)
Add `gcdx(a::Signed, b::Unsigned)` and `gcdx(a::Unsigned, b::Signed)`
methods to fix #58025:
```julia
julia> gcdx(UInt16(100), Int8(-101)) # pr
(0x0001, 0xffff, 0xffff)
julia> gcdx(UInt16(100), Int8(-101)) # master, incorrect result
(0x0005, 0xf855, 0x0003)
```
Also add the equivalent methods for `lcm` to fix the systematic
`InexactError` when one argument is a negative `Signed` and the other is
any `Unsigned`:
```julia
julia> lcm(UInt16(100), Int8(-101)) # pr
0x2774
julia> lcm(UInt16(100), Int8(-101)) # master, error
ERROR: InexactError: trunc(UInt16, -101)
Stacktrace:
[1] throw_inexacterror(func::Symbol, to::Type, val::Int8)
@ Core ./boot.jl:866
[2] check_sign_bit
@ ./boot.jl:872 [inlined]
[3] toUInt16
@ ./boot.jl:958 [inlined]
[4] UInt16
@ ./boot.jl:1011 [inlined]
[5] convert
@ ./number.jl:7 [inlined]
[6] _promote
@ ./promotion.jl:379 [inlined]
[7] promote
@ ./promotion.jl:404 [inlined]
[8] lcm(a::UInt16, b::Int8)
@ Base ./intfuncs.jl:152
[9] top-level scope
@ REPL[62]:1
```
Inspired by
#59487 (comment).
The difference is that the solution proposed in this PR keeps the
current correct result type for inputs such as `(::Int16, ::UInt8)`.
(cherry picked from commit 4f1e471)1 parent 5d70208 commit 2e71e44
2 files changed
+41
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
148 | 148 | | |
149 | 149 | | |
150 | 150 | | |
| 151 | + | |
| 152 | + | |
151 | 153 | | |
152 | 154 | | |
153 | 155 | | |
| |||
252 | 254 | | |
253 | 255 | | |
254 | 256 | | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
255 | 267 | | |
256 | 268 | | |
257 | 269 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
216 | 216 | | |
217 | 217 | | |
218 | 218 | | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
219 | 248 | | |
220 | 249 | | |
221 | 250 | | |
| |||
0 commit comments