为什么Java隐式(没有cast)将`long'转换为`float'? [英] Why does Java implicitly (without cast) convert a `long` to a `float`?

查看:286
本文介绍了为什么Java隐式(没有cast)将`long'转换为`float'?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我认为我了解投放和转换,我发现另一个奇怪的行为。

  long l = 123456789L; 
float f = l;
System.out.println(f); // outputs 1.23456792E8

假设 long 具有比 float 更大的位深度,我希望将需要显式转换以便于编译。



为什么在这里不需要强制转换?

c> c>到 double - 可以询问同样的问题。这两个转换都可能会丢失信息。



Java语言规范的第5.1.2节


扩展原语转换不
丢失关于数值的整体
幅度的信息。事实上,
转换从整数
类型扩展到另一个整型类型不会
丢失任何信息;
数值会完全保留。
从float变为
的转换strictfp表达式中的double也
完全保留数值;
然而,不是
strictfp的这样的转换可能丢失关于转换的
值的总量的
的信息。



将int或long值
转换为float,或将long值转换为
double,可能会导致
precision的损失 - 也就是说,结果可能会丢失

的某些最低有效位的值。在这种情况下,结果
浮点值将是
正确舍入版本的
整数值,使用IEEE 754
round-to-nearest模式(§4.2。 4)。


换句话说,即使您可能会丢失信息,您仍知道该值仍在目标类型。



这个选择肯定是要求所有隐式转换都不会丢失任何信息 - 所以 int long float >到 double 将是明确的。 ( int double 可以; double 精确地表示所有 int 值。)



在某些情况下, 。语言设计是关于妥协;你不能赢得所有。我不知道我会做出什么决定...


Every time I think I understand about casting and conversions, I find another strange behavior.

long l = 123456789L;
float f = l;
System.out.println(f);  // outputs 1.23456792E8

Given that a long has greater bit-depth than a float, I would expect that an explicit cast would be required in order for this to compile. And not surprisingly, we see that we have lost precision in the result.

Why is a cast not required here?

解决方案

The same question could be asked of long to double - both conversions may lose information.

Section 5.1.2 of the Java Language Specification says:

Widening primitive conversions do not lose information about the overall magnitude of a numeric value. Indeed, conversions widening from an integral type to another integral type do not lose any information at all; the numeric value is preserved exactly. Conversions widening from float to double in strictfp expressions also preserve the numeric value exactly; however, such conversions that are not strictfp may lose information about the overall magnitude of the converted value.

Conversion of an int or a long value to float, or of a long value to double, may result in loss of precision-that is, the result may lose some of the least significant bits of the value. In this case, the resulting floating-point value will be a correctly rounded version of the integer value, using IEEE 754 round-to-nearest mode (§4.2.4).

In other words even though you may lose information, you know that the value will still be in the overall range of the target type.

The choice could certainly have been made to require all implicit conversions to lose no information at all - so int and long to float would have been explicit and long to double would have been explicit. (int to double is okay; a double has enough precision to accurately represent all int values.)

In some cases that would have been useful - in some cases not. Language design is about compromise; you can't win 'em all. I'm not sure what decision I'd have made...

这篇关于为什么Java隐式(没有cast)将`long'转换为`float'?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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