将参数赋予数学函数

时间:2018-05-15 07:17:06

标签: c microcontroller stm32f4discovery

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

extern double trunc _PARAMS((double));

并在我的主文件中调用它:

int i = (int)trunc(2.5);

它工作正常,没有问题。但如果我试图被动地传递双重,例如:

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。

1 个答案:

答案 0 :(得分:3)

如果您参考trunc()的手册页,请注意,

  

与-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