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

Đang mở Phù hợp với người mới
#7,739 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
backend

Hướng nghiên cứu

Lỗi treo nằm ở hàm gcd với số nguyên trong Mojo/stdlib/std/math/math.mojo, khoảng dòng 2634-2652, nơi độ lớn được tính bằng abs rồi rút gọn trong vòng lặp dịch bit và trừ. Chạy đoạn tái hiện ba dòng trong issue với timeout để xác nhận gcd(Int.MIN, 6) không bao giờ trả về. Hoàn thành khi hàm trả về 2, các trường hợp MIN khác khớp với issue, và một maintainer trả lời câu hỏi còn mở về việc gcd(Int.MIN, 0) có trả về 2^63 hay không.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Mojo
Star
30.2k
Fork
3.2k
Merge trung bình
6 ngày 20 giờ
Pull request đã merge (30 ngày)
25

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của modular/modular

Tất cả issue của modular/modular

Issue tương tự

Thêm issue về Backend & API Design

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.