String print error in default example

I am running the following example arduino-1.8.12\examples\08.Strings\StringConstructors StringConstructors.ino file. I am programming to an Artemis Apollo 3 mcu provided from sparkfun (lora things plus explorable). For some reason floats will not print right , and i cannot figure out why it is not working. Here is the line of code that is not working:

// using a float and the right decimal places:
  stringOne = String(5.698, 3);
  Serial.println(stringOne);

  // using a float and less decimal places to use rounding:
  stringOne = String(5.698, 2);
  Serial.println(stringOne);

Here is the output on the serial monitor:

%5.3f
%4.2f

I cannot figure out why it is printing the format of the value and not the value itself???

The other odd thing is that if i write Serial.println(5.698,9); in the code, that works fine and outputs 5.698000000

I suspect a bug in the toolchain for the processor; I however can't straight away find something (e.g. a bug report) on the web.

SparkFun has their own forum (https://forum.sparkfun.com/), it might be useful to ask there as well.

Note:
Your code gives the expected result when compiled for a classic Nano.

I tried on an ESP32 and it also gives the expected result.

Could you check what you get from:

// using a float and the right decimal places:
  stringOne = String(5.698, 3);
  Serial.println(stringOne.c_str());

  // using a float and less decimal places to use rounding:
  stringOne = String(5.698, 2);
  Serial.println(stringOne.c_str());

I would suspect the mbedOS and core conflicting in some way (may be some printf replacement stuff )

(never played with Artemis Apollo 3 mcu - so can't say)

Do you mean the "SparkFun Edge Development Board - Apollo3 Blue"? Or is it a different "SparkFun Apollo 3" board? I ask because I don't see any Arduino support for the "SparkFun Edge Development Board - Apollo3 Blue".

I had assumed one of these boards Artemis - SparkFun Electronics

It is this board:

I tried your code and got the same results:
%5.3f
%4.2f

OK so it sounds like String(5.698, 3) returns "%5.3f"
that's really weird....

did you raise a question on sparkFun's forum or GitHub?

Not yet, i did a bunch of researching and found these references:

Sounds like that is my exact issue, Do you know where i need to make the change ("platform.minimal-printf-enable-floating-point": true) ?

I found mbed_app.json filename in multiple directories. I am guessing this is the correct one, however adding the line did not seem to work?
\AppData\Local\Arduino15\packages\SparkFun\hardware\apollo3\2.0.5\cores\mbed-os\tools\test\config\app_override_libs

The first one seems to be your issue. As I don’t have that platform, unfortunately I don’t know what they refer to…

Well i tried a bunch of changes to compiler flags e.g. adding --specs=nano.specs -u _printf_float , turnning off optimization etc. However it does not seem that the libraries are being recompiled with these changes, as the output says this during compilation:

Using previously compiled file:
C"\ .......\AppData\Local\Temp\arduino_build_441533\core\mbed-bridge\core-api\api\Print.cpp.o

Is there a way to force recompilation of the libraries?

Here are my flags in .c-flags

-c -std=gnu11 -DMBED_MINIMAL_PRINTF -DMBED_TRAP_ERRORS_ENABLED=1 -O0 -fdata-sections -ffunction-sections -fmessage-length=0 -fno-exceptions -fomit-frame-pointer -funsigned-char -g -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -mthumb --specs=nano.specs -u _printf_float

Also here is what the .ld-flags are, do you know what flag -wl means? Maybe that is causing the issue?

-DMBED_BOOT_STACK_SIZE=1024 -DXIP_ENABLE=0 -Wl,--gc-sections -Wl,--wrap,_calloc_r -Wl,--wrap,_memalign_r -Wl,--wrap,atexit -Wl,--wrap,exit -Wl,--wrap,fprintf -Wl,--wrap,main -Wl,--wrap,printf -Wl,--wrap,snprintf -Wl,--wrap,sprintf -Wl,--wrap,vfprintf -Wl,--wrap,vprintf -Wl,--wrap,vsnprintf -Wl,--wrap,vsprintf -Wl,-n -mcpu=cortex-m4 -mfloat-abi=softfp -mfpu=fpv4-sp-d16 -mthumb --specs=nano.specs -u _printf_float

Close the IDE, open it again and load / compile your sketch.

Yea it is recompiling it still getting the same result. Not sure what is going wrong. ive added the -u _printf_float to the compiler ld flags, and have also tried deleting nano.specs all together and get the same result

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.