长双字面量的 C++ 后缀是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21557816/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:42:00  来源:igfitidea点击:

What's the C++ suffix for long double literals?

c++cliteralsfloating-point-precision

提问by Walter

In C++ (and C), a floating point literal without suffix defaults to double, while the suffix fimplies a float. But what is the suffix to get a long double?

在 C++(和 C)中,没有后缀的浮点文字默认为double,而后缀f意味着float. 但是得到 a 的后缀是long double什么?

Without knowing, I would define, say,

在不知道的情况下,我会定义,说,

const long double x = 3.14159265358979323846264338328;

But my worry is that the variable xcontains fewer significant bits of 3.14159265358979323846264338328than 64, because this is a doubleliteral. Is this worry justified?

但我担心变量x包含的有效位3.14159265358979323846264338328少于 64,因为这是一个double文字。这种担心有道理吗?

回答by Vlad from Moscow

From the C++ Standard

来自 C++ 标准

The type of a floating literal is double unless explicitly specified by a suffix. The suffixes f and F specify float, the suffixes l and L specify long double.

除非后缀明确指定,否则浮动文字的类型是 double 。后缀 f 和 F 指定 float,后缀 l 和 L 指定 long double。

It is interesting to compare with corresponding paragraph of the C Standard. In C there is used term floating constantinstead of floating literalin C++:

与 C 标准的相应段落进行比较是很有趣的。在 C 中使用术语 floating constant而不是floating literal在 C++ 中:

4 An unsuffixed floating constant has type double. If suffixed by the letter f or F, it has type float. If suffixed by the letter l or L, it has type long double

4 无后缀的浮点常量具有 double 类型。如果以字母 f 或 F 为后缀,则它的类型为 float。如果以字母 l 或 L 为后缀,则类型为 long double

回答by Jens Gustedt

The C suffix is L. I'd strongly suspect that it is the same for C++.

C 后缀是L. 我强烈怀疑 C++ 也是如此。

Your worry is justified. Your literal would first be converted to a double, and thus truncated, and then converted back to long double.

你的担心是有道理的。您的文字将首先转换为 a double,从而被截断,然后转换回long double.

回答by Ivaylo Strandjev

Your concern is valid and you should use a Lsuffix for long double literal.

您的担忧是有效的,您应该L为 long double 文字使用后缀。