question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Floating point errors in `.align_to()`

See original GitHub issue

Describe the bug The below code:

import sectionproperties.pre.library.bridge_sections as bridge_sections
import sectionproperties.pre.library.primitive_sections as standard_sections
from sectionproperties.pre.pre import Material
from sectionproperties.analysis.section import Section
from sectionproperties.pre.geometry import CompoundGeometry

Dslab, w, t_f = 180, 2100, 75

precast = Material(
    name="65 MPa",
    elastic_modulus=37.4e3,
    poissons_ratio=0.2,
    yield_strength=65,
    density=2.4e-6,
    color="grey",
)
insitu = Material(
    name="40 MPa",
    elastic_modulus=32.8e3,
    poissons_ratio=0.2,
    yield_strength=40,
    density=2.4e-6,
    color="lightgrey",
)

super_t = bridge_sections.super_t_girder_section(girder_type=5, w=w, material=precast)
slab = standard_sections.rectangular_section(d=Dslab, b=w, material=insitu).align_center(super_t).align_to(super_t, "top")

geom = CompoundGeometry([super_t, slab])
geom.plot_geometry()
geom

Produces this…

image

But it SHOULD produce this…(Note the red svg denoting “invalid” geometry and the hole indicated on the plot)

image

Additional context This seems to be a problem with shapely. I opened an issue with shapely after realizing that one of their examples in their docs does not reproduce as given (another example of the same problem): https://shapely.readthedocs.io/en/stable/manual.html#polygons

exter = [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
inter = [(1, 0), (0, 1), (0.5, 1.5), (1.5, 0.5), (1, 0)]
(Polygon(exter) - Polygon(inter)) # Creates valid MultiPolygon even though two points in common

Produces this… image

Something else that’s funny (odd, not ‘ha ha’)

If I take the geom object that was created in the bridge section code (at top) and break it out with .points as such:

geom = CompoundGeometry(MultiPolygon([Polygon(slab.points), Polygon(super_t.points)]))

# Instead of...

geom = CompoundGeometry([slab, super_t])

Then I also get this…

image

I don’t fully understand but I think it is shapely related… Will post more on this as I learn more.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
connorferstercommented, Jan 7, 2022

Gah! Simple solution: round the resulting coordinates, not the offset. <eyeroll>

In Geometry.compile_geometry(), which generates the .points, .facets, etc. the coordinates are already being rounded to self.tol which is set to 12 decimal places. This is why the geometry can be created without holes but when using the generated points in a new geometry with .from_points(), we get what we want.

Totally forgot about that tolerance thing in the creation of .points. Going to re-order Geometry.__init__() so that the shapely object is exactly the same as the generated points because, right now, there is a a slight difference between the geometry you see and the points that are generated from it.

1reaction
connorferstercommented, Jan 7, 2022

@robbievanleeuwen

So, it seems that just rounding the offset some decimal places does not work: it seems like it can create a small overlap between two geometries which causes triangle to spiral out of control.

I think that numpy.clip could hold a solution but it will be a bit tricky to implement. Started playing with it this evening. Will take some thinking.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Floating point error in Python - GeeksforGeeks
It's a problem caused when the internal representation of floating-point numbers, which uses a fixed number of binary digits to represent a ...
Read more >
15. Floating Point Arithmetic: Issues and Limitations — Python ...
Representation error refers to the fact that some (most, actually) decimal fractions cannot be represented exactly as binary (base 2) fractions.
Read more >
python - How to avoid floating point errors? - Stack Overflow
Controlling floating-point numeric errors is the field called "numerical analysis", and is a very large and complex topic.
Read more >
Fixing the Floating Point Arithmetic Precision Error in Python
The documentation for the built-in round() function says that it rounds to the nearest value, rounding ties away from zero. Since the decimal ......
Read more >
Floating-point error mitigation - Wikipedia
Floating -point error mitigation is the minimization of errors caused by the fact that real numbers cannot, in general, be accurately represented in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found