一个类型整数的值,double&同时num?

发布于 2025-01-31 16:11:12 字数 484 浏览 4 评论 0原文

我有以下表达式:

const value = 10 / 2;

在VS代码中,如果我徘徊在value上,它显示了它的类型是double,这是有道理的。但是,如果我运行以下代码行,它们都将评估为true

print(value is num); // true
print(value is double); // true
print(value is int); // true

num是超级类,并且double& int是同级类。兄弟姐妹类型的价值如何&超级类型同时?

I have the following expression:

const value = 10 / 2;

In VS Code, if I hover over value, it shows me its type is double, which makes sense. But if I run the following lines of code, they all evaluate to true:

print(value is num); // true
print(value is double); // true
print(value is int); // true

num is the super class, and double & int are sibling classes. How come a value be of the sibling type & the super type simultaneously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2025-02-07 16:11:12

您正在为网络编译,并在浏览器中运行。
DARTPAD为您做到这一点,如果您使用Web编译器(DART2JS或Dev-Compiler)编译程序,则会获得相同的效果。

在为Web编译DART时,将所有数字汇编为JavaScript号码,因为这是获得有效执行的唯一方法。

JavaScript没有整数作为单独的类型。所有JavaScript号码均为IEEE-754双精度浮点号,Dart称为double s。

因此,当您具有10/2之类的数字,该数字将在本机飞镖中创建double时,它将在网络上创建JavaScript编号5.0。整数文字5也是如此。浏览器中只有一个“ 5”数字,因此整数和双重计算都创建了相同的数字。

为了使其与类型系统一致,这意味着“ 5”是实现intdouble接口的值。像5.5的分数值仅实现double

本地运行时,整数和双打是单独的类型。

这全是在飞镖语言之旅中描述的

You are compiling for the web, and running in a browser.
DartPad does that for you, and you get the same effect if you compile your program using the web compilers (dart2js or the dev-compiler).

When compiling Dart for the web, all numbers are compiled to JavaScript numbers, because that's the only way to get efficient execution.

JavaScript does not have integers as a separate type. All JavaScript numbers are IEEE-754 double precision floating point numbers, what Dart calls doubles.

So, when you have a number like 10 / 2, which would create a double in native Dart, it creates the JavaScript number 5.0 on the web. So does the integer literal 5. There is only one "5" number in the browser, so both integer and double computations create the same number.

To make that be consistent with the type system, it means that "5" is a value which implements both the int and the double interface. A fractional value like 5.5 only implements double.

When running natively, integers and doubles are separate types.

This is all described in the Dart language tour.

少女的英雄梦 2025-02-07 16:11:12

尝试

import 'dart:io';
 
void main() {
    const value = 10 / 2;
    print(value is num); // true
    print(value is double); // true
    print(value is int); // true
}

输出是:


真的
false

的结果。

try

import 'dart:io';
 
void main() {
    const value = 10 / 2;
    print(value is num); // true
    print(value is double); // true
    print(value is int); // true
}

The output is:

true
true
false

That is expected result.

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文