Hi

I've found a bug in FastMath.sign(float). It compares the value of a float against 0.0 to determine if something is zero. Floating point rounding will usually throw off equations enough that comparing strictly to zero will fail much of the time. So I propose that the value should be comapred to Epsilon instead of 0 to determine zero-ness.

So here's my patch

Index: src/com/jme/math/FastMath.java

===================================================================

RCS file: /cvs/jme/src/com/jme/math/FastMath.java,v

retrieving revision 1.22

diff -r1.22 FastMath.java

325c325

< if (fValue > 0.0f) return 1.0f;

—

> if (fValue > FLT_EPSILON) return 1.0f;

327c327

< if (fValue < 0.0f) return -1.0f;

—

> if (fValue < -FLT_EPSILON) return -1.0f;

Index: target/jme.jar

===================================================================

Mark

Hmm, can this really be considered a bug? Maybe it is useful for some applications to even detect an epsilon? At least java.lang.Math.sign checks for != 0, too…

Does the JDK actually detect 0 proper or does it use nearly zero?

In the real world, calculations will always come out with perfect numbers, but since we're dealing with floating-point so we have to accept that zero and nearly zero might be the same number.

For now I'll just change this to use the Java's Math.signum(fValue). This looks like it may use native code and should be as accurate as we can get.

java.lang.Math does this:

`return d != 0.0D && !isNaN(d) ? copySign(1.0D, d) : d;`

Which returns 0 iff d is exactly 0.

renanse said:

For now I'll just change this to use the Java's Math.signum(fValue). This looks like it may use native code and should be as accurate as we can get.

Err, Math.signum is new in 1.5 :| - I've rolled this back

Damn 1.4… *rubs slapped hand*

Maybe someone could help me figure out why eclipse doesn't flag something like that? My compliance levels and such are all set to 1.4… I'm using Eclipse 3.1.0

i think compliance level is just for syntax and class file generation. It wont actually flag methods, as they exist in the JDK you are pointing to for that project (it has no way of knowing what is a 1.4 method vs a 1.5 method). At work, I usually just have both JDKs installed and set up in eclipse, and use the appropriate one. It's a bit of a pain, but seems more reliable in cases like this one.

–Ruab

Meh, how annoying. thanks.