Java:双精度浮点数 [英] Java: double vs float
本文介绍了Java:双精度浮点数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在另一个 Bruce Eckel 练习中,我编写的代码采用一个方法并更改另一个类中的值.这是我的代码:
In another Bruce Eckel exercise, the code I've written takes a method and changes value in another class. Here is my code:
class Big {
float b;
}
public class PassObject {
static void f(Letter y) {
y.c = 'z';
} //end f()
static void g(Big z) {
z.b = 2.2;
}
public static void main(String[] args ) {
Big t = new Big();
t.b = 5.6;
System.out.println("1: t.b : " + t.b);
g(x);
System.out.println("2: t.b: " + t.b);
} //end main
}//end class
它抛出一个错误,提示可能会损失精度".
It's throwing an error saying "Possible loss of precision."
PassObject.java:13: possible loss of precision
found: double
required : float z.b = 2.2
passobject.java:20: possible loss of precision
found : double
required : float t.b = 5.6
double
不能也是float
吗?
推荐答案
是的,但是你必须指定它们是浮点数,否则它们被视为双精度数:
Yes, but you have to specify that they are floats, otherwise they are treated as doubles:
z.b = 2.2f
数字末尾的f"使其成为浮点数而不是双精度数.
The 'f' at the end of the number makes it a float instead of a double.
Java 不会自动将双精度数缩小为浮点数.
Java won't automatically narrow a double to a float.
这篇关于Java:双精度浮点数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文