Fcvt not present in stdlib.h

I've got an Adafruit Feather M0 Adalogger. As explained here, floating point to string conversion does not work, but the page refers to a discussion where someone has re-implemented the dtostrf function.

Unfortunately, when I compile it I get the following error:

C:\Users\titus\Documents\src\arduino_trials\04_keyence_distance_sensor\04_keyence_distance_sensor.ino: In function 'char* dtostrf(double, int, unsigned int, char*)':
04_keyence_distance_sensor:73:7: error: 'fcvt' was not declared in this scope
   73 |   s = fcvt(val, prec, &decpt, &sign);
      |       ^~~~

It seems that the fcvt function is not implemented in AVR-LibC. I would expect it to be in stdlib.h, but it's not there.

Can anybody give me some pointers on where to find it, or on how to convert floating point values to strings?

Welcome to the forum.

Adafruit Feather M0 Adalogger: https://www.adafruit.com/product/2796
That is a ATSAMD21G, just like the Arduino MKR Zero. It is not part of the AVR family.

Can you give a small sketch that shows the problem ?

use ftoa() or sprintf() instead of fcvt

Thanks for your help @Koepel.

In the page I linked is says that:

Like the AVR Arduinos, the M0 library does not have full support for converting floating point numbers to ASCII strings. Functions like sprintf will not convert floating point. Fortunately, the standard AVR-LIBC library includes the dtostrf function which can handle the conversion for you.

Unfortunately, the M0 run-time library does not have dtostrf. You may see some references to using #include <avr/dtostrf.h> to get dtostrf in your code. And while it will compile, it does not work.

Instead, check out this thread to find a working dtostrf function you can include in your code:

working dtostrf - Arduino Zero - Arduino Forum

So I tried the example with the "working dtostrf" but it seems that fcvt is missing from stdlib.h (which I assume is the AVR stdlib.h)

A small sketch is based on the example I downloaded with all irrelevant lines removed.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *dtostrf(double val, int width, unsigned int prec, char *sout)
{
  int decpt, sign, reqd, pad;
  const char *s, *e;
  s = fcvt(val, prec, &decpt, &sign);
  return sout;
}

void setup() { }

void loop() { }

it just doesn't compile. I gives the error I wrote previously.

Thanks @b707 but ftoa gives the same error during compilation: it can't be found in stdlib.h

Unfortunately sprintf doesn't work either. It does compile but doesn't properly convert the values. That's why I ended up trying dtostrf.

write your own ftoa() yourself, it will few lines of code... you can find some examples in google and on stackoverflow

My earlier reply has disappeared so I try again.

Thanks for your help @Koepel

In the link I posted says that:

Like the AVR Arduinos, the M0 library does not have full support for converting floating point numbers to ASCII strings. Functions like sprintf will not convert floating point. Fortunately, the standard AVR-LIBC library includes the dtostrf function which can handle the conversion for you.

Unfortunately, the M0 run-time library does not have dtostrf. You may see some references to using #include <avr/dtostrf.h> to get dtostrf in your code. And while it will compile, it does not work.

Instead, check out this thread to find a working dtostrf function you can include in your code:

working dtostrf - Arduino Zero - Arduino Forum

I therefore tried the "working dtostf" example but it didn't compile. It can't find the fcvt function in stdlib.h, which I assume it the stdlib.h from AVR.

A small sketch based on the "dtostff" example is:

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

char *dtostrf(double val, int width, unsigned int prec, char *sout)
{
  int decpt, sign, reqd, pad;
  const char *s, *e;
  s = fcvt(val, prec, &decpt, &sign);
  return sout;
}

void setup() { }

void loop() { }

It just doesn't compile and gives the error I wrote in my original post.

Did you install the Adafruit boards for your Feather M0 Adalogger ? Then you have the build environment of Adafruit.

I thought it was like this (but I could be wrong):

  1. The AVR boards have no floating point for printf() because that uses too much memory for the first board with a ATmega8. They have dtostrf().
  2. The Arduino MKR M0+ boards should have normal floating point for printf(). They don't ?
  3. The fcvt() function does not exist in the gcc libraries, it is not used with Arduino.

The link to this forum with a "solution" for dtostrf() is not correct.
I think that Adafruit made a mistake with a link to bad code.

:point_right: Can someone with a MKR Zero board check of sprintf() gives a question mark '?' for floating point ?