MathUtils { public static double wrapValueToWithinLimits( final double value, final int limit) { double wrapped = (((((value >= 0) ? value : -value) + limit) % (limit * 2)) - limit) * ((value >= 0) ? 1.0 : -1.0); if (Double.compare(wrapped, limit) == 0) { wrapped = -wrapped; } assert (-limit <= wrapped) && (wrapped < limit); return wrapped; } private  MathUtils(); static double findMin(@Nonnull final double[] a); static double findMax(@Nonnull final double[] a); static boolean isAlmostZero(final double x); static boolean isAlmostEqual(final double x, final double y); static boolean isBetween(final int value, final int min, final int max); static boolean isBetween(final double value, final double min, final double max); static int limitTo(final int value, final int min, final int max); static double limitTo(final double value, final double min, final double max); static int random(final int min, final int max); static void setRandomSeed(final long seed); static double wrapValueToWithinLimits(
            final double value,
            final int limit); static final double EPSILON; }
@Test public void testWrapValueToWithinLimits() { LOG.info("testWrapValueToWithinLimits"); Assert.assertEquals(-1, MathUtils.wrapValueToWithinLimits(1, 1), 1.0e-8); Assert.assertEquals(0, MathUtils.wrapValueToWithinLimits(0, 180), 1.0e-8); Assert.assertEquals(0, MathUtils.wrapValueToWithinLimits(360.0, 180), 1.0e-8); Assert.assertEquals(1, MathUtils.wrapValueToWithinLimits(361.0, 180), 1.0e-8); Assert.assertEquals(-1, MathUtils.wrapValueToWithinLimits(359.0, 180), 1.0e-8); Assert.assertEquals(179, MathUtils.wrapValueToWithinLimits(179.0, 180), 1.0e-8); Assert.assertEquals(-179, MathUtils.wrapValueToWithinLimits(181.0, 180), 1.0e-8); }