Standard way to normalize an angle to +/- π radians in Java

不打扰是莪最后的温柔 提交于 2019-11-30 18:16:20

Apache commons has one:

http://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/org/apache/commons/math3/util/MathUtils.html#normalizeAngle(double, double)

normalize an angle between -π and +π

a = MathUtils.normalizeAngle(a, 0.0);

And looking at the source code, you could reproduce it with this (they use their own FastMath.floor but in case you want to do it without an external library):

theta - TWO_PI * Math.floor((theta + Math.PI) / TWO_PI)

Source is here: https://github.com/apache/commons-math/blob/53ec46ba272e23c0c96ada42f26f4e70e96f3115/src/main/java/org/apache/commons/math4/util/MathUtils.java#L107


Note for readers from the future: this method has just (June 2017) been removed from the latest commons-math 4.x codebase. If you're using a version after this, you'll want to use commons-numbers instead (once it's released) - currently:

a = PlaneAngleRadians.normalizeBetweenMinusPiAndPi(a);

or

a = PlaneAngleRadians.normalize(a, 0.0);

There is only one 100% foolproof way:

public static double normalizeAngle(double angle) {
    return Math.atan2(Math.sin(angle), Math.cos(angle));
}   

Everything else is people trying to be too smart and failing.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!