Java:double vs float
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/787181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: double vs float
提问by phill
In another Bruce Eckel exercise, the code I've written takes a method and changes value in another class. Here is my code:
在另一个 Bruce Eckel 练习中,我编写的代码采用一个方法并更改另一个类中的值。这是我的代码:
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
Can't double
s be float
s as well?
无法double
S为float
S作为呢?
thanks in advance
提前致谢
采纳答案by mipadi
Yes, but you have to specify that they are floats, otherwise they are treated as doubles:
是的,但您必须指定它们是浮点数,否则它们将被视为双精度数:
z.b = 2.2f
The 'f' at the end of the number makes it a float instead of a double.
数字末尾的“f”使其成为浮点数而不是双精度数。
Java won't automatically narrow a double to a float.
Java 不会自动将双精度数缩小为浮点数。
回答by Powerlord
No, floats can be automatically upcast to doubles, but doubles can never be floats without explicit casting because doubles have the larger range.
不,浮点数可以自动向上转换为双精度数,但双精度数在没有显式转换的情况下永远不能成为浮点数,因为双精度数具有更大的范围。
float range is 1.40129846432481707e-45
to 3.40282346638528860e+38
浮动的范围是1.40129846432481707e-45
至3.40282346638528860e+38
double range is 4.94065645841246544e-324d
to 1.79769313486231570e+308d
双范围是4.94065645841246544e-324d
到1.79769313486231570e+308d
回答by mtlynch
By default, Java will treat a decimal (e.g. "4.3
") as a double
unless you otherwise specify a float
by adding an f after the number (e.g. "4.3f
").
默认情况下,Java 会将小数点(例如“ 4.3
”)视为 a,double
除非您float
通过在数字后添加 f 来指定 a (例如“ 4.3f
”)。
You're having the same problem on both lines. First, the decimal literal is interpreted as a double by the compiler. It then attempts to assign it to b
, which is of type float
. Since a double
is 64 bits and a float
is only 32 bits (see Java's primitives documentation), Java gives you an error indicating that the float
cannot fit inside the double
. The solution is to add an f to your decimal literals.
你在两条线上都遇到了同样的问题。首先,十进制文字被编译器解释为双精度值。然后它尝试将它分配给b
,它的类型是float
。由于double
是64位,一个float
是只有32位(见Java的元文件),Java的给你指示的错误float
可能不适合里面的double
。解决方案是在十进制文字中添加 f 。
If you were trying to do the opposite (i.e. assign a float
to a double
), that would be okay since you can fit a float
's 32 bits within a double
's 64.
如果您试图做相反的事情(float
即将 a分配给 a double
),那没问题,因为您可以将 afloat
的 32 位放入 adouble
的 64 位中。
回答by Michael Borgwardt
can't doubles be floats as well?
No. Each value or variable has exactly one type (double, float, int, long, etc...). The Java Language Specificationstates exactly what happens when you try to assign a value of one type to a variable of another type. Generally, assignments of a "smaller" value to a "larger" type are allowed and done implicitly, but assignments where information could be lost because the target type is too "small" to hold all values of the origin type are not allowed by the compiler, even if the concrete value does fit into the target type.
不是。每个值或变量都只有一种类型(double、float、int、long 等...)。在Java语言规范指出,当您试图将一种类型的值赋给另一种类型的变量究竟发生了什么。通常,允许将“较小”值分配给“较大”类型并隐式完成,但由于目标类型太“小”而无法容纳原始类型的所有值而导致信息丢失的分配是不允许的。编译器,即使具体值确实适合目标类型。
That's why the compiler complains that assigning a double value (which the literal implicitly is) to a float variable could lose information, and you have to placate it by either making the value a float, or by casting explicitly.
这就是为什么编译器抱怨将 double 值(字面隐含地)分配给 float 变量可能会丢失信息,您必须通过将值设置为 float 或通过显式转换来安抚它。
One area that often causes confusions is calculations, because these are implicitly "widened" to int for technical reasons. So if you multiply two shorts and try to assign the result to a short, the compiler will complain because the result of the calculation is an int.
经常引起混淆的一个领域是计算,因为出于技术原因,这些被隐含地“扩展”为 int。因此,如果您将两个 short 相乘并尝试将结果分配给一个 short,编译器会抱怨,因为计算结果是一个 int。
回答by Peter Lawrey
Don't use float. There is almost never a good reason to use it and hasn't been for more than a decade. Just use double.
不要使用浮动。几乎从来没有一个很好的理由使用它,而且已经有十多年了。只需使用双。
回答by sumanta
float range is lower than double so a float can be easily represented in double, but the reverse is not possible because, let say we take a value which is out of float range then during convention we will lose the exact data
浮点范围比双精度低,所以浮点数可以很容易地用双精度表示,但反过来是不可能的,因为假设我们取了一个超出浮点范围的值,那么在约定期间我们将丢失确切的数据