gpt4 book ai didi

c - 如何正确使用modf

转载 作者:行者123 更新时间:2023-11-30 20:56:50 25 4
gpt4 key购买 nike

我正在尝试使用 modf 函数,但它无法正常工作,它没有对变量进行签名

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

会给我0.00000 0.14000我做错了什么?

最佳答案

您正在将 &intp(float *)传递给需要 double * 的参数。这会导致未定义的行为。您需要使用modff:

fracp = modff(x,&intp);

或将 intp 改为 double:

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.

对于您的程序。

查看 modf and modff man page看看你哪里出错了。

关于c - 如何正确使用modf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19258809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com