假设在 C 中使用 IEEE754 浮点数表示浮点数是否安全? [英] Is it safe to assume floating point is represented using IEEE754 floats in C?

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

问题描述

浮点是在 C 中定义的实现.因此没有任何保证.

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

我们的代码需要可移植,我们正在讨论是否可以在我们的协议中使用 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.

虽然我知道平台和架构之间在 longwchar_t 的大小方面可能存在差异.但我似乎找不到关于 floatdouble 的任何具体信息.

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.

到目前为止,我发现字节顺序可能在大端平台上颠倒了.虽然有些平台不支持浮点,但包含 floatdouble 的代码甚至无法链接.否则平台似乎坚持使用 IEEE754 单精度和双精度.

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?

回应评论:

你对安全"的定义是什么?

What is your definition of "safe"?

我的意思是安全的,一个系统上的位模式在另一个系统上意味着相同(在字节旋转以处理字节顺序之后).

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 在因子 2 内,则可以精确表示 ab 的定理),但实际上这通常不会成为问题.
  • 未能将 infNaN 识别为特殊的.这些架构将无法遵循将 infNaN 作为操作数的规则,并且可能不会饱和到 inf,而是产生大于FLT_MAX,一般会被其他架构识别为NaN.
  • 除法和平方根的正确舍入.保证结果在精确结果的 1-3 ulp 范围内比在 1/2 ulp 范围内要容易得多.一个特别常见的情况是将除法实现为倒数+乘法,这会损失一点精度.
  • 更少或没有保护数字.这是一个不寻常的便宜货,但意味着其他操作可能会降低 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 a and b are within a factor of 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...即使是那些除了blah架构仍然使用IEEE-754的数字表示.除了字节顺序问题,架构 A 上描述 floatdouble 的位基本上保证在架构 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.

所以只要你关心的只是价值观的表现,你就完全没问题.如果您关心操作的跨平台一致性,您可能需要做一些额外的工作.

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 在评论中提到的,平台之间不一致的一个常见额外来源是使用扩展精度,例如 x87 的 80 位内部表示.这与便宜货相反,并且(通过适当的处理)完全符合 IEEE-754 和 C 标准,但同样会导致结果在架构之间甚至在编译器版本之间有所不同,并且遵循明显次要且不相关的代码变化.但是:由于精度更高,特定的 x86/x64 可执行文件不会在不同的处理器上产生不同的结果.

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. However: a particular x86/x64 executable will NOT produce different results on different processors due to extended precision.

这篇关于假设在 C 中使用 IEEE754 浮点数表示浮点数是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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