Problem printing float

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 I have been through the knowledgebase and applied suggestions 

   

Project -> Build Options -> linker -> Command Line -> -u_printf_float

   

Then tried Linker -> General -> Use newlib nano ... False

   

Finally tried, asm (".global _scanf_float"); 

   

None of these seemed to help. I can sprintf decimal values and strings ok but not floating point.

   

I am using Psoc Creator 3.2 SP1, ARM GCC 4.8.4 compiler, debug build

   

Here is my code (initially I was trying to print a float var but set an exlicit value to test)

   

#include "stdlib.h"

   

char tbuf[50];

   

sprintf(tbuf,"%f",(float) 1.35);

   

When I run this my buffer has values like

   

0x00, 0x2E, 0x80, 0x00, 0x20, 0x11, 0x00 ...

   

instead of  0x31, 0x2E, 0x33, 0x35

   

Any help much appreciated

   

Regards

   

David

27 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Switch on in project build options linker the Use newlib-nano and the use newlib-nano float formatting options.

   

 

   

Happy coding

   

Bob

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Bob,

   

Both were set to true by default. I then tried both set to false but no difference, 

   

Any other ideas I could try?

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 By the way Bob,

   

Is there a simulator for PSCO creator? I am remoting into my work machine (which is very slow) to test this at the moment and would be great if I could test just the code on my machine here at home without the hardware. 

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Last question's answer first:

   

I have got a PSoC5 Development kit connected to a (fast) PC in my lab. Using TightVNC I am able to check a Project just during the ads-pause in TV from the laptop in my living-room. Quite nice, give it a thought. May be a bit more complicated when not on the same net or via the internet.

   

When you provide me with a workspace bundle I do not have to type a project myself and I am able to check all your settings.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

http://airsupplylab.com/index.php/learn/14-psoc5/9-psoc-floating-point-problem-in-psoc-creator-3 nano library  enable

   

http://www.cypress.com/?id=4&rID=87354     nano library enable

   

 

   

I use Teamviewer to service remote machines, has been a very good ap, free

   

for non - commercial use.

   

 

   

Regards, Dana.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Increasing the heap size in system tab of .cydwr-file to 0x0200 will help.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Bob,

   

I have attached my test project where I cannot sprinf a float. The build settings for Nano and nano float stuff are both on, I have also tred both off and either one on.

   

Thanks

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Using these settings worked for me on a PSOC 4, I cannot find

   

my 5LP board -

   

 

   

   

 

   

0 Likes
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 How Bizarre,

   

Attached is what I get when I set the project up with same settings with my 5LP.

0 Likes
lock attach
Attachments are accessible only for community members.
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 And, if I change from debug build to release, the buffer contents change (though they are still wrong).

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

Oh how nice it would be to be able to include a float value in a string, I would have throught it pretty stright forward but alas, all is not as it seems afterall.

   

Yesterday I was working with a new smart display module, I spent most of the day trying to send a string to the display (again, not something I would have thought should have been too challenging in this space age world in which we live).

   

However, after several HOURS!! and several posts to the support forum, I receive an email advising me the display can fail to program properly if me code ise is 2047 + any multiple of 2048 Bytes. Well guess what, mine was. I changed the font size slightly (which changed the code size0 and now it works again).

   

I wonder if this time I need to wiggle my left foot, rub my head anti-clockwise and shout "beam me up Scotty" to get the float conversion happening?

   

For this particular application, the display wants the value sent as an integer that ia 10 x the value to be displayed to give 1 dp readout (ie, 123 = 12.3), could I multiple my float by 10, add 0.5 and convert to int and then use sprintf on my "safe" integer value?  Woudl I simply cast the float and get the real part 

   

sprintf(buf,"%l",(int)(myFloat*10+0.5));

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You overlooked my last post with the heap size!

   


   

Set the heap size to 0x0200

   

 

   

Bob

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 I set heap size to 0x200, no change.

   

Where is the documentation for printf showing all these settngs that require changing in order to print a float?

   

It doesn't seem obvious or logical to me. So far, we are setting the 2 nano settings to true, adding the custom parameter and changing the heap size. Maybe one day it may even be possible to have a button to enable sprintf Float and it makes adjustment to ALL of the parameters needed to implement it.

   

It appears a lot to do to get a standard C function running. Hopefully this ONLY applies to float? Or does it also apply to other formats in the string?

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 OK, it works now, I had to implement all the items at the same time to get it working: I must have had one of these not set when I changed the heap size earlier.

   

Where did you find info that states all you need to do to print a float is......

   

Use newlib-nano = true

   

Use nano-newlib Float Formatting = true

   

Add custom parameter to linker command line -u_printf_float

   

Change heap size to 0x200

0 Likes
Anonymous
Not applicable

Many thanks for this. I ran into the same problem, and this solved it for me.

   

On PSoC Creator 4, the System settings are under the 'Design-wide Resources' and, as Bob Marlowe mentioned, setting the heap size to 0x0200 does the trick.

0 Likes

Welcome in the forum, nayan.

   

Fine that such an old thread could help you!

   

 

   

Bob

0 Likes
Anonymous
Not applicable

I just came across with the same problem and solved it increasing the heap size to 0x200. How or why is that the solution?

Thanks

0 Likes

When I had that problem I started to debug my project and found out that a call from the newlib-nano library to malloc() returned a NULL pointer. This usually indicates an out-of-heap error. The caller took some fixup. So I increased the heap zize and was successful.

I have to admit that I am not quite content with the handling of the heap in Creator. I am used from other programming languages that all unused memory gets allocated to the heap. This ensures that without any modifications all memory could be used, even when the project changes.

Bob

0 Likes
DaHu_285096
Level 5
Level 5
10 likes received 250 replies posted 100 replies posted

 Finally,

   

Thanks for taking the time to help me sort this out Bob, much appreciated.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

You are using Creator 3.2 which is not yet released for full use (early access). The default settings will not allow for using sprintf() and there was a hint here in the forum to generally increase the heap.

   

The other change "use newlib-nano float formatting" (default: FALSE) does exactly what you had to do manually when setting a linker parameter. When you highlight that item the full description shows the command-line added term.

   

I admit that it can turn out to be rather difficult when searching for help to sort out the needed settings for the 3.1 and 3.2 versions which differ in some places.

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

The reason you had crap in buffer, aside from the heap and outher issues,

   

is when you declared it you did not initialize it to something like "      " blanks.

   

 

   

Regards, Dana.

0 Likes
Anonymous
Not applicable

(((FYI, fixed)))

   

Bob and others:

   

For me,  

   

char msgtemp[512]; // Plenty long, local var of a function
sprintf(msgtemp,"test_pht: Results: T=%g degC, P=%g Pa, H=%g %%RH\r\n", dTemp, dPressure, dHumidity);
 

   

prints up to the "T=" then the reset of the line is absent, as of processing failed or inserted a null terminator for the %g.  I tried %f and in fact the program hung rather than continuing.  Buffer is long enough.  Amount printed is only 21 chars anyway

   

Windows 10.  PSoC Creator 3.3.  ARM GCC 4.9-2015-q1-update.

   

 

   

I begin by trying "-u_printf_float -u_scanf_float", but then saw Bob's 26 May 2015 post, searched for, and found liner setting "Use newlib-nano Float Formatting", set it True and removed my manual switches.  Yes, I see the "-u _printf_float" in the gray window, but not the "-u_scanf_float".  Whatever.  But the behavior doesn't improve.

   

Looking for where to set heap size, but can't find it.  QUESTION ONE: Where is it? Found it via Bob's earlier post: "Increasing the heap size in system tab of .cydwr-file to 0x0200 will help."

   

QUESTION TWO: Do I have to do all the other items Orbitcoms suggested? WORKING NOW.  I had to do two things, set "Use newlib-nano Float Formatting" to True and set heap sizes to 0x200

   

QUESTION THREE: Otherwise, just how do I make my sprintf work?  Be less impatient.

   

Thanks,

   

Helmut

   

P.S. Strike through font displays in edit window but not in post.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

"QUESTION TWO: Do I have to do all the other items Orbitcoms suggested? WORKING NOW.  I had to do two things, set "Use newlib-nano Float Formatting" to True and set heap sizes to 0x200"

   

Usually changing only the above two items will allow for printing floats. Other changes became obsolete with Creator 4.0

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Ok this is driving me crazy. 

   

Using PSoC Creator 4 (4.0.0.432)

   

Set heap size to 0x200

   

enabled/True: Use newlib-nano Float Formatting

   

 

   

Using the following simple code:

   

#include <stdio.h>

   

#include <stdlib.h>

   

 

   

int main(void) {

   

   CyGlobalIntEnable;

   

  char tbuf[33];

   

   UART_Start();

   

   UART_UartPutString("TEST BEGIN\r\n");

   

   sprintf(tbuf, "%f", (float)18.32);

   

   UART_UartPutString(tbuf);

   

 

   

   for(;;) {

   

   }

   

}

   

 

   

It appears to hang on the sprintf function.  I've also tried using printf, it will print up until the float.  Am I missing something?!

   

 

   

EDIT: Looks like it is working IF I increase the heap size to 0x400.  (OUCH!  starting to wonder if it's worth using UART output floating point)

0 Likes
        Please check all settings again http://www.cypress.com/forum/psoc-4-architecture/sprintf-function-blocking-program   
0 Likes
Anonymous
Not applicable

Increasing heap size to 0x400 worked for me too. Thanks

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

I usually survive with following trick

    sprintf(tbuf, "%d.%02d", (int)(f_value + 0.005), ((int)(f_value * 100.0 + 0.5))%100);

===========================

#include "project.h"

#include <stdio.h>  

#include <stdlib.h>

int main(void) {

   char tbuf[33];

    float f_value = 18.32 ;

   CyGlobalIntEnable;

   UART_Start();

  

   UART_UartPutString("TEST BEGIN\r\n");  

//   sprintf(tbuf, "%f", (float)18.32);

    sprintf(tbuf, "%d.%02d", (int)(f_value + 0.005), ((int)(f_value * 100.0 + 0.5))%100);

   UART_UartPutString(tbuf);

  

    for(;;) {

   }  

}

===========================

moto

0 Likes