int和double之间的区别 [英] Difference between int and double
问题描述
我想知道是否有人可以告诉我为什么有人会使用int而不是double?
似乎double比int更灵活。
I was wondering if someone could tell me why anyone would use int instead of double? It seems to be that double is much more flexible than int.
谢谢
推荐答案
int
和 double
具有不同的语义。考虑分裂。 1/2
0
, 1.0 / 2.0
是 0.5
。在任何给定的情况下,其中一个答案是正确的,另一个是错误的。
int
and double
have different semantics. Consider division. 1/2
is 0
, 1.0/2.0
is 0.5
. In any given situation, one of those answers will be right and the other wrong.
也就是说,有一些编程语言,比如JavaScript,其中有64位浮点数是唯一的数字数据类型。您必须显式截断某些除法结果,以获得与Java int
相同的语义。支持整数类型的Java等语言会对整数变量进行自动截断。
That said, there are programming languages, such as JavaScript, in which 64-bit float is the only numeric data type. You have to explicitly truncate some division results to get the same semantics as Java int
. Languages such as Java that support integer types make truncation automatic for integer variables.
除了具有 double
, int
算术通常更快,较小的大小(32位与64位)可以更有效地使用缓存和数据传输带宽。
In addition to having different semantics from double
, int
arithmetic is generally faster, and the smaller size (32 bits vs. 64 bits) leads to more efficient use of caches and data transfer bandwidth.
这篇关于int和double之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!