hyperbolic_polygon bug

116 views
Skip to first unread message

Hakan Granath

unread,
Aug 11, 2024, 7:45:18 AM8/11/24
to sage-...@googlegroups.com
Hi,

There is a bug in hyperbolic_polygon which happens quite rarely, but for example

    l = [-1, 1, -0.0571909584179366 + 0.666666666666667*I]
    hyperbolic_polygon(l, model='PD')

results in an incorrectly drawn hyperbolic triangle. It seems to be a subtle numerical issue; if the last element of l is replaced with -0.0571909584179366 + 0.667*I everything looks fine.

Digging a bit further, there seems to be an underlying issue here:

    PD = HyperbolicPlane().PD()
    z0 = CC(-0.0571909584179366 + 0.666666666666667*I)
    z1 = CC(-1)
    PD.get_geodesic(z0, z1).ideal_endpoints()

yields the erroneous result

    [Boundary point in PD -9.52420782539595e-17 + 1.00000000000000*I,
     Boundary point in PD -0.800000000000000 + 0.600000000000000*I]

(the second endpoint should be -1). Because of this I am suspicious of lines 1267 - 1269 in src/sage/geometry/hyperbolic_space/hyperbolic_geodesic.py. There we have the code

    # We could also have a vertical line with two interior points
    if x1 == x2:
        return [M.get_point(x1), M.get_point(infinity)]

Maybe the if statement should instead be something like

    if abs(x1 - x2) < EPSILON:

Best regards,

Håkan Granath

Georgi Guninski

unread,
Aug 11, 2024, 10:41:45 AM8/11/24
to sage-...@googlegroups.com
On Sun, Aug 11, 2024 at 2:45 PM Hakan Granath <hakan....@gmail.com> wrote:
>

> # We could also have a vertical line with two interior points
> if x1 == x2:
> return [M.get_point(x1), M.get_point(infinity)]
>
> Maybe the if statement should instead be something like
>
> if abs(x1 - x2) < EPSILON:
>

This was discussed here last year:
https://groups.google.com/g/sage-devel/c/KwfbvDoi-yg

Security weakness CWE-1077: Floating Point Comparison with Incorrect Operator

https://cwe.mitre.org/data/definitions/1077.html

Numeric calculation using floating point values can generate imprecise
results because of rounding errors. As a result, two different
calculations might generate numbers that are mathematically equal, but
have slightly different bit representations that do not translate to
the same mathematically-equal values. As a result, an equality test or
other comparison might produce unexpected results.

This issue can prevent the product from running reliably. If the
relevant code is reachable by an attacker, then this reliability
problem might introduce a vulnerability.

dmo...@deductivepress.ca

unread,
Aug 13, 2024, 11:50:52 AM8/13/24
to sage-devel
This was previously reported as issue #32362 on github, so any further discussion should take place there.  The solution suggested there is to have sagemath calculate endpoints in the Poincare disk model, instead of in the upper half plane model.

Vincent Delecroix

unread,
Aug 14, 2024, 7:27:08 AM8/14/24
to sage-...@googlegroups.com
Let me advertize that sage-flatsurf
(https://github.com/flatsurf/sage-flatsurf) has an implementation of
the hyperbolic plane with exact coordinates

The following code
```
from flatsurf import HyperbolicPlane
H = HyperbolicPlane(AA)
z0 = H.point(2*AA(2).sqrt()/3 - 1, 4/6, model='half_plane')
z1 = H.point(-1, 0, model='half_plane')
print(z0)
g = H.geodesic(z0, z1)
print(g.start())
print(g.end())
```
gives the correct
```
-0.05719095841793663? + 0.6666666666666667?*I
0.4142135623730951?
-1
```

Best
Vincent
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/baff7122-26e0-40a1-8bbf-0f2fc36e88d2n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages