private static double remainderTwoPi(double angle){
  if (USE_JDK_MATH) {
    double y=STRICT_MATH ? StrictMath.sin(angle) : Math.sin(angle);
    double x=STRICT_MATH ? StrictMath.cos(angle) : Math.cos(angle);
    return STRICT_MATH ? StrictMath.atan2(y,x) : Math.atan2(y,x);
  }
  boolean negateResult;
  if (angle < 0.0) {
    negateResult=true;
    angle=-angle;
  }
 else {
    negateResult=false;
  }
  if (angle <= NORMALIZE_ANGLE_MAX_MEDIUM_DOUBLE) {
    double fn=(double)(int)(angle * INVTWOPI + 0.5);
    double result=(angle - fn * TWOPI_HI) - fn * TWOPI_LO;
    return negateResult ? -result : result;
  }
 else   if (angle < Double.POSITIVE_INFINITY) {
    long lx=Double.doubleToRawLongBits(angle);
    long exp=((lx >> 52) & 0x7FF) - 1046;
    double z=Double.longBitsToDouble(lx - (exp << 52));
    double x0=(double)((int)z);
    z=(z - x0) * TWO_POW_24;
    double x1=(double)((int)z);
    double x2=(z - x1) * TWO_POW_24;
    double result=subRemainderTwoPi(x0,x1,x2,(int)exp,(x2 == 0) ? 2 : 3);
    return negateResult ? -result : result;
  }
 else {
    return Double.NaN;
  }
}

private static double remainderTwoPi ( double angle ) { if ( USE_JDK_MATH ) { double y = STRICT_MATH ? StrictMath . sin ( angle ) : Math . sin ( angle ) ; double x = STRICT_MATH ? StrictMath . cos ( angle ) : Math . cos ( angle ) ; return STRICT_MATH ? StrictMath . atan2 ( y , x ) : Math . atan2 ( y , x ) ; } boolean negateResult ; if ( angle < 0.0 ) { negateResult = true ; angle = - angle ; } else { negateResult = false ; } if ( angle <= NORMALIZE_ANGLE_MAX_MEDIUM_DOUBLE ) { double fn = ( double ) ( int ) ( angle * INVTWOPI + 0.5 ) ; double result = ( angle - fn * TWOPI_HI ) - fn * TWOPI_LO ; return negateResult ? - result : result ; } else if ( angle < Double . POSITIVE_INFINITY ) { long lx = Double . doubleToRawLongBits ( angle ) ; long exp = ( ( lx > > 52 ) & 0x7FF ) - 1046 ; double z = Double . longBitsToDouble ( lx - ( exp << 52 ) ) ; double x0 = ( double ) ( ( int ) z ) ; z = ( z - x0 ) * TWO_POW_24 ; double x1 = ( double ) ( ( int ) z ) ; double x2 = ( z - x1 ) * TWO_POW_24 ; double result = subRemainderTwoPi ( x0 , x1 , x2 , ( int ) exp , ( x2 == 0 ) ? 2 : 3 ) ; return negateResult ? - result : result ; } else { return Double . NaN ; } }
remainder using an accurate definition of pi . derived from a fdlibm treatment called __ieee754_rem_pio2 . this method can return values slightly ( like one ulp or so ) outside [ - math . pi , math . pi ] range .
