从双精度转换为浮点型

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

此代码有一个整洁的错误。

错误状态:Narrowing conversion from 'double' to 'float'在xPos()内显示x_pos的位置;底部的功能。

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

    //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.