小编use*_*696的帖子

C++中的异常处理:使用"throw(int)"时抛出一个double

以下程序始终输出"错误:双10.2".

我不懂为什么.根据我的说法,如果fun1()只允许抛出int,程序应该(1)崩溃(2)或将double更改为int然后抛出.这意味着,输出应为"Error:int 10".然而,事实并非如此.谁能解释一下?

void fun1() throw (int)
{
    cout<<"3";
    throw 10.2;
    cout<<"4";
}

int main()
{
    try {   fun1(); }
    catch(int i) { cout<<"Error:int" <<i <<endl;}
    catch(double i) { cout << "Error:double" << i << endl; }
    cout << endl;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ double int try-catch

8
推荐指数
1
解决办法
1736
查看次数

双重和浮动比较

根据这篇文章,当比较float和double时,float应被视为double.以下程序似乎没有遵循此声明.这种行为看起来非常不可预测.这是我的计划:

void main(void)
{
    double a = 1.1;  // 1.5
    float b = 1.1;   // 1.5
    printf("%X  %X\n", a, b);
    if ( a == b)
        cout << "success " <<endl;
    else
        cout << "fail" <<endl;
}
Run Code Online (Sandbox Code Playgroud)
  • 当我运行以下程序时,我显示"失败".
  • 但是,当我将a和b更改为1.5时,它会显示"成功".

我还打印了值的十六进制表示法.它们在两种情况下都不同.我的编译器是Visual Studio 2005

你能解释一下这个输出吗?谢谢.

c++ floating-point double floating-point-precision

1
推荐指数
2
解决办法
1435
查看次数