2013-10-08 19 views
-3

我試圖使用MODF功能,但它不能正常工作,它不不可分割的一部分登錄到可變如何正確使用MODF

float intp; 
float fracp; 
float x = 3.14; 
fracp = modf(x,&intp); 
printf("%f %f\n", intp,fracp); 

會給我0.00000 0.14000 我到底做錯了什麼?

+3

-1顯然沒有考慮到編譯器警告的優點。 –

+3

-1沒有閱讀手冊頁。 –

回答

2

你傳遞&intp(一float *)到需要double *的參數。這會導致未定義的行爲。您需要使用modff

fracp = modff(x,&intp); 

或使intp一個double代替:

double intp; 

而且你會沒事的。

您應該在編譯器中打開更多警告。例如,甚至沒有任何特殊標誌,鐺給出:

example.c:9:20: warning: incompatible pointer types passing 'float *' to 
     parameter of type 'double *' [-Wincompatible-pointer-types] 
    fracp = modf(x,&intp); 
        ^~~~~ 
/usr/include/math.h:400:36: note: passing argument to parameter here 
extern double modf(double, double *); 
           ^
1 warning generated. 

爲您的程序。

查看modf and modff man page查看錯誤發生的位置。