snprintf在使用float时返回包含问号的字符串

Mah*_*any 1 c++ floating-point printf arduino

我在arduino中使用snprintf来打印一个字符*.由于存在一些错误,我目前正在阅读实际值的问题,但这不是这里的实际问题.我得到的字符串只是包含"?".我想知道这是NaN还是INF?

例:

char temp[100];
float f; //This float is initialised on some value, but i'm currently not really sure what this value is, but for example 1.23
f = 1.23
snprintf(temp, 100, "%f", f);
Run Code Online (Sandbox Code Playgroud)

temp现在只包含"?".

Mah*_*any 5

Arduino的snprintf实现没有浮点支持.不得不改用dtostrf(http://www.nongnu.org/avr-libc/user-manual/group__avr__stdlib.html#ga060c998e77fb5fc0d3168b3ce8771d42).

所以不要这样做:

char temp[100];
float f;    
f = 1.23;
snprintf(temp, 100, "%f", f);
Run Code Online (Sandbox Code Playgroud)

使用Arduino我必须这样做:

char temp[100];
float f;    
f = 1.23;
dtostrf(f , 2, 2, temp); //first 2 is the width including the . (1.) and the 2nd 2 is the precision (.23)
Run Code Online (Sandbox Code Playgroud)

这些人在avrfreaks论坛上发现了这一点:http://www.avrfreaks.net/index.php? name = PNnppBB2&file = printview&t = 119915