为什么2.0(数字2.0)同时是double和int? [英] Why 2.0 (the number 2.0) is double and int at the same time?

查看:63
本文介绍了为什么2.0(数字2.0)同时是double和int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习Dart,并遇到了以下代码

I just started to learn Dart and came across the below code

main(){
  print(2.0 is int);
  print(2.0 is double);
  print(int is double);
}

它产生以下输出

true
true
false

我不确定为什么会生成上述输出.以上输出表明,所有整数都可以视为双精度.

I am not sure why the above output is generated. The above output suggests that all integers can be treated as doubles.

我在这里缺少明显的东西吗?任何指针都会有所帮助.谢谢.

Am I missing something obvious here. Any pointers would help. Thanks.

推荐答案

仅当在浏览器中运行代码时,您才能获得此结果.

You can only get this result if you run the code in the browser.

浏览器无法区分int和double,只知道double,因此Dart在编译为JS时也无法区分它们.

The browser does not distinguish between int and double, and knows only double, and therefore Dart also can not distinguish between them when compiled to JS.

从理论上讲,这是可能的,但是使用自定义类型来维护整数属性的性能损失会非常高.

Theoretically it would be possible but the performance penalty for using a custom type to maintain the integer properties would be prohibitively high.

另请参见 https://webdev.dartlang.org/faq#q-how-are-integers-handled-when-compiled-to-javascript

这篇关于为什么2.0(数字2.0)同时是double和int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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