C#相当于IEEE 754余数()?

时间:2015-09-27 12:03:32

标签: c# .net c99 ieee-754 modulus

在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);
    }
}

http://ideone.com/HpuIVr

而类似的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;
}

str.translate

1 个答案:

答案 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

http://ideone.com/b2Lvfx