C / C ++中的固定宽度浮点数 [英] Fixed-width Floating-Point Numbers in C/C++

查看:86
本文介绍了C / C ++中的固定宽度浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

int 通常为32位,但在标准中, int 不能保证具有恒定的宽度。所以如果我们想要一个32位 int ,我们包括 stdint.h 并使用 int32_t

int is usually 32 bits, but in the standard, int is not guaranteed to have a constant width. So if we want a 32 bit int we include stdint.h and use int32_t.

有没有相当于浮动?我认识到浮点数更复杂一些,因为它们不是以均匀的方式存储,即符号,指数,有效位数。我只想要一个 double ,保证以1位符号位,10位指数和52/53位有效位数存储在64位(取决于你是否计数隐藏

Is there an equivalent for this for floats? I realize it's a bit more complicated with floats since they aren't stored in a homogeneous fashion, i.e. sign, exponent, significand. I just want a double that is guaranteed to be stored in 64 bits with 1 sign bit, 10 bit exponent, and 52/53 bit significand (depending on whether you count the hidden bit).

推荐答案

根据目前的C99草案标准,附件F应该是双倍。当然,这是假定您的编译器符合标准的那一部分。

According to the current C99 draft standard, annex F, that should be double. Of course, this is assuming your compilers meet that part of the standard.

对于C ++,我已经检查了0x草案和1998年版本的标准草案,但似乎没有指定任何关于C99这一部分的表示标准,超过了数字限制的一个布尔值,它指定了该平台上使用的IEEE 754 / IEC 559,像Josh Kelley所说。

For C++, I've checked the 0x draft and a draft for the 1998 version of the standard, but neither seem to specify anything about representation like that part of the C99 standard, beyond a bool in numeric_limits that specifies that IEEE 754/IEC 559 is used on that platform, like Josh Kelley mentions.

很少有平台不支持IEEE 754,尽管如此,一般来说,它不支持设计另一个浮点格式,因为IEEE 754的定义非常好,而且工作得很好 - 如果是这样的话,那么这是一个合理的假设:双重确实是64位(IEEE 754-1985调用格式双精度,毕竟这是有道理的)。

Very few platforms do not support IEEE 754, though - it generally does not pay off to design another floating-point format since IEEE 754 is well-defined and works quite nicely - and if that is supported, then it is a reasonable assumption that double is indeed 64 bits (IEEE 754-1985 calls that format double-precision, after all, so it makes sense).

关于双重不是双精度的机会,建立一个健全检查用户可以报告它,您可以单独处理该平台。如果该平台不支持IEEE 754,您将不会得到该表示,除非您自己实现。

On the off chance that double isn't double-precision, build in a sanity check so users can report it and you can handle that platform separately. If the platform doesn't support IEEE 754, you're not going to get that representation anyway unless you implement it yourself.

这篇关于C / C ++中的固定宽度浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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