please support printf for float

zerog2k
Posts: 6
Joined: Mon Mar 02, 2015 2:40 am

please support printf for float

Postby zerog2k » Wed Mar 04, 2015 2:37 pm

I noticed esp8266 sdk os_printf and os_sprintf do not support float!
This is much needed, as for IoT we will be dealing with lots of floats, like temp, rainfall, voltage, etc.

I had to implement quick float function, but it's probably not covering lots of edge cases...

Code: Select all

int power(int base, int exp){
    int result = 1;
    while(exp) { result *= base; exp--; }
    return result;
}

static char* ftoa(float num, uint8_t decimals) {
  // float to string; no float support in esp8266 sdk printf
  // warning: limited to 15 chars & non-reentrant
  // e.g., dont use more than once per os_printf call
  static char* buf[16];
  int whole = num;
  int decimal = (num - whole) * power(10, decimals);
  if (decimal < 0) {
    // get rid of sign on decimal portion
    decimal -= 2 * decimal;
  }
  char* pattern[10]; // setup printf pattern for decimal portion
  os_sprintf(pattern, "%%d.%%0%dd", decimals);
  os_sprintf(buf, pattern, whole, decimal);
  return (char *)buf;
}

User avatar
rudi
Posts: 197
Joined: Fri Oct 24, 2014 7:55 pm

Re: please support printf for float

Postby rudi » Wed Mar 04, 2015 11:59 pm

You are right, this i have missed too

viewtopic.php?f=7&t=44

will be fine in future for support this

;)

-------------------------------------
love it, change it or leave it.
-------------------------------------
問候飛出去的朋友遍全球魯迪

vowstar
Posts: 15
Joined: Fri Oct 24, 2014 7:23 pm

Re: please support printf for float

Postby vowstar » Thu Mar 05, 2015 1:48 pm

Please consider look at NodeMCU's source code.

NodeMCU firmware of esp8266 have solved this problem, it have standard vsprintf and sprintf. You could use vsprintf to get your printf.

https://github.com/nodemcu/nodemcu-firm ... /c_stdio.c

Who is online

Users browsing this forum: No registered users and 69 guests