question: public static double remainder ( final double dividend , final double divisor ) { if ( Double . isInfinite ( divisor ) ) { if ( Double . isInfinite ( dividend ) ) { return Double . NaN ; } else { return dividend ; } } final double value = dividend % divisor ; if ( Math . abs ( value + value ) > Math . abs ( divisor ) ) { return value + ( value > 0.0 ? - Math . abs ( divisor ) : Math . abs ( divisor ) ) ; } else { return value ; } }
target:  Returns dividend - divisor * n, where n is the mathematical integer closest to dividend/divisor. If dividend/divisor is equally close to surrounding integers, we choose n to be the integer of smallest magnitude, which makes this treatment differ from Math.IEEEremainder(double,double), where n is chosen to be the even integer. Note that the choice of n is not done considering the double approximation of dividend/divisor, because it could cause result to be outside [-|divisor|/2,|divisor|/2] range. The practical effect is that if multiple results would be possible, we always choose the result that is the closest to (and has the same sign as) the dividend. Ex. : - for (-3.0,2.0), this method returns -1.0, whereas Math.IEEEremainder returns 1.0. - for (-5.0,2.0), both this method and Math.IEEEremainder return -1.0. If the remainder is zero, its sign is the same as the sign of the first argument. If either argument is NaN, or the first argument is infinite, or the second argument is positive zero or negative zero, then the result is NaN. If the first argument is finite and the second argument is infinite, then the result is the same as the first argument. NB: - Modulo operator (%) returns a value in ]-|divisor|,|divisor|[, which sign is the same as dividend. - As for modulo operator, the sign of the divisor has no effect on the result. - On some architecture, % operator has been observed to return NaN for some subnormal values of divisor, when dividend exponent is 1023, which impacts the correctness of this method.