在C#中,是否完全等同于C99 / IEEE 754的remainder()
功能?
operator %(double x, double y)
C# language specification says类似于用于整数操作数的http://ideone.com/GBITYq,但与IEEE 754定义不同(其中n
是最接近{x / y
的整数1}})"
有关差异的示例,此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;
}
答案 0 :(得分:4)
此方法实现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