b=907986 avoid under and overflow in Normalize() r=padenot

--HG--
extra : transplant_source : %83%C5%A4%92%28%2Bf%7DHT%13%DE7L%26%B89x%E4%FF
This commit is contained in:
Karl Tomlinson 2013-09-04 21:20:58 +12:00
Родитель ce7c2ca91b
Коммит de80c0afe2
1 изменённых файлов: 13 добавлений и 0 удалений

Просмотреть файл

@ -8,6 +8,7 @@
#define ThreeDPoint_h_ #define ThreeDPoint_h_
#include <cmath> #include <cmath>
#include <algorithm>
namespace mozilla { namespace mozilla {
@ -34,6 +35,12 @@ struct ThreeDPoint {
void Normalize() void Normalize()
{ {
// Normalize with the maximum norm first to avoid overflow and underflow.
double invMax = 1 / MaxNorm();
x *= invMax;
y *= invMax;
z *= invMax;
double invDistance = 1 / Magnitude(); double invDistance = 1 / Magnitude();
x *= invDistance; x *= invDistance;
y *= invDistance; y *= invDistance;
@ -62,6 +69,12 @@ struct ThreeDPoint {
return x == 0 && y == 0 && z == 0; return x == 0 && y == 0 && z == 0;
} }
double x, y, z; double x, y, z;
private:
double MaxNorm() const
{
return std::max(fabs(x), std::max(fabs(y), fabs(z)));
}
}; };
ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs); ThreeDPoint operator-(const ThreeDPoint& lhs, const ThreeDPoint& rhs);