Redlib trunc() unexpectedly returns 0

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

Redlib trunc() unexpectedly returns 0

2,990件の閲覧回数
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 返信

2,873件の閲覧回数
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 件の賞賛
返信