gpt4 book ai didi

c++ - Arduino snprintf 在使用 float 时返回一个包含问号的字符串

转载 作者:行者123 更新时间:2023-11-30 04:03:57 25 4
gpt4 key购买 nike

我在 arduino 中使用 snprintf 将 float 打印到字符 *。由于一些错误,我目前在读取实际值时遇到问题,但这不是这里的实际问题。我得到的字符串只是包含“?”。我想知道这是 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);

temp 现在只包含“?”。

最佳答案

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);

使用 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)

这些人已经在 avrfreaks 论坛上解决了这个问题:http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=119915

关于c++ - Arduino snprintf 在使用 float 时返回一个包含问号的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24017413/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com