Leggere in inglese

Condividi tramite


Math.IEEERemainder(Double, Double) Metodo

Definizione

Restituisce il resto della divisione tra il numero specificato e un altro numero specificato.

public static double IEEERemainder(double x, double y);

Parametri

x
Double

Dividendo.

y
Double

Divisore.

Restituisce

Un numero uguale a x - (y Q), dove Q è il quoziente di x / y arrotondato all'intero più vicino; se x / y è contenuto tra due interi, verrà restituito l'intero pari.

Se x - (y Q) è zero, verrà restituito il valore +0 se x è positivo oppure -0 se x è negativo.

Se y = 0, verrà restituito NaN.

Esempio

Nell'esempio seguente viene confrontato il resto restituito dal IEEERemainder metodo con il resto restituito dall'operatore resto.

using System;

public class Example
{
   public static void Main()
   {
      Console.WriteLine($"{"IEEERemainder",35} {"Remainder operator",20}");
      ShowRemainders(3, 2);
      ShowRemainders(4, 2);
      ShowRemainders(10, 3);
      ShowRemainders(11, 3);
      ShowRemainders(27, 4);
      ShowRemainders(28, 5);
      ShowRemainders(17.8, 4);
      ShowRemainders(17.8, 4.1);
      ShowRemainders(-16.3, 4.1);
      ShowRemainders(17.8, -4.1);
      ShowRemainders(-17.8, -4.1);
   }

   private static void ShowRemainders(double number1, double number2)
   {
      var formula = $"{number1} / {number2} = ";
      var ieeeRemainder = Math.IEEERemainder(number1, number2);
      var remainder = number1 % number2;
      Console.WriteLine($"{formula,-16} {ieeeRemainder,18} {remainder,20}");
   }
}
// The example displays the following output:
//
//
//                       IEEERemainder   Remainder operator
// 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

Commenti

Questa operazione è conforme all'operazione di resto definita nella sezione 5.1 di ANSI/IEEE Std 754-1985; IEEE Standard per binary Floating-Point aritmetica; Institute of Electrical and Electronics Engineers, Inc; 1985.

Il IEEERemainder metodo non corrisponde all'operatore di resto. Anche se entrambi restituiscono il resto dopo la divisione, le formule usate sono diverse. La formula per il IEEERemainder metodo è:

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

Al contrario, la formula per l'operatore di resto è:

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

Si applica a

Prodotto Versioni
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0