c++ - 代码块中的奇怪逻辑

标签 c++ codeblocks

#include <iostream>
#include <stdlib.h>
#include <iomanip>

using namespace std;

int main ()
{
    double x = 10000;
    double y = x*0.05;

    if(500 == 400+100)
        cout<<"asd1"<<endl;

    if(500 == 0.05*x)
        cout<<"asd2"<<endl;

    if(500 == y)
        cout<<"asd3"<<endl;

}

上面的代码应该打印所有三个asd1asd2asd3。正如预测的那样,Visual Studio 打印了所有三个。但是,我的代码块不会。它没有打印 asd2。我该如何解决这个问题?

最佳答案

将其更改为:

int main ()
{
    int x = 10000;
    int y = x*0.05;

    if(500 == 400+100)
        cout<<"asd1"<<endl;

    if(500 == int(0.05*x))
        cout<<"asd2"<<endl;

    if(500 == y)
        cout<<"asd3"<<endl;

}

并尝试阅读:http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html

关于c++ - 代码块中的奇怪逻辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33428881/

相关文章:

c++ - 管理 C++ 单精度和 double 混合计算的规则是什么?

python - C++中的Python var操作数

C++ 多态类、虚函数和性能转换

c - 函数 "fgetc ()"暂时无法正常工作

Visual Studio 中的 C++ 错误

C++ 输出中的完全右对齐

c++ - 程序挂起不循环

c++ - 错误 : "object" was not declared in this scope

codeblocks - 对 SDL_Init ( CodeBlocks ) 的 undefined reference

c++ - 人们实际上使用什么无锁原语在c++中进行无锁音频处理?