Page 1 of 1

imprecise normalizing in vector3ds

Posted: Wed Apr 30, 2008 2:02 pm
by neoIxen
Hi,
this isn't a bug at all but i think this should be done because it's pretty imprecise.

Code: Select all

vector3d<T>& normalize()
{
	T l = X*X + Y*Y + Z*Z;
	if (l == 0)
		return *this;
	l = (T) reciprocal_squareroot ( (f32)l );
	X *= l;
	Y *= l;
	Z *= l;
	return *this;
}
should be:

Code: Select all

vector3d<T>& normalize()
{
	f32 l = X*X + Y*Y + Z*Z;
	if (l == 0)
		return *this;
	l = reciprocal_squareroot ( l );
	X = (T) ((f32)X * l);
	Y = (T) ((f32)Y * l);
	Z = (T) ((f32)Z * l);
	return *this;
}
this would be much more precise for integer types...
just a suggestion ^^

neoIxen

Posted: Wed Apr 30, 2008 4:53 pm
by rogerborg
Can you identify any users who rely on normalised integer vectors?

Also, were you abused by a f64 at one time, that you hate them so much, and want them to suffer?

Posted: Wed Apr 30, 2008 8:51 pm
by neoIxen
i don't know what i needed it for but I once came across with this while using integer vectors and had to rewrite it ^^

but ur right it's not necessary to change at all...
and yes i fear f64.. it's bad!
just kidding... that would also do the job ^^

neoIxen