Skip to content

Segmentation fault occurs when using rectangular lattices with large pitch values. #3852

@MatteoZammataro

Description

@MatteoZammataro

Segmentation faults often occur when rectangular lattices with large pitch values are used. This is most likely due to a numerical cancellation issue in the boundary-crossing detection logic check in RectLattice::distance.

The original implementation computes the distance d to the nearest lattice boundary, then checks which boundaries were crossed using a floating-point proximity test:

if (u.x != 0.0 && std::abs(x + u.x * d - x0) < FP_PRECISION)
    lattice_trans[0] = copysign(1, u.x);

When pitch_[0] (and therefore x0) is large (eg. 100 cm), the expression x + u.x * d may suffer from cancellation: two large, nearly-equal floating-point numbers are subtracted, destroying significant digits. The tolerance threshold FP_PRECISION is a constant number, so it does not scale with the pitch. The result is that lattice_trans is sometimes computed as {0, 0, 0}, which causes the segmentation fault.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions