Is Math.IEEERemainder(x,y) equivalent to x%y?

前端 未结 2 1083
谎友^
谎友^ 2020-12-10 03:05

Is there any differences between Math.IEEERemainder(x,y) and x%y ?

2条回答
  •  半阙折子戏
    2020-12-10 03:52

    No, they're not the same; see the documentation.

    Here's the source:

        public static double IEEERemainder(double x, double y) { 
            double regularMod = x % y;
            if (Double.IsNaN(regularMod)) { 
                return Double.NaN;
            }
            if (regularMod == 0) {
                if (Double.IsNegative(x)) { 
                    return Double.NegativeZero;
                } 
            } 
            double alternativeResult;
            alternativeResult = regularMod - (Math.Abs(y) * Math.Sign(x)); 
            if (Math.Abs(alternativeResult) == Math.Abs(regularMod)) {
                double divisionResult = x/y;
                double roundedResult = Math.Round(divisionResult);
                if (Math.Abs(roundedResult) > Math.Abs(divisionResult)) { 
                    return alternativeResult;
                } 
                else { 
                    return regularMod;
                } 
            }
            if (Math.Abs(alternativeResult) < Math.Abs(regularMod)) {
                return alternativeResult;
            } 
            else {
                return regularMod; 
            } 
        }
    

提交回复
热议问题