I noticed that with FastMath enabled, the fast integration of the sqrt returns a very little number (10^-20 or something like that) when the input is 0. I know the invsqrt is an approximation, but wouldn't it be better to set a control for the zero case? One problem i get, for example, is the calulation of friction between two frictionless circle shapes. friction = MathUtils.sqrt(0*0) returns a very little number, which eventually causes two colliding circles to start rotating (when they shouldn't, i believe). For now i simply changed this:
into this:
moving the function after checking if the argument is zero.
Am i completely off track here, or do i make any sense? 🙂
Cheers!
[EDIT]
Ok, actually the circle problem doesn't come from the close to zero approx, sorry for that. But anyway the consideration on FastMath sqrt remains.
Code:
public static final float sqrt(float x) {
if (Settings.FAST_MATH) {
x = invSqrt(x);
if (x != 0.0f) {
return 1.0f / x;
}
else {
return 0;
}
}
else {
return (float) Math.sqrt(x);
}
}
if (Settings.FAST_MATH) {
x = invSqrt(x);
if (x != 0.0f) {
return 1.0f / x;
}
else {
return 0;
}
}
else {
return (float) Math.sqrt(x);
}
}
into this:
Code:
public static final float sqrt(float x) {
if (Settings.FAST_MATH) {
if (x != 0.0f) {
x = invSqrt(x);
return 1.0f / x;
}
else {
return 0;
}
}
else {
return (float) Math.sqrt(x);
}
}
if (Settings.FAST_MATH) {
if (x != 0.0f) {
x = invSqrt(x);
return 1.0f / x;
}
else {
return 0;
}
}
else {
return (float) Math.sqrt(x);
}
}
moving the function after checking if the argument is zero.
Am i completely off track here, or do i make any sense? 🙂
Cheers!
[EDIT]
Ok, actually the circle problem doesn't come from the close to zero approx, sorry for that. But anyway the consideration on FastMath sqrt remains.