是否在任何操作系统上都保证IEEE浮点数和double大小相同? [英] Are IEEE float and double guaranteed to be the same size on any OS?

查看:101
本文介绍了是否在任何操作系统上都保证IEEE浮点数和double大小相同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究OS便携式数据库系统.我希望我们的数据库文件是操作系统可移植的,以便客户可以自行决定将其数据库文件移动到其他类型的操作系统.由于这种用例,我需要我的数据类型在操作系统之间保持一致,并且我想知道是否可以保证IEEE浮点数和双精度数在任何操作系统上都具有相同的字节大小吗?

解决方案

C ++对于浮点类型的表示几乎一无所知.

[basic.fundamental]/8说(强调我):

共有三种浮点类型:floatdoublelong double.类型double提供至少与float一样高的精度,类型long double提供至少与double一样多的精度.类型为float的值的集合是类型为double的值的子集;类型为double的值的集合是类型为long double的值的子集. 浮点类型的值表示形式是实现定义的.整数和浮点类型统称为算术类型.标准模板std::numeric_limits(18.3)的专业化应为实现指定每种算术类型的最大值和最小值.

如果仅使用floatdoublelong double编写C ++代码,除了针对特定编译器的文档中给出的保证以及std::numeric_limits可能暗示的保证之外,您几乎没有任何保证.

另一方面,IEEE 754提供了行为的精确定义以及其浮点类型的二进制表示.这些定义不足以保证在所有IEEE 754平台上具有相同的行为,因为(例如)当结果比执行两个操作更为精确时,IEEE 754有时允许将多个操作折叠在一起分别地.这可能与您的特定情况无关紧要,因为您只希望文件具有可移植性,并且可能不关心在不同平台上对文件创建相同更改的相同查询与对加载相同文件所做的关注一样多在不同平台上使用相同的方式.

所以问题是:我如何获得用于C ++的可移植IEEE 754实现?".

这个问题的答案有些棘手.大多数在合理平台上使用的C ++编译器都将提供至少与IEEE 754的binary32binary64规范相匹配的floatdouble(尽管您需要阅读每个单独的编译器的文档才能确定).

或者,您可以使用软件浮点实现或包装器,例如 FLIP libgcc的软浮动 STREFLOP .这些库有时仍会根据C ++标准对实现进行假设,但这些假设并非完全可移植,因此后果自负.

I'm working on a OS portable database system. I want our database files to be OS portable so that customers can move their database files to other kinds of OS's at their discretion. Because of this use case I need my data types to be consistent across OS's, and I'm wondering if IEEE float's and double's are guaranteed to be the same byte size on any OS?

解决方案

C++ says almost nothing about the representation of floating point types.

[basic.fundamental]/8 says (Emphasis mine):

There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of the type double; the set of values of the type double is a subset of the set of values of the type long double. The value representation of floating-point types is implementation-defined. Integral and floating types are collectively called arithmetic types. Specializations of the standard template std::numeric_limits (18.3) shall specify the maximum and minimum values of each arithmetic type for an implementation.

If you just write C++ code using float, double and long double, you have virtually no guarantees, apart from those given in the documentation for your particular compiler, and those that can be implied from std::numeric_limits.

On the other hand, IEEE 754 provides exact definitions of the behaviour and binary representation of its floating point types. These definitions are not quite enough to guarantee identical behaviour on all IEEE 754 platforms, since (for example) IEEE 754 sometimes allows multiple operations to be folded together when the result would be more precise than performing the two operations separately. This is likely to be unimportant to your specific case, since you just want the files to be portable, and probably do not care quite as much about identical queries creating identical changes to the files on different platforms as you do about identical files being loaded in identical ways on different platforms.

So the question is: "how do I get a portable IEEE 754 implementation for C++?".

The answer to this question is somewhat tricky. Most C++ compilers for reasonable platforms will provide at least float and double that approximately match IEEE 754's binary32 and binary64 specifications (although you will need to read the documentation for each individual compiler to be sure).

Alternatively, you can use a software floating point implementation or wrapper such as FLIP, libgcc's soft-float, SoftFloat, or STREFLOP. These libraries sometimes still make assumptions about the implementation that are not completely portable according to the C++ standard, so use at your own risk.

这篇关于是否在任何操作系统上都保证IEEE浮点数和double大小相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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