[BUG] `math.gcd(Int.MIN, n)` never returns

オープン 初心者向け
#7,739 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

まだ誰も着手していません。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
72/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
領域
backend

調査の方向性

ハングは Mojo/stdlib/std/math/math.mojo の整数 gcd にあり、2634〜2652行目付近で、絶対値を abs で計算したあと、シフトと減算のループで縮小しています。issue の3行の再現コードを timeout 付きで実行し、gcd(Int.MIN, 6) が戻らないことを確認してください。2 を返し、他の MIN のケースが issue どおりになり、gcd(Int.MIN, 0) が 2^63 を返すかどうかという未解決の質問にメンテナーが回答すれば完了です。

索引モデルが issue の本文から書いたものです。

説明

bug_feature_triaged Team: Mojo Libraries

Bug description

Tiny one I hit while writing down std.math's contracts in Lean for MojoProof: gcd(Int.MIN, 6) never returns. It should be 2. gcd(6, Int.MIN) and the Span and variadic overloads hang too, and inputs right next to MIN are fine. (AI helped me find the loop below; I checked it.)

Actual behavior

It hangs.

Expected behavior

Steps to reproduce

from std.benchmark import black_box
from std.math import gcd


def main():
    print(gcd(black_box(Int.MIN + 2), 4), flush=True)  # 2
    print(gcd(black_box(Int.MIN), 0), flush=True)  # Int.MIN (2**63 does not fit)
    print(gcd(black_box(Int.MIN), 6))  # never returns, expected 2
$ timeout 20 mojo run repro.mojo; echo "exit=$?"
2
-9223372036854775808
exit=124

abs(Int.MIN) is still Int.MIN, so u starts out negative (math.mojo#L2634), and the shift and min(u, v) in the loop keep it negative, so diff never reaches 0 (L2645-L2652). Taking the magnitudes as UInt and running the same binary GCD fixes it: a copy written that way gives 2, 4 and 2 for gcd(Int.MIN, 6), gcd(Int.MIN, 4) and gcd(6, Int.MIN).

One question: gcd(Int.MIN, 0) and gcd(Int.MIN, Int.MIN) are 2^63, which doesn't fit in Int (today the first returns Int.MIN). Would you rather abort there, or keep the wrapped value and document it? I'm happy to open a PR with the UInt version :)

System information

Mojo 1.3.0.dev2026112105 (f23ed0af), MAX 26.8.0.dev2026112105, installed with pip from the nightly index. Amazon Linux 2023, x86_64, Intel Xeon Platinum 8275CL.

主要言語
Mojo
スター
30.2k
フォーク
3.2k
平均マージ
6日 20時間
マージ済み PR(30日)
25

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

modular/modular のほかの issue

modular/modular の issue をすべて見る

似ている issue

Backend & API Design の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。