C 中的十六进制浮点常量

发布于 2024-10-14 20:56:48 字数 71 浏览 2 评论 0原文

0x0.3p10代表什么值?

上面语句中的 p 是什么意思?

0x0.3p10 represents what value?

And what's the meaning of the p in the statement above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

究竟谁懂我的在乎 2024-10-21 20:56:48

0x0.3p10 是 C99(1) 中引入的十六进制浮点文字示例。 p 将基数与指数分开。

0x0.3 位称为有效数字部分(带有可选小数的整数),指数是其缩放的 2 的幂。

该特定值的计算方式为十六进制 0.33 * 16-1 (3/16) 乘积通过 210 (1024),得到 3 * 1024 / 16192 >。以下程序证实了这一点:

#include <stdio.h>
int main (void) {
    double d = 0x0.3p10;
    printf ("%.f\n", d); // Outputs 192
    return 0;
}

C99 的 6.4.4.2 Floating Constants 节包含所有详细信息:

浮点常量有一个有效数部分,后面可以跟一个指数部分和一个指定其类型的后缀。有效数部分的组成部分可以包括表示整数部分的数字序列,后跟句点.,后跟
表示小数部分的数字序列。

指数部分的组成部分是 eEpP 后跟一个由可选的有符号数字序列组成的指数。必须存在整数部分或小数部分;对于十进制浮点常量,必须存在句点或指数部分。

有效数部分被解释为(十进制或十六进制)有理数;指数部分的数字序列被解释为十进制整数。对于十进制浮点常量,指数表示有效数部分要缩放的 10 次方。对于十六进制浮点常量,指数表示有效数部分要缩放的 2 的幂。

对于十进制浮点常量,以及当 FLT_RADIX 不是 2 的幂时的十六进制浮点常量,结果是最接近的可表示值,或者是紧邻的较大或较小的可表示值最接近的可表示值,以实现定义的方式选择。对于十六进制浮点常量,当 FLT_RADIX 为 2 的幂时,结果会正确舍入。


(1) 这些文字也在标准的 C++17 迭代中添加到 C++ 中,尽管格式化和解析它们的能力是 C++11 的一部分。

0x0.3p10 is an example of a hexadecimal floating point literal, introduced in C99(1). The p separates the base number from the exponent.

The 0x0.3 bit is called the significand part (whole with optional fraction) and the exponent is the power of two by which it is scaled.

That particular value is calculated as 0.3 in hex, or 3 * 16-1 (3/16) multiplied by 210 (1024), which gives 3 * 1024 / 16 or 192. The following program confirms this:

#include <stdio.h>
int main (void) {
    double d = 0x0.3p10;
    printf ("%.f\n", d); // Outputs 192
    return 0;
}

Section 6.4.4.2 Floating constants of C99 has all the details:

A floating constant has a significand part that may be followed by an exponent part and a suffix that specifies its type. The components of the significand part may include a digit sequence representing the whole-number part, followed by a period ., followed by a
digit sequence representing the fraction part.

The components of the exponent part are an e, E, p, or P followed by an exponent consisting of an optionally signed digit sequence. Either the whole-number part or the fraction part has to be present; for decimal floating constants, either the period or the exponent part has to be present.

The significand part is interpreted as a (decimal or hexadecimal) rational number; the digit sequence in the exponent part is interpreted as a decimal integer. For decimal floating constants, the exponent indicates the power of 10 by which the significand part is to be scaled. For hexadecimal floating constants, the exponent indicates the power of 2 by which the significand part is to be scaled.

For decimal floating constants, and also for hexadecimal floating constants when FLT_RADIX is not a power of 2, the result is either the nearest representable value, or the larger or smaller representable value immediately adjacent to the nearest representable value, chosen in an implementation-defined manner. For hexadecimal floating constants when FLT_RADIX is a power of 2, the result is correctly rounded.


(1) These literals were also added to C++ in the C++17 iteration of the standard, though the ability to format and parse them was part of C++11.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文