Redlib trunc() unexpectedly returns 0

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Redlib trunc() unexpectedly returns 0

724 Views
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

Tags (2)
0 Kudos
1 Reply

607 Views
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 Kudos