C中的十六进制浮点常量 [英] hexadecimal floating constant in C

查看:28
本文介绍了C中的十六进制浮点常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

0x0.3p10代表什么值?

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

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

推荐答案

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

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

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

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.

该特定值以十六进制计算为 0.3,或 3 * 16-1 (3/16) 乘以 210 (1024),得到 3 * 1024/16192.

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
", d);
    return 0;
}

C99 的 6.4.4.2 部分有所有细节:

Section 6.4.4.2 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.

指数部分的组成部分是一个 e、E、p 或 P,后跟一个由可选带符号的数字序列组成的指数.必须存在整数部分或小数部分;对于十进制浮点常量,必须存在句点或指数部分.

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.

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

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.

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

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.

这篇关于C中的十六进制浮点常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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