JuliaLang/julia

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

开放

#57,855 创建于 2025年3月21日

 (8 条评论) (0 个反应) (0 位负责人)Julia (5,773 个派生)batch import
docshelp wanted

仓库指标

星标
 (48,709 个星标)
PR 合并指标
 (PR 指标待抓取)

描述

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.

贡献者指南