将参数传递给数学函数

giving parameters into math function

如果我在 C 中调用,数学库中的数学函数 `"trunc" 定义为:

extern double trunc _PARAMS((double));

在我的主文件中调用它:

int i = (int)trunc(2.5);

它工作正常,没有问题。但是如果我尝试被动地传递 double,比如:

double d = 2.5;
int i = (int)trunc(d);

这行不通?!?在我的微处理器 STM32F4 IDE 中,它进入调试器模式:

 Infinite_Loop:
 b  Infinite_Loop

它卡在那里。我还更改了 double 并尝试 float, int, unit8_t,... 没有人不工作。 其他数学函数也可以正常工作,因为我这样称呼它们:

i =  sin(1);
i =  cos(1);

但是,如果这样调用,它也会崩溃:

int a = 1;
i = sin(a);
i = cos(a);

我是运行微处理器STM32F4 Discovery上的这段代码,IDE是Eclipse Ac6。

如果您参考 trunc() 的手册页,您应该注意,它说

Link with -lm

因此,请确保您链接的是 -lm

其次,当概要说 double trunc(double x); 时,用其他数据类型尝试它是没有意义的。

如果您想尝试其他数据类型,那么您应该寻找这些变体:

float truncf(float x);
long double truncl(long double x);

测试代码:

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

int main(void)
{
    // using trunc function which return
    // Truncated value of the input 
    double d = 2.7;
    printf(" Truncated value is %d \n", (int)trunc(d)); 

    return 0;
}

这生成了一个输出:

 Truncated value is 2

并且是用 gcc 5.3.0 版本编译的

当我在没有 -lm 选项的情况下编译相同的代码 gcc 版本 7.2.0 时, 我收到以下警告:

undefined reference to `trunc'
error: ld returned 1 exit status