比较double与int [英] Comparing double to an int

查看:84
本文介绍了比较double与int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读《使用C ++编程原理和实践》这本书时,在第3章中指出我们不能直接将double与int进行比较.但是,当我在Visual Studios上对其进行测试时,它运行正常且没有错误?他不能将double与int进行比较是什么意思.后来,他解释说C ++提供了一种间接方式.他是说隐式转换吗?

While reading the book Programming Principles and Practice Using C++, in Chapter 3 it sates that we can;t directly compare a double to an int. However, when I tested it out on Visual Studios, it was running fine with no errors? What does he mean by not being able to compare double to an int. Later, he explains that C++ provides an indirect way. Does he mean implicit conversion?

推荐答案

是的,存在隐式转换.

请考虑以下程序:

bool f()
{
    return 3 == 3.0;
}

如果您查看由Clang生成的AST ,那么您会看到隐式转换的位置.Clang称它为 ImplicitCastExpr :

If you look at the AST generated by Clang then you see where the implicit conversion is done. Clang calls it an ImplicitCastExpr:

  `-BinaryOperator <col:12, col:17> 'bool' '=='
    |-ImplicitCastExpr <col:12> 'double' <IntegralToFloating>
    | `-IntegerLiteral <col:12> 'int' 3
    `-FloatingLiteral <col:17> 'double' 3.000000e+00

这篇关于比较double与int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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