文字双精度浮点数转换等于浮点文字吗? [英] Is literal double to float conversion equal to float literal?

查看:93
本文介绍了文字双精度浮点数转换等于浮点文字吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接分配浮点字面量 float x = 3.2f; 和隐式转换为浮点数的 double 之间的位表示形式是否有所不同?code> float x2 = 3.2; ?

即是

  #define等于(FLOAT_LITERAL)\FLOAT_LITERAL ## f == static_cast< float>(FLOAT_LITERAL)等于(3.2)&&EQUAL(55.6200093490)//等... 

所有浮点文字的

true ?

我问这个问题,因为如果数字在float的值范围内,那么clang或gcc不会抱怨缩小转换范围:通过-缩小:

启用了警告

  float f {3.422222222222222222222222222222222246454};//没有警告/错误,尽管它肯定会失去精度浮点数f2 {static_cast< double>(std :: numeric_limits< float> :: max())+ 1.0};//没有警告/错误浮动f3 {3.5e38};//错误:在{}内将'3.5e + 38'的转换范围从'double'缩小为'float'[-Wnarrowing] 

编译器进行实际范围检查很好,但这足够了吗?

解决方案

假定IEEE 754,浮点数为32位二进制,两倍为64位二进制.

根据IEEE 754舍入至最接近规则,如果将十进制分数直接从十进制转换为浮点数,则首先将其从十进制转换为双精度,然后再转换为浮点数.

例如,考虑1.0000000596046447753906250000000000000000000000000001

1.000000059604644775390625可以精确地表示为双精度值,并且恰好位于1.0和1.00000011920928955078125之间的一半,最小浮点的值大于1.0.如果直接转换,则1.0000000596046447753906250000000000000000000000000001舍入为1.00000011920928955078125,因为它大于中点.如果先将其转换为64位,则四舍五入到最接近值会将其取到中点1.000000059604644775375390625,然后将一半舍入为偶数取整为1.0.

Can there be a difference in bit-representation between a direct assignment of a floating point literal float x = 3.2f; and a double implicitly converted to a float float x2 = 3.2;?

I.e. is

#define EQUAL(FLOAT_LITERAL)\
  FLOAT_LITERAL##f == static_cast<float>(FLOAT_LITERAL)
EQUAL(3.2) && EQUAL(55.6200093490) // etc ... 

true for all floating point literals?

I ask this question, because clang or gcc do not complain about narrowing conversions if numbers are in the value range of float: Warning is enabled with -Wnarrowing:

float f {3.422222222222222222222222222222246454}; // no warning/ error although it should definitely lose precision
float f2 {static_cast<double>(std::numeric_limits<float>::max()) + 1.0}; // no warning/ error
float f3 {3.5e38}; // error: narrowing conversion of '3.5e+38' from 'double' to 'float' inside { } [-Wnarrowing]

It is great that the compiler does actual range checks, but is that sufficient?

解决方案

Assuming IEEE 754, float as 32 bit binary, double as 64 bit binary.

There are decimal fractions that round differently, under IEEE 754 round-to-nearest rules, if converted directly from decimal to float from the result of first converting from decimal to double and then to float.

For example, consider 1.0000000596046447753906250000000000000000000000000001

1.000000059604644775390625 is exactly representable as a double and is exactly half way between 1.0 and 1.00000011920928955078125, the value of the smallest float greater than 1.0. 1.0000000596046447753906250000000000000000000000000001 rounds up to 1.00000011920928955078125 if converted directly, because it is greater than the mid point. If it is first converted to 64 bit, round to nearest takes it to the mid point 1.000000059604644775390625, and then round half even rounds down to 1.0.

这篇关于文字双精度浮点数转换等于浮点文字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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