JuliaLang/julia

Document promotion behavior of `div` and `divrem` for `(::Unsigned, ::Signed)`

Open

#57.855 aberto em 21 de mar. de 2025

Ver no GitHub
 (8 comments) (0 reactions) (0 assignees)Julia (5.773 forks)batch import
docshelp wanted

Métricas do repositório

Stars
 (48.709 stars)
Métricas de merge de PR
 (Mesclagem média 23d 11h) (145 fundiu PRs em 30d)

Description

Currently we have

julia> div(UInt(1), -1)
0xffffffffffffffff

This happens because we are returning the promote_type, but violates both common sense, and the documentation which states "Generally equivalent to a mathematical operation x/y without a fractional part".

As such, I propose that we instead make the return type of div(x,y) be ifelse(isa(y, Signed), signed(promote_type(x,y)), promote_type(x,y)). This would obviously be slightly breaking, but I think it qualifies as a bugfix.

We also would want to make similar changes for divrem, fld, cld since they all have the same problem.

Guia do colaborador