turtle.setworldscoordinates() and floating point precision

Đang mở
#101,113 10 bình luận 0 reaction 0 người được giao Xem trên GitHub

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

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
35/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
python
Lĩnh vực
desktop

Hướng nghiên cứu

Bắt đầu bằng cách chạy trình tái hiện xoắn ốc turtle được cung cấp và kiểm tra Lib/turtle.py quanh dòng 1105. Theo dõi cách setworldcoordinates cập nhật xscale và yscale, sau đó so sánh lỗi số thực dấu phẩy động của Tk với ZeroDivisionError của RawTurtle. Được xem là hoàn thành khi hành vi tại giới hạn độ chính xác được xử lý mà không xuất hiện màn hình trống hoặc sự cố, với độ bao phủ cho các điều kiện biên đã được báo cáo.

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

Mô tả

topic-tkinter

I made a turtle draw a spiral while rescaling the screen according to the last position of the turtle.

Simple code to reproduce (increasing the speed variable will make it crash sooner):

"""Crash a Turtle with negative infinity canvas scale"""
import turtle

speed = 3

angle = 0
radius = 1
boundaries = {'llx': -1.0, 'lly': -1.0, 'urx': 1.0, 'ury': 1.0}
pen = turtle.Turtle()
pen.speed(10)
pen.getscreen().tracer(1e3)
while True:
    print(f"radius {radius}, angle {angle}")
    print(f"screen xscale {pen.getscreen().xscale}")
    print(f"screen yscale {pen.getscreen().yscale}")
    pen.circle(radius, angle)
    radius *= speed
    angle += 1
    pos = pen.pos()
    boundaries['llx'] = min(boundaries['llx'], pos[0])
    boundaries['lly'] = min(boundaries['lly'], pos[1])
    boundaries['urx'] = max(boundaries['urx'], pos[0])
    boundaries['ury'] = max(boundaries['ury'], pos[1])
    pen.getscreen().setworldcoordinates(**boundaries)

This will raise _tkinter.TclError: expected floating-point number but got "floating" when x or y scale gets near 1e-306.

But in the code I'm using with a RawTurtle and a TurtleScreen I get ZeroDivisionError because somewhere after 6.7e-307 (in my machine) both TurtleScreen's x and y scales becomes 0.0, RawTurtle's position becomes (-inf, -inf) and then this line of code is reached: https://github.com/python/cpython/blob/3ef9f6b508a8524f385cdc9fdd4b4afca0eac59b/Lib/turtle.py#L1105

I had to set this in my code to prevent screen becoming blank (because of scale becoming 0.0), so I could stop drawing before it crashes:

precision: int = 1e-306
while True:
    logger.debug(f"""
boundaries = {boundaries}
screen.xscale = {screen.xscale}
screen.yscale = {screen.yscale}
""")
    try:
        1 / screen.xscale
        1 / screen.yscale
    except ZeroDivisionError:
        logger.warning(f"reached infinity")
        break
    if precision != min(precision, screen.xscale, screen.yscale):
        logger.warning(f"reached floating point precision limit")
        break
    screen.setworldcoordinates(**boundaries)
## Make screen still without going blank
root.mainloop()
Ngôn ngữ chính
Python
Star
77.2k
Fork
36k
Merge trung bình
1 ngày 9 giờ
Pull request đã merge (30 ngày)
558

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

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 python/cpython

Tất cả issue của python/cpython

Issue tương tự

Thêm issue về Python

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.