简体   繁体   English

float / int隐式转换

[英]float/int implicit conversion

I'm doing multiplication and division of float s and int s and I forget the implicit conversion rules (and the words in the question seem too vague to google more quickly than asking here). 我正在做floatint的乘法和除法,我忘记了隐式转换规则(问题中的单词看起来太模糊了,谷歌比在这里要求更快)。

If I have two int s, but I want to do floating-point division, do I need only to cast one or both of the operands? 如果我有两个int ,但我想做浮点除法,我是否只需要转换一个或两个操作数? How about for multiplication — if I multiply a float and an int , is the answer a float ? 乘法怎么样 - 如果我乘以一个float和一个int ,答案是float

You can't assign to an int result from division of a float by an int or vice-versa. 你不能分配给int从划分结果float通过int或反之亦然。

So the answers are: 所以答案是:

If I have two int s, but I want to do floating point division…? 如果我有两个int ,但我想做浮点除法......?

One cast is enough. 一个演员就足够了。

If I multiply a float and an int , is the answer a float ? 如果我乘以floatint ,答案是float吗?

Yes it is. 是的。


float f = 1000f;
int i = 3; 

f = i; // Ok
i = f; // Error

f = i/f; //Ok 0.003
f = f/i; //Ok 333.3333(3)

i = i/f; //Error
i = f/i; //Error

To demonstrate: 展示:

 int i1 = 5;
 float f = 0.5f;
 int i2 = 2;
 System.out.println(i1 * f);
 System.out.println(i1 / i2);
 System.out.println(((float) i1) / i2);

Result: 结果:

2.5
2 
2.5

为了使用整数执行任何类型的浮点运算,您需要将至少一个操作数转换(读取:强制转换)为float类型。

If at least one of the operands to a binary operator is of floating-point type, then the operation is a floating-point operation, even if the other is integral. 如果二元运算符的至少一个操作数是浮点类型,则操作是浮点运算,即使另一个是整数。

(Source: Java language specifications - 4.2.4 ) (来源: Java语言规范 - 4.2.4

if I multiply a float and an int, is the answer a float? 如果我乘以一个浮点数和一个int,答案是浮点数吗?

System.out.println(((Object)(1f*1)).getClass());//class java.lang.Float

(If you use DrJava, you can simply type ((Object)(1f*1)).getClass() into the interactions panel. There's a plugin for DrJava for Eclipse too.) (如果你使用DrJava,你只需在交互面板中键入((Object)(1f * 1))。getClass()。还有一个DrJava for Eclipse的插件。)

The simple answer is that Java will perform widening conversions automatically but not narrowing conversions. 简单的答案是Java会自动执行扩展转换但不会缩小转换范围。 So for example int->float is automatic but float->int requires a cast. 所以例如int-> float是自动的,但float-> int需要一个强制转换。

Java ranks primitive types in the order int < long < float < double . Java按照int < long < float < double的顺序对基本类型进行排序。 If an operator is used with different primitive types, the type which appears before the other in the above list will be implicitly converted to the other, without any compiler diagnostic, even in cases where this would cause a loss of precision. 如果运算符与不同的基本类型一起使用,则在上面列表中出现在另一个之前的类型将隐式转换为另一个,而不进行任何编译器诊断,即使在这会导致精度损失的情况下也是如此。 For example, 16777217-1.0f will yield 16777215.0f (one less than the correct value). 例如, 16777217-1.0f将产生16777215.0f(比正确值小1)。 In many cases, operations between a float and an int outside the range +/-16777216 should be performed by casting the int to double , performing the operation, and then--if necessary--casting the result to float . 在许多情况下, float和+/- 16777216范围之外的int之间的操作应该通过将intdouble ,执行操作,然后 - 如果需要 - 将结果转换为float I find the requirement for the double casting annoying, but the typecasting rules in Java require that one either use the annoying double cast or suffer the loss of precision. 我发现双重播放的要求很烦人,但Java中的类型转换规则要求使用恼人的双重演员或者精度损失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM