tags:

views:

81

answers:

2

In C++ C: Output: "1610612736"

#include <math.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    printf("%d\n", fmodf(5.6f, 6.4f));
    getchar();
}

In C#: Output: "5.6"

using System;

static class Program
{
    public static void Main(string[] args)
    {
        Console.WriteLine(5.6f % 6.4f);
        Console.Read();
    }
}

Clearly not the same output. Suggestions?

+5  A: 

Try with printf("%f\n", fmodf(5.6f, 6.4f)) instead.

Alexandre C.
Good, sorry about the fact that it's C and tagged C++, I actually am coding in C# and only used the C version to compare.
Lazlo
@Lazlo: So you don't mind if I edit your original post then ;)
Alexandre C.
A: 

Correcting the decimal problem with fprintf()

#include <math.h>
#include <stdio.h>
#include <iostream>

int main(int argc, char** argv)
{
    std::cout << fmodf(5.6f, 6.4f) << std::endl;
    getchar();
}

Output 5.6

Martin York
Please use <cmath> and <cstdio>, I'm quite sure the C headers trigger errors (or deprecation warnings at the very least) on most compilers. And please use the overloaded function `std::fmod` instead of C's `fmodf`.
Alexandre C.