turtle.setworldscoordinates() and floating point precision

Open
#101,113 10 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
python
Domain
desktop

Research direction

Start by running the supplied turtle spiral reproducer and inspect Lib/turtle.py around line 1105. Trace how setworldcoordinates updates xscale and yscale, then compare the Tk floating-point error with the RawTurtle ZeroDivisionError. Done means the precision-limit behavior is handled without a blank screen or crash, with coverage for the reported boundary conditions.

Written by the indexing model from the issue text.

Description

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()
Dominant language
Python
Stars
77.2k
Forks
36k
Avg merge
1d 9h
Merged PRs (30d)
558

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python/cpython

All issues in python/cpython

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.