从'double'到'float'(c ++)的狭窄转换

问题描述 投票:0回答:2

此代码有一个整洁的错误。错误指出“在xPos();函数底部显示x_pos的情况下,从'double'到'float'的转换变窄了。有人可以解释为什么会这样,以及如何纠正它吗?

'''

    //grab the current position
    double x_pos = clownfish->xPos();

    // move the fish 500pixels/sec in the right direction
    // delta time is measured in milliseconds, so divide by 1000 and multiply
    x_pos += 500 * (game_time.delta.count() / 1000.f);

    // update the position of the clown fish
    clownfish->xPos(x_pos);

'''

c++ clang tidy
2个回答
0
投票

警告消息说明了问题所在:“”缩小从'double'到'float'的转换。cpp core guidelines解释了为什么这可能是一个问题。

大概,clownfish->xPos函数采用浮点参数。请将函数更改为双精度,以免丢失精度。或对x_pos使用浮点数,这样就不会失去精度。更普遍的是,不要混合使用float和willy-nilly。


0
投票

有人可以解释这是为什么以及如何纠正它?

© www.soinside.com 2019 - 2024. All rights reserved.