2009-12-28 16 views

उत्तर

12

नहीं, वे समान नहीं होते हैं:

यहाँ स्रोत है। MSDN सापेक्ष के लिए और के लिए IEEERemainder इस्तेमाल किया विभिन्न सूत्रों से पता चलता है और मतभेदों का प्रदर्शन एक छोटा नमूना कार्यक्रम है:

IEEERemainder = dividend - (divisor * Math.Round(dividend/divisor)) 

Modulus = (Math.Abs(dividend) - (Math.Abs(divisor) * 
     (Math.Floor(Math.Abs(dividend)/Math.Abs(divisor))))) * 
     Math.Sign(dividend) 

कुछ उदाहरण हैं, जहां वे विभिन्न/समान उत्पादन (MSDN से लिया गया):

      IEEERemainder    Modulus 
    3/2 =       -1     1 
    4/2 =       0     0 
    10/3 =       1     1 
    11/3 =       -1     2 
    27/4 =       -1     3 
    28/5 =       -2     3 
    17.8/4 =      1.8     1.8 
    17.8/4.1 =     1.4     1.4 
    -16.3/4.1 = 0.0999999999999979     -4 
    17.8/-4.1 =     1.4     1.4 
    -17.8/-4.1 =     -1.4     -1.4 

इसी तरह के प्रश्न पर छःlettervariables द्वारा यह अच्छा answer भी देखें।

+1

मैं बस सोच रहा हूं - पृथ्वी पर मैं उन परिणामों को क्यों चाहूंगा? '11/3 = -1'? स्पष्ट रूप से यहां मॉड्यूलस 2 है। लेकिन किस परिदृश्य पर मैं '-1' चाहता हूं? –

+0

@RoyiNamir आपके पास यहां एक जवाब है: http://stackoverflow.com/a/27378075/200443 – Maxence

2

नहीं, वे समान नहीं हैं; documentation देखें।

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; 
     } 
    } 
संबंधित मुद्दे