|
From: Damian M. <da...@er...> - 2001-12-31 14:51:39
|
While there is an inline routine, fpequal(a, b) which tests whether 2
numbers are close together to a floating point tolerance, there are
3 instances where this is not used but maybe should be.
field.cpp
(1.0 - cosom) > FPTOLERANCE) Anonymous
vrml97node.cpp
(dotval+1.0) > FPTOLERANCE) S.K.Bose
(1.0-dotval) > FPTOLERANCE)
which would appear to be saying
cosom is damn close to +1
+
dotval is damn close to -1
+
dotval is damn close to +1
respectively.
If this is the case, then the way this is written will cause some
cases, where what I think they are saying, will not actually happen.
Is this a potential bug or am I being paranoid?
Also, FPTOLERANCE is 1.0e-6 in the main library, but 1.0e-7 in the
OpenGL stuff. Should they be consistent?
And
const float FPTOLERANCE(1.0e-6);
inline fpzero(float a)
{
return fabs(b) <= FPTOLERANCE;
}
will probably kill the optimizer because it will trigger a call to fabs.
If you really want it inline, it will often be faster to do something like
inline fpzero(float a)
{
static float FPTOLERANCE = 1.0e-6;
return (-FPTOLERANCE <= a && a <= FPTOLERANCE);
}
or
inline fpzero(float a)
{
static float FPTOLERANCE = 1.0e-6;
return ((a < 0.0 ? -a : a) <= FPTOLERANCE);
}
and I hope I got that right.
If you want really optimal and machine portable code of a number within 3
binary digits of the machine precison, you can try
inline fpzero(float a)
{
return 1.0f + a * 0.125 == 1.0f;
}
but most optimizers kill portability of this by almost always returning
true so we instead do
inline fpadd1(float a)
{
return 1.0f + a * 0.125;
}
inline fpzero(float a)
{
return fpadd1(a) == 1.0f;
}
to at least fool g++/gcc. I've yet to try it on some other compilers.
I hope we all understood this while we are nursing our sore heads from
last night.
Many Thanks - Damian (McGuckin)
Pacific Engineering Systems International, 22/8 Campbell St, Artarmon NSW 2064
Ph:+61-2-99063377 .. Fx:+61-2-99063468 | unsolicited email not wanted here !
Views and opinions here are mine and not those of any past or present employer
|