Gibt es Unterschiede zwischen Math.IEEERemainder (x, y) und x% y?Ist Math.IEEERemainder (x, y) äquivalent zu x% y?
13
A
Antwort
12
Nein, sie sind nicht gleichwertig. MSDN zeigt die verschiedenen Formeln für Modulo verwendet und für IEEERemainder und hat ein kurzes Beispielprogramm, die Unterschiede aufweist:
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)
Einige Beispiele, in denen sie unterschiedlichen/identischen Ausgang (aus MSDN) haben:
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
Siehe auch dieses gute answer von sixlettervariables zu einer ähnlichen Frage.
2
Nein, sie sind nicht gleich; siehe die documentation.
Hier ist die Quelle:
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;
}
}
Verwandte Themen
- 1. Ruby Seltsamkeit: x == y && [x, y] .uniq == [x, y]
- 2. x, y = getPos() vs. (x, y) = getPos()
- 3. Python, das effizienter ist; Wenn x! = y: x = y oder x = y
- 4. Prolog, Test (X, Y, Z): - Y ist X + Z
- 5. Wie mache ich eine unendliche Schleife mit "x <= y && x > = y && x! = Y"?
- 6. Warum/Wenn in Python `x == y` ruft `y .__ eq __ (x)`?
- 7. Darstellung der Werte (x, y) vs x._1, y._1
- 8. Pythonic tauschen x, y = y, x funktioniert nicht?
- 9. Was bedeutet x [:] = y?
- 10. Regex X/Y-Koordinate
- 11. Mysql-Abfragezähler x/y
- 12. Pyautogui.click (x, y) Fehler
- 13. Was ist die Logik für x, y = y, x, um die Werte in Python zu tauschen?
- 14. Ist es für GCC machbar, isnan (x) || zu optimieren? isnan (y) in isunordered (x, y)?
- 15. Warum sind x = x + y und x = y + x manchmal anders?
- 16. Rund y = x * x zum nächsten
- 17. jQuery Attributselektor: [x = y] oder [x = z]
- 18. Warum nicht javac akzeptieren `x = x +++++ y`?
- 19. VB.net - "Dim x wie neu Y()" vs "Dim x wie Y = new Y()"
- 20. Prolog: X ist der Großvater von Y
- 21. LayoutParams move x und y
- 22. #if definiert (x) || (y); Ist das gültig?
- 23. StatusBar-Eintrag x von y
- 24. x + y + = z = in Javascript
- 25. x = y = 1 in Scala?
- 26. Fortran Dim (x, y), zu welchem Zweck?
- 27. Warum ist die Pixelkoordinate in x, y Ebene x + y * Breite?
- 28. in C/C++ ist x [i] * Y [i ++] immer gleich x [i] * y [i]
- 29. Entry.place (X, Y) funktioniert nicht
- 30. Paare x, y Gleichung verifizieren
Ich frage mich nur - Warum in aller Welt möchte ich diese Ergebnisse? '11/3 = -1'? klar ist der Modul hier 2. Aber auf welchen Szenarien würde ich '-1' wollen? –
@RoyiNamir Sie haben eine Antwort hier: http://StackOverflow.com/a/27378075/200443 – Maxence