2010-09-08 137 views
2

C++ C:輸出: 「1610612736」C#中fmodf的等價物?

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

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

在C#:輸出: 「5.6」

using System; 

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

顯然不相同的輸出。建議?

+1

%d爲小數。如果您使用C++使用流來避免此類問題。 – 2010-09-08 23:07:37

+0

btw這是C代碼,而不是C++。在C++中,使用'',''和'std :: fmod'。 – 2010-09-08 23:09:22

+0

@ Alexandre C:它被標記爲C++。 – 2010-09-08 23:11:54

回答

5

改爲使用printf("%f\n", fmodf(5.6f, 6.4f))

+0

很好,抱歉它是C和C++標記的事實,我實際上是使用C#編寫的,只使用C版本進行比較。 – Lazlo 2010-09-08 23:51:04

+0

@Lazlo:所以你不介意我是否編輯你原來的帖子;) – 2010-09-08 23:53:53

0

校正與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(); 
} 

輸出5.6

+2

請使用,我確定C頭文件會在大多數編譯器上觸發錯誤(至少是棄用警告)。請使用重載函數'std :: fmod'而不是C的'fmodf'。 – 2010-09-08 23:15:58