添加浮动收益率 Double
Add floats yields Double
这个groovy:
float a = 1;
float b = 2;
def r = a + b;
使用 IntelliJ 从 .class 反转时创建此 Java 代码:
float a = (float)1;
float b = (float)2;
Object r = null;
double var7 = (double)a + (double)b;
r = Double.valueOf(var7);
所以 r
包含一个 Double
。
如果我这样做:
float a = 1;
float b = 2;
float r = a + b;
它生成执行双精度加法并转换回浮点数的代码:
float a = (float)1;
float b = (float)2;
float r = 0.0F;
double var7 = (double)a + (double)b;
r = (float)var7;
因此,是否应该放弃 float
和 groovy,因为它似乎无论如何都不想使用它们?
.net (C#) 对 16 位整数执行类似的操作:Addition of Bytes or Int16s yield Int32。 可能是为了防止溢出。
使用 "smaller" 数据类型的操作可能会产生 "bigger" 数据类型。更大,我的意思是更多位。
如本例所示(更多数字也意味着更多位)
15(2 位数)x 15(2 位数)= 225(3 位数)
1.5(2 位数)x 1.5(2 位数)= 2.25(3 位数)
然而,将两个 32 位整数相加 returns 只是一个 32 位整数。并且添加两个双打只是 returns 一个双打。这是因为(虚拟)机器针对这些大小进行了优化,这是因为物理处理器曾经针对这些大小进行了优化。他们中的一些人仍然是。 32 位操作通常仍然比 64 位操作快,即使在 64 位处理器上也是如此。但是,16位运算不是或者勉强。
您的编译器会尝试保护您免受溢出,并允许您显式检查它们。因此,除非您有充分的理由不这样做,否则我会默认使用这些类型,并且在存储数据时可以选择截断为更紧凑的类型。
很好的理由不包括处理大量(1000s)数字的场景,例如用于图形处理。
Groovy决定采用5种标准结果类型的数值运算。退回到某些标准数字类型进行操作。它们是 int、long、BigInteger、double 和 BigDecimal。因此 adding/multiplying 两个花车 returns 一个双精度浮点数。除法和 pow 很特别。
来自http://www.groovy-lang.org/syntax.html
Division and power binary operations aside,
binary operations between byte, char, short and int result in int
binary operations involving long with byte, char, short and int result
in long
binary operations involving BigInteger and any other integral type
result in BigInteger
binary operations between float, double and BigDecimal result in
double
binary operations between two BigDecimal result in BigDecimal
至于你是否应该放弃 float
...通常将双精度数转换为浮点数就足够了,特别是因为 groovy 会自动为你完成。
这个groovy:
float a = 1;
float b = 2;
def r = a + b;
使用 IntelliJ 从 .class 反转时创建此 Java 代码:
float a = (float)1;
float b = (float)2;
Object r = null;
double var7 = (double)a + (double)b;
r = Double.valueOf(var7);
所以 r
包含一个 Double
。
如果我这样做:
float a = 1;
float b = 2;
float r = a + b;
它生成执行双精度加法并转换回浮点数的代码:
float a = (float)1;
float b = (float)2;
float r = 0.0F;
double var7 = (double)a + (double)b;
r = (float)var7;
因此,是否应该放弃 float
和 groovy,因为它似乎无论如何都不想使用它们?
.net (C#) 对 16 位整数执行类似的操作:Addition of Bytes or Int16s yield Int32。 可能是为了防止溢出。
使用 "smaller" 数据类型的操作可能会产生 "bigger" 数据类型。更大,我的意思是更多位。
如本例所示(更多数字也意味着更多位)
15(2 位数)x 15(2 位数)= 225(3 位数)
1.5(2 位数)x 1.5(2 位数)= 2.25(3 位数)
然而,将两个 32 位整数相加 returns 只是一个 32 位整数。并且添加两个双打只是 returns 一个双打。这是因为(虚拟)机器针对这些大小进行了优化,这是因为物理处理器曾经针对这些大小进行了优化。他们中的一些人仍然是。 32 位操作通常仍然比 64 位操作快,即使在 64 位处理器上也是如此。但是,16位运算不是或者勉强。
您的编译器会尝试保护您免受溢出,并允许您显式检查它们。因此,除非您有充分的理由不这样做,否则我会默认使用这些类型,并且在存储数据时可以选择截断为更紧凑的类型。
很好的理由不包括处理大量(1000s)数字的场景,例如用于图形处理。
Groovy决定采用5种标准结果类型的数值运算。退回到某些标准数字类型进行操作。它们是 int、long、BigInteger、double 和 BigDecimal。因此 adding/multiplying 两个花车 returns 一个双精度浮点数。除法和 pow 很特别。
来自http://www.groovy-lang.org/syntax.html
Division and power binary operations aside,
binary operations between byte, char, short and int result in int
binary operations involving long with byte, char, short and int result in long
binary operations involving BigInteger and any other integral type result in BigInteger
binary operations between float, double and BigDecimal result in double
binary operations between two BigDecimal result in BigDecimal
至于你是否应该放弃 float
...通常将双精度数转换为浮点数就足够了,特别是因为 groovy 会自动为你完成。