1

I'm working with psoc creator, and I'm trying to print a float between -1 and 1 to an I2C OLED. To get this working I'm using the function sprintf. I added the following flag to my linker -u _printf_float because I'm using newlib-nano and it omits float handling code if not requested. (my problem might have to do something with this.. maybe?) For testing purposes I wrote the lines:

char stringbuffer[20];
sprintf(stringbuffer, "(%1.1f, %1.1f)", 1.1, 1.1);

While debugging I noticed that every non-zero float I try to print is printed as garbage, with a null character up front. Actually every non-zero float is replaced by the following 3 bytes. If I replace all the values with 0.0 then it is printed perfectly fine.

0x00 '\000'
0x2E '.'
0x80 '\200'

The integers that I'm trying to print in the following line are behaving as expected.

Anyone that ran into a similar problem?

Typhaon
  • 828
  • 8
  • 27
  • 1
    Not sure it's the cause of the problem, but the `%1.1f` format looks a bit weird. This is asking for a (total) width of `1` character and `1` digit after the decimal. Try `%3.1f`? – Adrian Mole Nov 05 '20 at 20:39
  • 1
    @AdrianMole The first `1` is the _minimum_ width, not total width. Still a tad weird. I doubt it is part of the problem. – chux - Reinstate Monica Nov 05 '20 at 20:43
  • 1
    Does code use a proper prototype for `sprintf()`?, Proper linking? – chux - Reinstate Monica Nov 05 '20 at 20:45
  • cannot reproduce https://godbolt.org/z/346q37 voting to close – 0___________ Nov 05 '20 at 22:15
  • @AdrianMole you're right, shouldn't have added the 1 in front of the period. But that is not the problem. – Typhaon Nov 06 '20 at 00:34
  • I found in this post: https://www.cypress.com/comment/305501 that adjusting the heap size might help. In this post https://stackoverflow.com/questions/28746062/snprintf-prints-garbage-floats-with-newlib-nano it is mentioned that it might be a problem with the linker (which goes way above my skill-level) Testing it tomorrow, and will report back if it's fixed. – Typhaon Nov 06 '20 at 00:37
  • @P__JsupportswomeninPoland That is because you're working with a different toolchain. I will be reporting back for future reference if I find a working solution. I found two possibilities already, see my other comments. – Typhaon Nov 06 '20 at 00:40
  • @Typhaon you should mention uCs. By default most implementations do not print floats. You need to: -u_printf_float – 0___________ Nov 06 '20 at 00:48
  • @P__JsupportswomeninPoland I already stated that I did that in my original post – Typhaon Nov 06 '20 at 00:53
  • @chux-ReinstateMonica Can you elaborate? what do you mean with proper prototype? sprintf works perfectly with integers, and floats equal to 0. – Typhaon Nov 06 '20 at 00:55
  • @Typhaon it does not matter hewre – 0___________ Nov 06 '20 at 00:56
  • @Typhaon Without a function prototype for `sprintf()`, 1) the compiler may not compiler code (which it apparently did for you), 2) make assumptions about types (which would be a problem with `char */int` incompatibility or 3) perhaps work OK. – chux - Reinstate Monica Nov 06 '20 at 03:00

0 Answers0