C# 相当于 IEEE 754 余数 ()?
C# equivalent of IEEE 754 remainder()?
在 C# 中,是否存在与 C99/IEEE 754 的 remainder()
函数完全等效的函数?
operator %(double x, double y)
是"analogous to that used for integer operands, but differs from the IEEE 754 definition (in which n
is the integer closest to x / y
)"的C# language specification says。
作为差异的示例,此 C# 程序输出两个 1:
using System;
public class Test
{
public static void Main()
{
Console.WriteLine(5.0 % 2.0);
Console.WriteLine(3.0 % 2.0);
}
}
而类似的 C 程序输出 1 和 -1:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%f\n", remainder(5.0, 2.0));
printf("%f\n", remainder(3.0, 2.0));
return EXIT_SUCCESS;
}
此方法实现了 IEEE 754 余数算法:
Math.IEEERemainder(double, double)
public class Test
{
public static void Main()
{
Console.WriteLine(Math.IEEERemainder(5.0, 2.0));
Console.WriteLine(Math.IEEERemainder(3.0, 2.0));
}
}
// Output: 1, -1
在 C# 中,是否存在与 C99/IEEE 754 的 remainder()
函数完全等效的函数?
operator %(double x, double y)
是"analogous to that used for integer operands, but differs from the IEEE 754 definition (in which n
is the integer closest to x / y
)"的C# language specification says。
作为差异的示例,此 C# 程序输出两个 1:
using System;
public class Test
{
public static void Main()
{
Console.WriteLine(5.0 % 2.0);
Console.WriteLine(3.0 % 2.0);
}
}
而类似的 C 程序输出 1 和 -1:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {
printf("%f\n", remainder(5.0, 2.0));
printf("%f\n", remainder(3.0, 2.0));
return EXIT_SUCCESS;
}
此方法实现了 IEEE 754 余数算法:
Math.IEEERemainder(double, double)
public class Test
{
public static void Main()
{
Console.WriteLine(Math.IEEERemainder(5.0, 2.0));
Console.WriteLine(Math.IEEERemainder(3.0, 2.0));
}
}
// Output: 1, -1