Skip to content

Provide round_up and round_down for converting double to float #621

Open
@aprokop

Description

In some situations, it is helpful when we can control which way the rounding of a number is done. For example, right now, given data in double precision (like, points) we simply do float() conversion to the single precision. This is subject to round off errors and may lead to false negatives during the intersection tests. If we want to be conservative (i.e., only allow false positives but do not allow false negatives), the data should be rounded down for the minimum corner, and rounded up for the maximum corner, as to include the original data.

One of the ways to achieve that is using nextafterf functions. I'm thinking something like this:

#include <cmath>
float round_up(double x) {
  float f = x;
  if (double(f) < x)
    x = std::nextafterf(x, FLT_MAX);
}
float round_down(double x) {
  float f = x;
  if (double(f) > x)
    x = std::nextafterf(x, FLT_MIN);
}

(This may need to be changed slightly to account for nan, inf, etc).

Some papers do it informally (i.e., without using nextafterf) by rounding a positive number up by one ulp through multiplying it with 1 + 2^(-23) and down by multiplying with 1 - 2^(-23) (see pages 73-74 in Woop et al paper).

There are also some other situations where we may want to use nextafterf even for floats to account for losing precision using floating point operations. For example, this may help #560 (see this comment).

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions