turtle.setworldscoordinates() and floating point precision
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
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
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python/cpython
-
docs pending
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
stdlib type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
-
stdlib type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
build type-bug
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
stdlib topic-email type-feature
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
Similar issues
-
🐛 Bug 🔔 Pending processing
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
jumpserver/jumpserver#17584 ·
-
link-check link-check:sphinx-theme
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 90/100
modelscope/DiffSynth-Studio#1702 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
OpenHands/extensions#626 · 1 comment ·