是否定义了static_cast< double(std :: nanf(“)))? [英] Is static_cast<double>(std::nanf("")) well defined?

查看:93
本文介绍了是否定义了static_cast< double(std :: nanf(“)))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

标题几乎要问所有问题,但要提供MCVE:

Title pretty much asks it all, but to provide a MCVE:

#include <cmath>

int main()
{
    float f = std::nanf("");
    double d = static_cast<double>(f);
    return 0;
}

在MSVC 2017中,fd都报告为nan,但这并不能证明任何事情,因为static_cast可能是未定义的行为.

Under MSVC 2017, both f and d report as nan, but that proves nothing, since it may be that the static_cast is undefined behavior.

以类似的方式,0.0f / 0.0f会产生-nan(ind),我将假设这是一个信号传递nan,是否遵循相同的已定义/未定义规则?同上inf.

In a similar vein, 0.0f / 0.0f produces -nan(ind) which I am going to assume is a signalling nan, does that follow the same defined / undefined rule? Ditto inf.

推荐答案

这看起来是标准的保证,我们可以从

This looks guaranteed by the standard, we can start with section [expr.static.cast]p4 which says:

如果存在从e到T的隐式转换序列,则可以将表达式e显式转换为T类型.

An expression e can be explicitly converted to a type T if there is an implicit conversion sequence from e to T ...

隐式转换序列中进行了介绍. best.ics] ,我们有以下内容:

implicit conversion sequence is covered in [over.best.ics] and we have the following:

格式正确的隐式转换序列是以下形式之一:
-(3.1)标准转换顺序,
...

A well-formed implicit conversion sequence is one of the following forms:
- (3.1) a standard conversion sequence,
...

标准转换顺序

[注:如[conv]中所述,标准转换序列是本身的身份转换(即没有转换),或者由其他四个类别的一到三个转换组成. 如果序列中有两个或多个转换,则转换将按照以下规范顺序应用:左值转换,升级或转换,资格调整. —尾注]

[ Note: As described in [conv], a standard conversion sequence is either the Identity conversion by itself (that is, no conversion) or consists of one to three conversions from the other four categories. If there are two or more conversions in the sequence, the conversions are applied in the canonical order: Lvalue Transformation, Promotion or Conversion, Qualification Adjustment. — end note ]

,我们在 [conv. fpprom] p1 表示:

float类型的prvalue可以转换为double类型的prvalue. 该值保持不变.

A prvalue of type float can be converted to a prvalue of type double. The value is unchanged.

以另一种方式 double 转换为 float 将是一种转换,并且在

Going the other way double to float would be a conversion and that is covered in [conv#double]p1:

浮点类型的prvalue可以转换为另一种浮点类型的prvalue. 如果可以在目标类型中准确表示源值,则转换的结果就是该准确表示. 如果源值在两个相邻的目标值之间,则转换的结果是这些值之一的实现定义选择.否则,行为是不确定的.

A prvalue of floating-point type can be converted to a prvalue of another floating-point type. If the source value can be exactly represented in the destination type, the result of the conversion is that exact representation. If the source value is between two adjacent destination values, the result of the conversion is an implementation-defined choice of either of those values. Otherwise, the behavior is undefined.

这种情况取决于来源是否可以由目标准确地代表,这是无法保证的.

This case depends on whether the source can be exactly represented by the destination which is not guaranteed.

关于浮点除以零的子问题很复杂,并在我对浮点除以零的行为的回答中涵盖了.

The subquestion on floating point divide by zero is complicated and covered in my answer to The behaviour of floating point division by zero.

这篇关于是否定义了static_cast&lt; double(std :: nanf(“)))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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