Afficher en anglais

Partage via


Math.IEEERemainder(Double, Double) Méthode

Définition

Retourne le reste de la division d'un nombre spécifié par un autre.

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

Paramètres

x
Double

Dividende.

y
Double

Diviseur.

Retours

Nombre égal à x - (y Q), où Q est le quotient de x / y arrondi à l’entier le plus proche (si x / y se trouve à égale distance de deux entiers, l’entier pair est retourné).

Si x - (y Q) est égal à zéro, la valeur retournée est +0 si x est positif, ou -0 si x est négatif.

Si y = 0, NaN est retourné.

Exemples

L’exemple suivant compare le reste retourné par la IEEERemainder méthode avec le reste retourné par l’opérateur de reste.

C#
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

Remarques

Cette opération est conforme à l’opération restante définie à la section 5.1 de la norme ANSI/IEEE Std 754-1985 ; Norme IEEE pour binary Floating-Point arithmétique; Institute of Electrical and Electronics Engineers, Inc; 1985.

La IEEERemainder méthode n’est pas la même que l’opérateur de reste. Bien que les deux retournent le reste après division, les formules qu’ils utilisent sont différentes. La formule de la IEEERemainder méthode est la suivante :

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

En revanche, la formule de l’opérateur restant est la suivante :

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

S’applique à

Produit Versions
.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