浮动双重误解? g ++ [英] float to double misinterpretation??? g++

查看:143
本文介绍了浮动双重误解? g ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我收到以下警告

for some reason I'm getting the following warning

filename.cpp:99:53: warning: narrowing conversion of ‘sin(((double)theta))’ from ‘double’ to ‘float’ inside { } [-Wnarrowing]
filename.cpp:99:66: warning: narrowing conversion of ‘cos(((double)theta))’ from ‘double’ to ‘float’ inside { } [-Wnarrowing]

它听起来像是试图使用'double cos(double)'等而不是'float cos(float)'等。
我一直试图想出更多的方法来建议这个到编译器,但不是任何地方。
我可以如何解决这个问题?

which makes it sounds like it's trying to use the 'double cos(double)' etc. instead of 'float cos(float)' etc. I keep trying to think of more ways to suggest this to the compiler but am not getting anywhere. What can I do to resolve this?

void foo(float theta)
{
    theta = (float)M_PI*theta/180.0f;
    MyClass variable = { 1.0f,    0.0f,         0.0f,   0.0f,
                         0.0f, cos(theta), -sin(theta), 0.0f,
                         0.0f, sin(theta),  cos(theta), 0.0f,
                         0.0f,     0.0f,        0.0f,   1.0f };
    bob = variable;
}

感谢

编辑:
将其更改为这样会使警告消失,但我仍然更愿意知道问题是什么。

changing it to this makes the warnings go away, but I'd still rather know what the problem is

float C = cos(theta), S = sin(theta);
MyClass variable = { 1.0f,    0.0f,         0.0f,   0.0f,
                 0.0f, C, -S, 0.0f,
                 0.0f, S,  C, 0.0f,
                 0.0f,     0.0f,        0.0f,   1.0f };


推荐答案

您需要使用 std :: sin std :: cos 代替 sin cos ,以便您将获得正确重载的版本。您可以看到实时的差异:

You need to use std::sin and std::cos in place of sin and cos so that you will obtain the properly overloaded versions. You can see the difference live:

MyClass variable = { 1.0f,    0.0f,         0.0f,   0.0f,
                     0.0f, std::cos(theta), -std::sin(theta), 0.0f,
                     0.0f, std::sin(theta),  std::cos(theta), 0.0f,
                     0.0f,     0.0f,        0.0f,   1.0f };

这是未指定行为来自 C库的函数是否首次在全局命名空间中声明C ++草案标准节 17.6.1.2 段落 说: 强调:

It is unspecified behavior whether the functions from C libraries are first declared in the global namespace the C++ draft standard section 17.6.1.2 Headers paragraph 4 says(emphasis mine):


除非在第18至30条和附件D中指出,每个标题cname的内容应与相应的标题name.h的内容相同,如C标准库(1.2)或C Unicode TR,如果适当的话,就像通过包含。然而,在C ++标准库中,声明(除了在C中定义为宏的名称)在命名空间std的命名空间范围(3.3.6)内。 未指定这些名称是否首先在全局命名空间范围内声明,然后通过显式使用声明(7.3.3)注入到命名空间std中。

Except as noted in Clauses 18 through 30 and Annex D, the contents of each header cname shall be the same as that of the corresponding header name.h, as specified in the C standard library (1.2) or the C Unicode TR, as appropriate, as if by inclusion. In the C++ standard library, however, the declarations (except for names which are defined as macros in C) are within namespace scope (3.3.6) of the namespace std. It is unspecified whether these names are first declared within the global namespace scope and are then injected into namespace std by explicit using-declarations (7.3.3).

所以在 C库函数在全局命名空间中的情况下,您将获得 cos sin 只有 double ,这与我们所看到的行为一致。

so in the case where the C library functions were in the global namespace you would be getting the version of cos and sin that only takes double which is consistent with the behavior we are seeing.

这篇关于浮动双重误解? g ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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