在分割之前将类型转换为浮点数.我真的需要哪些演员表,哪些可以删除?为什么?

Kei*_*ane 3 java types casting

当用int分隔时,我希望结果是浮点数,我可以得到类似的东西.

float ratio = landscape ? 
              ((float) image.getWidth()) / ((float)image.getHeight()) : 
              ((float)image.getHeight()) / ((float)image.getWidth());
Run Code Online (Sandbox Code Playgroud)

但是我想我不需要每个演员.潜水时有时似乎是自动投入.有什么规则,这是什么时候发生的?

Jon*_*eet 8

您需要为每个分区至少投射一个操作数.另一个操作数将自动升级.

我个人提取局部变量,此时它都是隐含的(这就是为什么我会有一个注释):

// Use floating point promotion to avoid integer division
float width = image.getWidth();
float height = image.getHeight();
float ratio = landscape ? width / height : height / width;
Run Code Online (Sandbox Code Playgroud)