C 如何正确使用modf
我试图使用modf函数,但它不能正常工作,它不能为变量的整数部分签名C 如何正确使用modf,c,C,我试图使用modf函数,但它不能正常工作,它不能为变量的整数部分签名 float intp; float fracp; float x = 3.14; fracp = modf(x,&intp); printf("%f %f\n", intp,fracp); 将给我0.000000.14000 我做错了什么?您正在将&intp(afloat*)传递给需要双精度*的参数。这会导致未定义的行为。您需要使用modff: fracp = modff(x,&intp); 或者将intp
float intp;
float fracp;
float x = 3.14;
fracp = modf(x,&intp);
printf("%f %f\n", intp,fracp);
将给我0.000000.14000
我做错了什么?您正在将
&intp
(afloat*
)传递给需要双精度*
的参数。这会导致未定义的行为。您需要使用modff
:
fracp = modff(x,&intp);
或者将intp
adouble
改为:
double intp;
你会没事的
您应该在编译器中启用更多警告。例如,即使没有特殊标志,clang也会提供:
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.
为了你的节目
查看以查看错误所在。-1表示显然没有利用编译器警告。-1表示没有阅读手册页。