Redlib trunc() unexpectedly returns 0

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Redlib trunc() unexpectedly returns 0

3,131 次查看
danielholala
Senior Contributor II

Hi there,

 

I observe an unexpected result when using trunc(). I wonder if anybody could help me using Redlib with floats and libm.

Compiling, linking using Redlib and debugging

#include <math.h>

int main(void) {

  float adc_read = 123.123;
  int adc_int = trunc(adc_read);
‍‍
  //...enter infinite loop or other code
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

will give me the a value of 0 for adc_int (see attached screenshot).Capture trunc debug.png

If I link using Newlib instead, trunc() returns the correct result of 123. 

As a workaround, I'm now using a cast to int instead:

int adc_int = (int)adc_read;

But still I'm puzzled why Redlib's trunc() returns 0? What am I missing here? According to LPCXsupport, "Redlib is just a C library" and therefore I assume it should implement at least functionality according to C90 standard.

Thanks a lot!

Daniel

标记 (2)
0 项奖励
回复
1 回复

3,014 次查看
lpcxpresso_supp
NXP Employee
NXP Employee

So trunc() is a C99 function and one that Redlib unfortunately doesn't currently implement. So I'm intrigued as to how you even got your project to build against Redlib - unless something else in your setup is providing it.

 

You should also note that where available, trunc() returns a double, not an int:

http://www.cplusplus.com/reference/cmath/trunc/?kw=trunc 

 

Regards,

MCUXpresso IDE Support

0 项奖励
回复