如果浮点范围较大,是通过浮点往返定义的行为吗? [英] Is round-trip through floating point always defined behavior if floating point range is bigger?

查看:120
本文介绍了如果浮点范围较大,是通过浮点往返定义的行为吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个算术类型,一个整数, I 和一个浮点数, F 。我还假设 std :: numeric_limits< I> :: max()小于 std :: numeric_limits& 。

Let's say I have two arithmetic types, an integer one, I, and a floating point one, F. I also assume that std::numeric_limits<I>::max() is smaller than std::numeric_limits<F>::max().

现在,假设我有一个正整数值 i 。因为 F 的可表示范围大于 I F(i)应始终定义行为。

Now, let's say I have a positive integer value i. Because the representable range of F is larger than I, F(i) should always be defined behavior.

但是,如果我有一个浮点值 f f == F(i) I(f)换句话说,是 I(F(i))总是定义的行为?

However, if I have a floating point value f such that f == F(i), is I(f) well defined? In other words, is I(F(i)) always defined behavior?

C ++ 14标准的相关部分:

Relevant section from the C++14 standard:


4.9浮动积分转换 strong> [conv.fpint]

4.9 Floating-integral conversions [conv.fpint]


  1. 浮点类型的prvalue可转换为整数类型的prvalue 。转换截断;
    即是,小数部分被丢弃。如果截断的值不能在目标类型中表示
    ,那么行为是未定义的。 [注意:如果目标类型为 bool ,请参阅4.12。 - 结束注释]

  2. 整数类型或无范围枚举类型的prvalue可以转换为浮点
    点类型的prvalue。如果可能,结果是准确的。如果被转换的值在可以表示
    的值的范围内,但该值不能被精确地表示,则它是下一个更低或更高的可表示值的实现定义的选择之一
    。 [注意:如果整数值不能
    完全表示为浮点类型的值,则会发生精度损失。 - end note ]如果要转换的值在
    之外,则可以表示的值的范围,该行为是未定义的。如果源类型为 bool ,值
    false 将转换为零,值 true 转换为一个。

  1. A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type. [ Note: If the destination type is bool, see 4.12. — end note ]
  2. A prvalue of an integer type or of an unscoped enumeration type can be converted to a prvalue of a floating point type. The result is exact if possible. If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value. [ Note: Loss of precision occurs if the integral value cannot be represented exactly as a value of the floating type. — end note ] If the value being converted is outside the range of values that can be represented, the behavior is undefined. If the source type is bool, the value false is converted to zero and the value true is converted to one.



推荐答案

否。

这可能是 i == std :: numeric_limits< I& code>,但 i F 中不能表示。

It's possible that i == std::numeric_limits<I>::max(), but that i is not exactly representable in F.


如果要转换的值在可以表示的值的范围内,但该值无法准确表示,则它是下一个更低或

If the value being converted is in the range of values that can be represented but the value cannot be represented exactly, it is an implementation-defined choice of either the next lower or higher representable value.

由于可以选择下一个更高的可表示值,因此结果 F i)不再适用于 I ,因此转换回未定义的行为。

Since the next higher representable value may be chosen, it's possible that the result F(i) no longer fits into I, so conversion back would be undefined behavior.

这篇关于如果浮点范围较大,是通过浮点往返定义的行为吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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