diff --git a/src/libcore/triangle.cpp b/src/libcore/triangle.cpp index 3a9a4e8c..5906e182 100644 --- a/src/libcore/triangle.cpp +++ b/src/libcore/triangle.cpp @@ -126,20 +126,24 @@ AABB Triangle::getClippedAABB(const Point *positions, const AABB &aabb) const { for (int j=0; j<3; ++j) { /* Now this is really paranoid! */ double pos_d = vertices1[i][j]; - Float pos_f = (Float) pos_d; + float pos_f = (float) pos_d; Float pos_roundedDown, pos_roundedUp; -#if defined(SINGLE_PRECISION) - pos_roundedUp = nextafterf(pos_f, - std::numeric_limits::infinity()); - pos_roundedDown = nextafterf(pos_f, - -std::numeric_limits::infinity()); -#else - pos_roundedUp = nextafter(pos_f, - std::numeric_limits::infinity()); - pos_roundedDown = nextafter(pos_f, - -std::numeric_limits::infinity()); -#endif + if (pos_f < pos_d) { + /* Float value is too small */ + pos_roundedDown = pos_f; + pos_roundedUp = nextafterf(pos_f, + std::numeric_limits::infinity()); + } else if (pos_f > pos_d) { + /* Float value is too large */ + pos_roundedUp = pos_f; + pos_roundedDown = nextafterf(pos_f, + -std::numeric_limits::infinity()); + } else { + /* Double value is exactly representable */ + pos_roundedDown = pos_roundedUp = pos_f; + } + result.min[j] = std::min(result.min[j], pos_roundedDown); result.max[j] = std::max(result.max[j], pos_roundedUp); }