C # equivalent of IEEE 754 () remainder?

In C #, is there an exact equivalent to the C99 / IEEE 754 function remainder()?

The C # language specification says it operator %(double x, double y)is "similar to that used for integer operands, but different from the IEEE 754 definition (which nis the integer closest to x / y)."

For an example of the difference, this C # program outputs two characters 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/GBITYq

whereas a similar program C produces 1 and -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;
}

http://ideone.com/HpuIVr

+4
source share
1 answer

This method implements the IEEE 754 shutdown algorithm:

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

+4

Source: https://habr.com/ru/post/1609171/


All Articles