C++ 中的 LONG float 和 double 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16863826/
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
What's the difference between LONG float and double in C++?
提问by Nealon
So I know that there's a large difference in the precision of floats and doubles. I get that. Promise.
所以我知道浮点数和双精度数的精度有很大的不同。我明白了。承诺。
But, in C++, when calling scanf and printf, the notation used to specify a double is "%lf", and that stands for long float, right? So while a float is less precise than a double, a LONG float (presumedly called long float because it can be "longer" by having more terms) is the same accuracy and therefore essentially the same thing?
但是,在 C++ 中,当调用 scanf 和 printf 时,用于指定 double 的符号是“%lf”,它代表 long float,对吗?因此,虽然浮点数不如双精度数精确,但 LONG 浮点数(推测称为长浮点数,因为它可以通过更多项来“更长”)具有相同的准确性,因此本质上是相同的?
Just to clarify, here's what I mean:
为了澄清,这就是我的意思:
double number = 3.14159;
printf("The number is %lf", number);
So the root of my question: Is long float another name for double?
所以我的问题的根源是:long float 是 double 的另一个名字吗?
回答by taocp
There is no such a type as long float
within my knowledge.
long float
在我的知识范围内,没有这样的类型。
This postgives you information about why people use lf
to print double
with printf
if this is the cause of your confusion.
这篇文章给你,为什么人们使用信息lf
打印double
使用printf
,如果这是你的困惑的原因。
By courtesy of @Jerry Coffin:
由@Jerry Coffin 提供:
"%f" is the (or at least "a") correct format for a double. There is no format for a float, because if you attempt to pass a float to printf, it'll be promoted to double before printf receives it."%lf" is also acceptable under the current standard -- the l is specified as having no effect if followed by the f conversion specifier(among others).
“%f”是(或至少是“a”)双精度数的正确格式。浮点数没有格式,因为如果您尝试将浮点数传递给 printf,它会在 printf 接收到它之前被提升为双精度数。"%lf" 在当前标准下也是可以接受的——如果后跟 f 转换说明符(以及其他),则 l 被指定为无效。
So the reason is that when people do:
所以原因是当人们这样做时:
printf("The number is %lf", number);
It is equivalent to do:
它相当于做:
printf("The number is %f", number); //l has no effect when printing double
回答by milleniumbug
printf
specifier names don't have anything in common with names of types.
printf
说明符名称与类型名称没有任何共同之处。
They are just named that way so they are short and easy to remember.
它们只是以这种方式命名,因此它们简短且易于记忆。
float -> double -> long double
浮动 -> 双 -> 长双
%f -> %lf -> %Lf
%f -> %lf -> %Lf
(also, they couldn't name printf
double specifier as %d
because that name is already reserved for decimal representation of int
(compared to octal %o
))
(此外,他们无法命名printf
双说明符,%d
因为该名称已保留用于int
(与八进制相比%o
)的十进制表示)
@taocp's answer explains why you can use both %f
and %lf
with printf
, but note you can't do it with scanf
@taocp 的回答解释了为什么您可以同时使用%f
和%lf
with printf
,但请注意您不能使用scanf
回答by Abraham Le
The long float
is a K&R C first edition type that existed. It is synonymous with double
.
这long float
是存在的 K&R C 第一版类型。它是 的同义词double
。
After the first standard C89/C90, long float
is removed. It is not deprecated. C89/C90 is also K&R C second edition. Then there is the multilingual amendment known as C94/C95 that adds wchar_t
, as well as features such as <iso646.h>
.
第一个标准C89/C90后,long float
被删除。它没有被弃用。C89/C90也是K&R C第二版。然后有被称为C94 / C95的多语种的修订,增加了wchar_t
,以及功能,如<iso646.h>
。
Many features of K&R C first edition are deprecated but not removed until the second standard C99. Automatic return type to int is removed, and default parameter type to int and double is removed from C99. The C99 standard requires function prototype, not function declaration i.e. int function_declaration();
vs int function_prototype(void);
. It also removed the K&R C style prototype.
K&R C 第一版的许多功能已弃用,但直到第二个标准 C99 才删除。删除了 int 的自动返回类型,并从 C99 中删除了 int 和 double 的默认参数类型。C99 标准需要函数原型,而不是函数声明,即int function_declaration();
vs int function_prototype(void);
. 它还删除了 K&R C 风格的原型。
// implicit int type
main(argc, argv)
char ** argv;
// explicit int type
int main(argc, argv)
int argc;
char ** argv;
C++ started long before C was standardized. Templates were standardized in 1983, making it harder to compile to C code. It was not standardized until 1998. Old compilers may have deprecated old features that are removed with more contemporary compilers. The %lf
is a legacy of long float
that gets carried forward for C's standard library.
C++ 早在 C 被标准化之前就开始了。模板在 1983 年标准化,这使得编译成 C 代码变得更加困难。它直到 1998 年才被标准化。旧的编译器可能已经弃用了被更现代的编译器删除的旧功能。这%lf
是long float
C 标准库继承的遗产。
回答by Pete Becker
For scanf
you pass a pointer to the location where the result will be stored; you read a float
value with %f
and a double value with %lf
; you have to distinguish them because float
and double
are not required to have the same representation. For printf
you pass in the value to be displayed, and float
values get promoted to double
because they're passes as arguments in the ...
portion of the prototype; that is, for printf
there's no difference between a float
and a double
, so %f
and %lf
do the same thing.
因为scanf
你传递了一个指向结果将被存储的位置的指针;你用 读取一个float
值%f
和一个双值%lf
; 你必须区分它们,因为float
并double
没有要求有相同的表示。因为printf
您传入要显示的值,并且float
值被提升,double
因为它们作为...
原型部分中的参数传递;也就是说,对于printf
有一个没有任何区别float
和double
,所以%f
与%lf
做同样的事情。