什么是负浮点值转换成一个unsigned int的行为? [英] What is the behaviour on converting a negative floating point value into an unsigned int?

查看:736
本文介绍了什么是负浮点值转换成一个unsigned int的行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果负浮点值转换成无符号整型的值会发生什么?标准报价将AP preciated。我现在面临的问题是转换为无符号整数类型从variant类的值,即包含浮点类型的对象。

例如:

 无符号I = -.1;


解决方案

在情况下,负值为-1.0或更低,它调用自组成部分,不确定的行为则不能再通过一个无符号数psented $ P $。否则,(如在-0.1的情况下),如果它可以被重新用一个整数类型psented $ P $,它是良好定义的行为。见C11标准,ISO 9899:2011:


  

6.3.1.4


  
  

当真实浮动型的有限值被转换为整数
  比_Bool其他类型,小数部分被丢弃(即,
  价值向零截断)。如果积分部分的值
  不能由整数类型psented重新$ P $,这种行为是未定义。 61)


再有一个非规范的脚注解释上面的文字:


  

61)的remaindering操作时执行整数类型的值
  转换为无类型不需要执行时的一个值
  真正的浮动式转换为无符号类型。因此,该范围
  便携真正浮动值是(-1,Utype_MAX + 1)。


ISO / IEC 9899:1999(C99)包含完全相同的文字

What happens if a negative floating point value is converted into a value of unsigned integral type? Standard quotes would be appreciated. The problem I'm facing is conversion into values of unsigned integral types from a variant class, that contains an object of floating-point type.

EXAMPLE:

unsigned i = -.1;

解决方案

In case the negative value is -1.0 or lower, it invokes undefined behavior since the integral part then cannot be represented by an unsigned number. Otherwise, (as in the case of -0.1), if it can be represented by an integer type, it is well-defined behavior. See the C11 standard, ISO 9899:2011:

6.3.1.4

When a finite value of real floating type is converted to an integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value of the integral part cannot be represented by the integer type, the behavior is undefined. 61)

And then there is a non-normative foot note explaining the above text:

61) The remaindering operation performed when a value of integer type is converted to unsigned type need not be performed when a value of real floating type is converted to unsigned type. Thus, the range of portable real floating values is (−1, Utype_MAX+1).

ISO/IEC 9899:1999 (C99) contains exactly the same text.

这篇关于什么是负浮点值转换成一个unsigned int的行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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