IEEE float和double保证在任何操作系统上的大小相同? [英] IEEE float and double's guaranteed to be the same size on any OS?

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

问题描述

我正在使用操作系统便携式数据库系统。我想我们的数据库文件是操作系统可移植的,以便客户可以将他们的数据库文件移动到其他类型的操作系统的自由裁量权。因为这个用例,我需要我的数据类型在操作系统是一致的,我想知道是否IEEE float和doubles是保证在任何操作系统上相同的字节大小?

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 ++几乎没有说明浮点类型的表示方式。



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


有三种浮点类型: float double long double 。类型 double 提供至少与 float 一样多的精度,类型 long double 提供至少与双精度一样多的精度。类型 float 的值的集合是 double 类型的值的集合的子集;类型 double 的值的集合是类型 long double 的值的集合的子集。 浮点类型的值表示形式是实施定义的。积分和浮动类型统称为算术类型。标准模板 std :: numeric_limits (18.3)的特殊化应指定实现的每种算术类型的最大值和最小值。

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.

如果你只是使用 float double long double ,除了您的特定编译器的文档中提供的以及那些可以从 std ::

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.

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

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.

这个问题的答案有点棘手。用于合理平台的大多数C ++编译器将提供大致匹配IEEE 754的 float double binary32 binary64 规范(虽然您需要阅读每个编译器的文档以确保)。

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

或者,您可以使用软件浮点实现或包装器,例如 FLIP libgcc's soft-float SoftFloat STREFLOP 。这些库有时仍然对根据C ++标准不完全可移植的实现做出假设,所以使用风险自负。

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 float和double保证在任何操作系统上的大小相同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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