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

查看:156
本文介绍了固定宽度浮点数在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.

对此有一个相当于花车?我意识到这是更复杂一点用浮漂,因为它们不是存储在一个均匀的方式,即迹象,指数,尾数。我只想要一个双击这是保证存储在64位与1个符号位,10位指数和52/53位有效位(取决于你是否算隐藏位)。

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年版标准的草案,但也似乎指定有关重新presentation像C99标准的那部分东西,超出了布尔在numeric_limits的指定IEEE 754 / IEC 559使用该平台上,像乔许·凯利提到。

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调用该格式双precision,毕竟,所以它是有道理的)。

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).

在该翻番不是双重precision,建立一个全面的检查,以便用户可以进行举报,你可以单独处理该平台关闭的机会。如果平台不支持IEEE 754,你就不会得到那些重新presentation无论如何,除非你自己实现它。

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天全站免登陆