Issue printing negative floating point numbers - debug_printf

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

Issue printing negative floating point numbers - debug_printf

3,008 Views
richardmay
Contributor I

Using KSDK 1.1.0 with no OS, I have been trying to use printf to output floating point numbers. I eventually settled on using PRINTF which is defined as debug_printf as printf did not output floating point numbers at all (even after following instructions in another thread (printf() with float values). debug_printf worked fine for positive numbers, but not for negative numbers. Junk characters were output instead.

Stepping through the revealed the problem to be in mkfloatnumstr which converts the float to a string.

The following code loops through the digits of the fractional part.

 

   for (i = 0; i < precision_width; i++)
   {
      fb = fa / (int32_t)radix;
      c = (int32_t)(fa - (uint64_t)fb * (uint32_t)radix);
      if (c < 0)
      {
         c = ~c + 1 + '0';
      }else
      {
         c = c + '0';
      }
      fa = fb;
      *nstrp++ = (char)c;
      ++nlen;
   }

The problem is the calculation of c - as it is, with negative numbers, the sign of fb is lost, and so instead of c being the value of the least significant digit of fa, it becomes equal to fa. I believe the solution is to leave fb signed as follows.

      c = (int32_t)(fa - (int64_t)fb * (int32_t)radix);

Labels (1)
0 Kudos
4 Replies

2,473 Views
christophertsch
Contributor III

Many thanks to Richard May who tracked this down. I changed that line of code in print_scan.c In KSDK 1.2.0 line 759 and my debug_printf started to work correctly.

To those of you at freescale, the bug still exists in KSDK 1.2.0. It seems trivial but before this fix I started writing code like this as a work-around and got highly annoyed every time:

if (fCurrentError < 0.0)

     debug_printf ("fCurrentError     -%f \n\r", fabs(fCurrentError));

else

     debug_printf("fCurrentError     %f \n\r", fCurrentError);

To re-create the problem make a project that uses the debug console component and use a %f in the debug_printf call to print a negative floating point number.

0 Kudos

2,473 Views
ivadorazinova
NXP Employee
NXP Employee

Hello

Thank you for your feedback. I passed it SW team.

It will be fixed in next KSDK release.

Best Regards,

Iva

0 Kudos

2,308 Views
docmo99
Contributor I

not fixed in MCUXpresso IDE v11.2.1 [Build 4149] [2020-10-07]

0 Kudos

2,300 Views
ErichStyger
Senior Contributor V

Hi @docmo99 

I would say it does not depend on the IDE, as the original report was about the SDK code. And the SDK has changed from the Kinetis SDK to the MCUXpresso SDK.

So: if you use say MCUXpresso IDE (11.3.1 would be the most recent one) then you probably use the MCUXpresso SDK 2.9.x. In essence: it is not about the IDE but the code/library you are using.

ErichS_0-1618378908906.png

 

Further the floating point support depends on the standard library used. If using newlib-nano you have to turn on floating point support in the linker settings. For newlib it is automatically turned on.

If you decide to map your printf calls to the standard library, then it will use whatever is in the standard library, and I can confirm that floating point numbers are working as they should with this, for example:

float.png

 
 

Can you be a bit more specific and show with an example (and setting used) what is not working on your end?

I hope this helps,

Erich

0 Kudos