Skip to content

Commit

Permalink
Protect from floating point rounding errors (#302)
Browse files Browse the repository at this point in the history
  • Loading branch information
brechtm committed Nov 4, 2021
1 parent ad38d25 commit 275adb3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ Changed:

Fixed:

* Crash due to floating point rounding error (#302 by Sam Hartman)
* Setting 'number_format' to *none* caused a crash; now it causes the caption
label to be omitted.
* Handle citations and corresponding citation references that are not defined
Expand Down
7 changes: 5 additions & 2 deletions src/rinoh/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def advance(self, height, ignore_overflow=False):
"""Advance the cursor by `height`. If this would cause the cursor to
point beyond the bottom of the container, an :class:`EndOfContainer`
exception is raised."""
if height <= self.remaining_height:
if height <= self.remaining_height + FLOATING_POINT_FUZZ:
self._self_cursor.grow(height)
elif ignore_overflow:
self._self_cursor.grow(float(self.remaining_height))
Expand All @@ -271,7 +271,7 @@ def advance2(self, height, ignore_overflow=False):
bottom of the container.
"""
if height <= self.remaining_height:
if height <= self.remaining_height + FLOATING_POINT_FUZZ:
self._self_cursor.grow(height)
elif ignore_overflow:
self._self_cursor.grow(float(self.remaining_height))
Expand Down Expand Up @@ -305,6 +305,9 @@ def log_styleds(index):
log_styleds(i)


FLOATING_POINT_FUZZ = 1e-10


class _FlowablesContainer(FlowableTarget, FlowablesContainerBase):
def __init__(self, name, type, parent, *args, **kwargs):
super().__init__(parent.document_part, name, type, parent,
Expand Down

0 comments on commit 275adb3

Please sign in to comment.