C++:“float”的 printf() 格式规范是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7197589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: What is the printf() format spec for "float"?
提问by Michael Fitzpatrick
C++: What is the printf()
format spec for float
? (Visual C++)
C++:什么是printf()
格式规范float
?(Visual C++)
It used to be that I used %g
for float
and %lg
for double
.
它曾经是我用%g
的float
和%lg
为double
。
It looks like the spec changed and float
is undefined and double
is %g
.
看起来规范已更改并且float
未定义并且double
是%g
.
I have bits in memory that I am printing out so casting is not an option.
我的内存中有一些要打印出来,所以不能选择强制转换。
Is there a way that I can print out float
values using printf()
?
有没有办法可以float
使用打印出值printf()
?
Update:
更新:
This code was written for unit testing generic C++ libs used on an embedded system.
Here's what I had to do to get the float
to work.
The code is in a template function:
此代码是为在嵌入式系统上使用的通用 C++ 库进行单元测试而编写的。这是我必须做的事情float
才能正常工作。代码在模板函数中:
template <typename T,typename TTYP,typename Ttyp,int bits,bool IsSigned>
Error testMatrixT()
{ ...
Here is a code snip:
这是一个代码片段:
if (typeid(Ttyp) == typeid(float)) {
float64 c = *(float32*)&Tp(row,col);
float64 a1 = *(float32*)&Arg1(row,col);
float64 a2 = *(float32*)&Arg2(row,col);
float64 e = *(float32*)&Exp(row,col);
m_b = (c == e);
_snprintf(m_acDiag, sizeof(m_acDiag)-1
, "add(Arg1,Arg2): arg1=%g, arg2=%g, Expected=%g, Actual=%g, Result: %s"
, a1, a2, e, c, BOOL_PF(m_b));
} else {
...
Pretty ugly isn't it? Using floats as args give bad output. Maybe due to using _snprintf()
?
Years ago I would use %lg
and it would be OK. Not anymore.
很丑是不是?使用浮点数作为参数会产生错误的输出。可能是因为使用_snprintf()
?几年前我会使用%lg
它会没事的。不再。
回答by Stephen Canon
double and float use the same format specifiers with printf
(%a
, %e
, %f
, and %g
). This is because printf
is a variadic function. Any float arguments are implicitly promoted to double before the call; you can't actually pass a float to printf
.
双和浮子使用相同的格式说明与printf
(%a
,%e
,%f
,和%g
)。这是因为printf
是一个可变参数函数。在调用之前,任何浮点参数都被隐式提升为双精度;您实际上无法将浮点数传递给printf
.
回答by eq-
To the question in title: There is none / "%f"
对于标题中的问题:没有/“%f”
To the question in message body: Yes and no.
对于消息正文中的问题:是和否。
printf
is a variadic function. All float
arguments are automatically promoted to double
s. You print a float by passing it to printf, but no float actually reaches the printf function.
printf
是一个可变参数函数。所有float
参数都会自动提升为double
s。您通过将浮点数传递给 printf 来打印浮点数,但实际上没有浮点数到达 printf 函数。
回答by Keith Thompson
%g
has always been the correct format for either float or double (since float arguments are promoted to double for variadic functions like printf
).
%g
一直是 float 或 double 的正确格式(因为 float 参数被提升为 double 之类的可变参数函数printf
)。
%lg
was added as a synonym for %g
(in C99, I don't know when or whether C++ adopted it), probably for better symmetry with the *scanf
family.
%lg
被添加为%g
(在 C99 中,我不知道 C++ 何时或是否采用它)的同义词,可能是为了更好地与*scanf
家庭对称。
$Lg
is for long double.
$Lg
是长双。
You can replace g
with f
or e
in the above, depending on what output format you want.
您可以替换g
为f
或e
在上面,具体取决于您想要的输出格式。
回答by Kiley Naro
From: http://www.cplusplus.com/reference/clibrary/cstdio/printf/
来自:http: //www.cplusplus.com/reference/clibrary/cstdio/printf/
%f Decimal floating point 392.65
%f 十进制浮点数 392.65
回答by Praetorian
Others have pointed out the format for printing float using printf
, but, since you're using C++, my suggestion is to avoid this completely.
其他人已经指出了使用 打印浮点数的格式printf
,但是,由于您使用的是 C++,我的建议是完全避免这种情况。
Use C++ streams instead, you don't have to worry about format specifiers then. It is also type safe, while printf
isn't.
改用 C++ 流,然后您不必担心格式说明符。它也是类型安全的,而printf
不是。
#include <iostream>
int main()
{
float f = 1.234;
std::cout << f << std::endl;
}
If you have an array of bytes containing a float, memcpy
it into a float variable and use the above code.
如果您有一个包含浮点数的字节数组,memcpy
则将其转换为浮点数变量并使用上面的代码。
回答by SSteve
printf("float: %f", floatValue);
or if you want exponential notation:
或者如果你想要指数表示法:
printf("float: %e", floatValue);