cnlohr / ch32v003fun

Open source minimal stack for the ch32 line of WCH processors, including the ch32v003, a 10¢ 48 MHz RISC-V Microcontroller - as well as many other chips within the ch32v/x line.
MIT License
951 stars 147 forks source link

How to printf a float number? #264

Closed eznas closed 9 months ago

eznas commented 10 months ago

When I use " printf("%f\r\n", 1/3); " , I got only " f "? How can I print a float number?

emeb commented 10 months ago

ch32v003fun provides a stripped-down version of printf() which doesn't support floating point numbers. Use of floating point on the ch32v003 MCU is discouraged due to the heavy code size requirements. If you require floating point output you will need to provide that yourself.

eznas commented 10 months ago

Okay, understood and thanks!

TommyMurphyTM1234 commented 10 months ago

If, having heeded @emeb's sound advice, you still really need float support, then maybe consider using a self-contained "tiny" printf() implementation, ideally one that doesn't use heap which should generally be avoided in a resource constrained embedded environment. There are several such implementations available. For example (in no particular order):

maxgerhardt commented 10 months ago

WCH's properiatary toolchain does provide a printfloat library that can do that, and that works just fine: https://github.com/Community-PIO-CH32V/platform-ch32v/issues/41#issuecomment-1792337029

However, that is so big in code size anyways that you should never ever use it. Avoid floating pointing at all costs, or do cheaper fixed-point arithmetic instead.