在嵌入式 C 中使用带有 sprintf() 的浮点数 [英] Using floats with sprintf() in embedded C

查看:27
本文介绍了在嵌入式 C 中使用带有 sprintf() 的浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位,我想知道 float 变量是否可以在 sprintf() 函数中使用.

Guys, I want to know if float variables can be used in sprintf() function.

比如,如果我们写:

sprintf(str,"adc_read = %d 
",adc_read);

其中adc_read是一个整数变量,它将存储字符串

where adc_read is an integer variable, it will store the string

"adc_read = 1023 "

"adc_read = 1023 "

str 中(假设 adc_read = 1023)

如何使用浮点变量代替整数?

How can I use a float variable in place of integer?

推荐答案

由于您在嵌入式平台上,您很可能没有 printf() 的全部功能风格的函数.

Since you're on an embedded platform, it's quite possible that you don't have the full range of capabilities from the printf()-style functions.

假设你有浮动(对于嵌入式的东西仍然不一定是给定的),你可以用类似的东西来模拟它:

Assuming you have floats at all (still not necessarily a given for embedded stuff), you can emulate it with something like:

char str[100];
float adc_read = 678.0123;

char *tmpSign = (adc_read < 0) ? "-" : "";
float tmpVal = (adc_read < 0) ? -adc_read : adc_read;

int tmpInt1 = tmpVal;                  // Get the integer (678).
float tmpFrac = tmpVal - tmpInt1;      // Get fraction (0.0123).
int tmpInt2 = trunc(tmpFrac * 10000);  // Turn into integer (123).

// Print as parts, note that you need 0-padding for fractional bit.

sprintf (str, "adc_read = %s%d.%04d
", tmpSign, tmpInt1, tmpInt2);

您需要根据整数的大小限制小数点后的字符数.例如,对于 16 位有符号整数,您只能使用四位数字(9,999 是可以表示的最大 10 次幂).

You'll need to restrict how many characters come after the decimal based on the sizes of your integers. For example, with a 16-bit signed integer, you're limited to four digits (9,999 is the largest power-of-ten-minus-one that can be represented).

但是,有一些方法可以通过进一步处理小数部分,每次将其移动四位小数(并使用/减去整数部分),直到获得所需的精度.

However, there are ways to handle this by further processing the fractional part, shifting it by four decimal digits each time (and using/subtracting the integer part) until you have the precision you desire.

更新:

您提到的最后一点是您使用 avr-gcc 来响应其他答案之一.我发现以下网页似乎描述了在 printf() 语句中使用 %f 所需的操作 这里.

One final point you mentioned that you were using avr-gcc in a response to one of the other answers. I found the following web page that seems to describe what you need to do to use %f in your printf() statements here.

正如我最初所怀疑的那样,您需要做一些额外的工作才能获得浮点支持.这是因为嵌入式东西很少需要浮点(至少我做过的东西都没有).它涉及在您的 makefile 中设置额外的参数并与额外的库链接.

As I originally suspected, you need to do some extra legwork to get floating point support. This is because embedded stuff rarely needs floating point (at least none of the stuff I've ever done). It involves setting extra parameters in your makefile and linking with extra libraries.

但是,由于需要处理通用输出格式,这可能会大大增加您的代码大小.如果您可以将浮点输出限制为小数点后 4 位或更少,我建议将我的代码转换为函数并直接使用它 - 它可能占用更少的空间.

However, that's likely to increase your code size quite a bit due to the need to handle general output formats. If you can restrict your float outputs to 4 decimal places or less, I'd suggest turning my code into a function and just using that - it's likely to take up far less room.

万一该链接消失,您需要做的是确保您的 gcc 命令具有 "-Wl,-u,vfprintf -lprintf_flt -lm".这转化为:

In case that link ever disappears, what you have to do is ensure that your gcc command has "-Wl,-u,vfprintf -lprintf_flt -lm". This translates to:

  • 强制 vfprintf 最初未定义(以便链接器必须解析它).
  • 指定浮点 printf() 库进行搜索.
  • 指定要搜索的数学库.

这篇关于在嵌入式 C 中使用带有 sprintf() 的浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆