# [patch] Remove FastMath.sin2/cos2

Here is a patch to drop the sin2/cos2 methods from FastMath. The difference between them (using “safe area on x86”) and the normal Math.sin/cos aren’t explained and they lead to confusion (http://hub.jmonkeyengine.org/forum/topic/noob-texturing-a-procedural-mesh-questions/page/2/#post-198265)

It also isn’t clear what the “safe area” for x86 actually means since the documentation is vague, what are the implications on ARM devices? So the patch drops that method also.

[java]

# Above lines and this line are ignored by the patching process.

Index: FastMath.java
— FastMath.java Base (BASE)
+++ FastMath.java Locally Modified (Based On LOCAL)
@@ -483,58 +483,6 @@
}

/**
• * Fast Trig functions for x86. This forces the trig functiosn to stay

• * within the safe area on the x86 processor (-45 degrees to +45 degrees)

• * The results may be very slightly off from what the Math and StrictMath

• * trig functions give due to rounding in the angle reduction but it will be

• * very very close.

• *

• * note: code from wiki posting on java.net by jeffpk

• */

• public static float reduceSinAngle(float radians) {
•    radians %= TWO_PI; // put us in -2PI to +2PI space

•    if (Math.abs(radians) &gt; PI) { // put us in -PI to +PI space

•    }

•    if (Math.abs(radians) &gt; HALF_PI) {// put us in -PI/2 to +PI/2 space

•    }

• }
• /**
• * Returns sine of an angle.

• *

• * note: code from wiki posting on java.net by jeffpk

• *

• * @param fValue

• *            The angle to sine, in radians.

• * @return The sine of fValue.

• * @see java.lang.Math#sin(double)

• */

• public static float sin2(float fValue) {
•    fValue = reduceSinAngle(fValue); // limits angle to between -PI/2 and +PI/2

•    if (Math.abs(fValue) &lt;= Math.PI / 4) {

•        return (float) Math.sin(fValue);

•    }

•    return (float) Math.cos(Math.PI / 2 - fValue);

• }
• /**
• * Returns cos of an angle.

• *

• * @param fValue

• *            The angle to cosine, in radians.

• * @return The cosine of fValue.

• * @see java.lang.Math#cos(double)

• */

• public static float cos2(float fValue) {
•    return sin2(fValue + HALF_PI);

• }
• /**
• Returns cosine of an angle. Direct call to java.lang.Math
• @see Math#cos(double)
• @param v The angle to cosine.
[/java]
3 Likes

mhhh @Momoko_Fan any ideas why those methods were added in the first place?
Looks like a remaining of JME2, but it doesn’t mean it’s not relevant

If they are kept then the math or the internal comments should be fixed because one of them is wrong. For example, if a negative angle just over -PI is passed then it still subtracts from it to put it “in range”. The “in range” comment is either total BS or the math is wrong.

If there is a reason for having these then it should be added to the javadoc.

To the OP, one reason that other thread’s poster is getting inconsistent results is because he’s passing degrees instead of radians. So there is confusion just beyond this API.

FYI: nowhere in JME seems to use cos2, sin2, or reduceSinAngle. And if user code is using these then they most likely don’t know why they are.

Good catch, looks like they can go.

Ok I looked through jme2.1 code and those methods were already in FastMath, BUT they were called sin, and cos instead of sin2 and cos2.
My guess is that Kirill made proper sin and cos for JME3 (proper being calling math.cos ) and renamed them to keep them “in case of”.

So I vote this out too…

Mark them as obsolete, wait for a stable, delete them?

Although having said that it doesn’t seem likely that many/any people are using them…

Yeah…and there are easy alternatives

Done.

1 Like

Thx, might not help much people with good math skills but those methods did bring confusion to the herd :D.