Skip to content

CGAL freezes on arithmetic operations involving CGAL::sqrt  #4387

@copyme

Description

@copyme

Issue Details

Operating on numbers defined within Exact_predicates_exact_constructions_kernel_with_sqrt and using CGAL::sqrt can cause a possible infinite computation or inaccurate result.

Source Code

The above loop can't finish:

#include <iostream>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
int main() {
  typedef CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt Kernel;
  typedef Kernel::RT Number;

  Number coordMinX = Number(-15) * CGAL::sqrt(Number(3));
  Number coordMaxX = Number(-11) * CGAL::sqrt(Number(3));

 for (Number coordIndex = coordMinX - CGAL::sqrt(Number(3)); coordIndex <= coordMaxX; coordIndex += CGAL::sqrt(Number(3)) / Number(4))
  {
    std::cout << coordIndex << " " << coordMinX << " " << coordMaxX << std::endl;
  }
  return 0;
}

you can "fix" this issue by changing the code to this version:

#include <iostream>
#include <CGAL/Exact_predicates_exact_constructions_kernel_with_sqrt.h>
int main() {
  typedef CGAL::Exact_predicates_exact_constructions_kernel_with_sqrt Kernel;
  typedef Kernel::RT Number;

  Number coordMinX = Number(-15) * CGAL::sqrt(Number(3));
  Number coordMaxX = Number(-11) * CGAL::sqrt(Number(3));

  Number inc = CGAL::sqrt(Number(3)) / Number(4);

  for (Number coordIndex = coordMinX - CGAL::sqrt(Number(3)); coordIndex <= coordMaxX; coordIndex += inc)
  {
    std::cout << coordIndex << " " << coordMinX << " " << coordMaxX << std::endl;
  }
  return 0;
}

but then even if the loop terminates, the output is not correct precisely at the iteration where the previous version could not end. How do I know it is not correct? I use this code for defining triplets of lines that intersect at a single point, and these lines which depend on the problematic iteration do not intersect at a single point, making the whole idea of computing 2D arrangement with CGAL useless.

Do I do something wrong here, or this is a bug?

Environment

  • Operating system (Windows/Mac/Linux, 32/64 bits): Linux 64 bits
  • Compiler: GCC
  • Release or debug mode: Release
  • Specific flags used (if any):
  • CGAL version: 4.12, 4.14, and 5.0
  • Boost version: 1.66
  • Other libraries versions if used (Eigen, TBB, etc.):

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions