它是安全的假设浮点重新$ P $使用psented IEEE754用C浮? [英] Is it safe to assume floating point is represented using IEEE754 floats in C?

查看:119
本文介绍了它是安全的假设浮点重新$ P $使用psented IEEE754用C浮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

浮点处于C.实现定义所以没有任何保证。

Floating point is implementation defined in the C. So there isn't any guarantees.

我们的code需要是便携式的,我们正在讨论是否不能接受使用IEEE754在我们的协议漂浮。出于性能的考虑,将是很好,如果我们没有转换来回定点格式之间发送或接收数据时。

Our code needs to be portable, we are discussing whether or not acceptable to use IEEE754 floats in our protocol. For performance reasons it would be nice if we don't have to convert back and forth between a fixed point format when sending or receiving data.

虽然我知道有可能是关于的大小平台和架构之间的差异长期 wchar_t的。但我似乎无法找到任何具体的有关浮动双击

While I know that there can be differences between platforms and architectures regarding the size of long or wchar_t. But I can't seem to find any specific about the float and double.

我发现了什么至今字节顺序颠倒可能对大型平台。虽然有不支持浮点运算平台,其中包含浮动一个code 双击甚至无法链接。否则平台似乎要坚持IEEE754单人和双人precision。

What I found so far that the byte order maybe reversed on big endian platforms. While there are platforms without floating point support where a code containing float and double wouldn't even link. Otherwise platforms seem to stick to IEEE754 single and double precision.

因此​​,它是安全的假设,浮点在IEEE754时可用?

So is it safe to assume that floating point is in IEEE754 when available?

编辑:在回应评论:

你是什么的定义安全?

通过安全我的意思是,在一个系统上的位模式是指对另一个(字节旋转处理字节排列顺序后)相同。

By safe I mean, the bit pattern on one system means the same on the another (after the byte rotation to deal with endianness).

推荐答案

从本质上讲当前非穿卡的使用,包括嵌入式架构和异国情调的信号处理架构中所有硬件架构,提供了两个浮点系统之一:

Essentially all architectures in current non-punch-card use, including embedded architectures and exotic signal processing architectures, offer one of two floating point systems:


  • IEEE-754。

  • IEEE-754的除了嗒嗒的。即,它们大多的实施754,但在一些比较昂贵和/或繁琐的比特廉价的。

  • IEEE-754.
  • IEEE-754 except for blah. That is, they mostly implement 754, but cheap out on some of the more expensive and/or fiddly bits.

最常见的廉价奏:


  • 法拉盛非正规为零。这种无效一定有时有用的定理(特别是定理AB 可以准确地重新presented如果 0℃= A / 2 &所述; = b将; = A * 2 ),但在实践中它通常不会是一个问题

  • 不承认 INF NaN的特殊。这些架构将无法遵守有关规则 INF NaN的作为操作数,可能不饱和到 INF ,而不是生产数字,比 FLT_MAX ,它通常会被其他架构被视为 NaN的

  • 分工和平方根的适当的四舍五入。这是一个容易许多,以保证结果是在准确结果的1-3 ULPS超过1/2 ULP之内。特别常见的情况是分裂为相互+乘法,它会让你在precision的一个位来实现。

  • 较少或没有后卫数字。这是一个不寻常的低价出,但这意味着其他操作可能是1-2 ULPS关闭。

  • Flushing denormals to zero. This invalidates certain sometimes-useful theorems (in particular, the theorem that a-b can be exactly represented if 0 <= a/2 <= b <= a*2), but in practice it's generally not going to be an issue.
  • Failure to recognize inf and NaN as special. These architectures will fail to follow the rules regarding inf and NaN as operands, and may not saturate to inf, instead producing numbers that are larger than FLT_MAX, which will generally be recognized by other architectures as NaN.
  • Proper rounding of division and square root. It's a whole lot easier to guarantee that the result is within 1-3 ulps of the exact result than within 1/2 ulp. A particularly common case is for division to be implemented as reciprocal+multiplication, which loses you one bit of precision.
  • Fewer or no guard digits. This is an unusual cheap-out, but means that other operations can be 1-2 ulps off.

BUUUUT ......甚至那些除嗒嗒的架构仍然采用数字IEEE-754的重presentation。除了字节顺序问题,描述了一个浮法位双击的体系结构的基本上保证具有相同的含义在结构B

BUUUUT... even those except for blah architectures still use IEEE-754's representation of numbers. Other than byte ordering issues, the bits describing a float or double on architecture A are essentially guaranteed to have the same meaning on architecture B.

所以,只要你关心的是价值观的重新presentation,你完全的罚款。如果你关心业务的跨平台的一致性,你可能需要做一些额外的工作。

So as long as all you care about is the representation of values, you're totally fine. If you care about cross-platform consistency of operations, you may need to do some extra work.

修改:由于Chux在评论中提到,平台之间不一致的常见额外的来源是使用扩展precision,如使用x87的80位内部重新presentation 。这是一个廉价出的对面,(适当的治疗),完全符合两国IEEE-754和C标准,但它同样会导致结果,甚至编译器版本之间和之后貌似无关紧要的和无关$架构之间不同, C $ C钮。

EDIT: As Chux mentions in the comments, a common extra source of inconsistency between platforms is the use of extended precision, such as the x87's 80-bit internal representation. That's the opposite of a cheap-out, and (with proper treatment) fully conforms to both IEEE-754 and the C standard, but it will likewise cause results to differ between architectures, and even between compiler versions and following apparently minor and unrelated code changes.

这篇关于它是安全的假设浮点重新$ P $使用psented IEEE754用C浮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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