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

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

问题描述

每次我认为我了解强制转换和转换时,我都会发现另一种奇怪的行为.

long l = 123456789L;浮动 f = l;System.out.println(f);//输出 1.23456792E8

鉴于 longfloat 具有更大的位深度,我预计需要显式转换才能编译.毫不奇怪,我们发现我们已经失去了结果的精确度.

为什么这里不需要演员表?

解决方案

longdouble 可能会被问到同样的问题 - 两种转换都可能丢失信息.

Java 语言的第 5.1.2 节规范说:

<块引用>

扩展原始转换不会丢失有关整体的信息数值的大小.的确,从积分扩大的转换类型到另一个整数类型不要完全丢失任何信息;这数值被完全保留.转换从浮动扩大到在strictfp 表达式中也加倍准确保留数值;然而,这种转换不是strictfp 可能会丢失有关的信息转换后的整体幅度价值.

int 或 long 值的转换浮动,或长期价值双倍,可能会导致损失精度——即结果可能会丢失一些最低有效位价值.在这种情况下,结果浮点值将是一个正确四舍五入的版本整数值,使用 IEEE 754舍入到最近模式(第 4.2.4 节).

换句话说,即使您可能会丢失信息,但您知道该值仍会在目标类型的整体范围内.

当然可以选择要求所有隐式转换都不会丢失任何信息 - 所以 intlongfloat 会已经显式并且 longdouble 将是显式的.(intdouble 是可以的;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 隐式(没有强制转换)将 `long` 转换为 `float`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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