将双常量定义为十六进制? [英] define double constant as hexadecimal?

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

问题描述

我想使用小于1.0的最接近的数字作为浮点.通过阅读Wikipedia在 IEEE-754 上的文章,我设法找到了答案表示1.0的二进制表示形式是3FF0000000000000,因此最接近的double值实际上是0x3FEFFFFFFFFFFFFF.

I would like to have the closest number below 1.0 as a floating point. By reading wikipedia's article on IEEE-754 I have managed to find out that the binary representation for 1.0 is 3FF0000000000000, so the closest double value is actually 0x3FEFFFFFFFFFFFFF.

我知道用此二进制数据初始化双精度的唯一方法是:

The only way I know of to initialize a double with this binary data is this:

double a;
*((unsigned*)(&a) + 1) = 0x3FEFFFFF;
*((unsigned*)(&a) + 0) = 0xFFFFFFFF;

使用起来比较麻烦.

是否有更好的方法来定义此双精度数字,如果可能的话,可以将其定义为常量?

Is there any better way to define this double number, if possible as a constant?

推荐答案

十六进制浮点数和双精度文字确实存在. 语法为0x1.(尾数)p(十进制指数) 在您的情况下,语法为

Hexadecimal float and double literals do exist. The syntax is 0x1.(mantissa)p(exponent in decimal) In your case the syntax would be

double x = 0x1.fffffffffffffp-1

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

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