在 Java 中表示浮点值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3033137/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:48:14  来源:igfitidea点击:

Representing float values in Java

javafloating-point

提问by Abhishek Jain

Look at the three lines of code below.

看下面的三行代码。

  float f = 1;

  float g = 1.1;

  float h = 1.1f;

Second line has compilation errors, while the other lines do not have compilation errors. First line is working fine without suffix f and third line is working with suffix f. Why is this?

第二行有编译错误,而其他行没有编译错误。第一行在没有后缀 f 的情况下工作正常,第三行在后缀 f 下工作。为什么是这样?

采纳答案by polygenelubricants

Floating point literals in Java is a doublevalue by default.

Java 中的浮点文字double默认是一个值。

JLS 3.10.2 Floating-Point Literals

A floating-point literal is of type floatif it is suffixed with an ASCII letter For f; otherwise its type is doubleand it can optionally be suffixed with an ASCII letter Dor d.

JLS 3.10.2 浮点文字

如果浮点文字float以 ASCII 字母F或为后缀,则它属于类型f。否则它的类型是double,它可以选择后缀为 ASCII 字母Dd.

You can't assign a doublevalue to a floatwithout an explicit narrowing conversion. You therefore have two options:

您不能doublefloat没有显式收缩转换的情况下为 a赋值。因此,您有两个选择:

  • For literals, use the suffix for Fto denote a floatvalue
  • For non-literals, use an explicit cast (float)
  • 对于文字,使用后缀fF表示一个float
  • 对于非文字,使用显式转换 (float)

An example of the latter is:

后者的一个例子是:

double d = 1.1;
float f = (float) d; // compiles fine!


On widening conversions

关于扩大转化

The reason why this compiles:

编译的原因:

float f = 1;

is because the widening conversion from intto floatcan be done implicitly in the context of an assignment.

是因为从intto的扩展转换float可以在赋值的上下文中隐式完成。

JLS 5.2 Assignment Conversion

Assignment conversionoccurs when the value of an expression is assigned to a variable: the type of the expression must be converted to the type of the variable. Assignment contexts allow the use of one of the following:

  • a widening primitive conversion (§5.1.2)
  • [...]

JLS 5.1.2 Widening Primitive Conversion

The following 19 specific conversions on primitive types are called the widening primitive conversions:

  • intto long, float, or double
  • [...]

JLS 5.2 作业转换

赋值转换发生在将表达式的值赋给变量时:表达式的类型必须转换为变量的类型。赋值上下文允许使用以下之一:

  • 扩大原始转换(第 5.1.2 节)
  • [...]

JLS 5.1.2 扩大原语转换

以下 19 种关于原始类型的特定转换称为扩展原始类型转换

  • intlong, float, 或double
  • [...]


Other data type suffix for literals

文字的其他数据类型后缀

As mentioned above, there's also the Dor dsuffix for double. Consider this snippet for example:

如上所述,还有Dd后缀double。例如,考虑这个片段:

static void f(int i) {
    System.out.println("(int)");
}
static void f(double d) {
    System.out.println("(double)");
}

//...
f(1);   // prints "(int)"
f(1D);  // prints "(double)"

There's also a suffix for longliterals, which is Lor l(lowercase letter). It is highly recommendedthat you use the uppercase variant.

long文字还有一个后缀,即Lor l(小写字母)。这是强烈建议您使用大写字母变体。

JLS 3.10.1 Integer Literals

An integer literal is of type longif it is suffixed with an ASCII letter Lor l(ell); otherwise it is of type int. The suffix Lis preferred, because the letter l(ell) is often hard to distinguish from the digit 1(one).

JLS 3.10.1 整数文字

long如果以 ASCII 字母Ll( ell)为后缀,则为整数文字类型;否则它是类型int。最好使用后缀L,因为字母l( ell) 通常很难与数字1( one)区分开来。

回答by Bill the Lizard

You're assigning a doublevalue to a floatvariable. 1.1by itself (without the ftacked on the end) is assumed by the compiler to be of type double. The compiler doesn't like to make implicit downcasts because there's potential there to lose precision.

您正在doublefloat变量赋值。 编译器假定1.1其本身(没有f附加在最后)是 类型double。编译器不喜欢进行隐式向下转换,因为那里可能会失去精度。

回答by Arne Deutsch

First line autocasts int to float (ok).

第一行将 int 自动转换为 float (ok)。

Second line could not cast double to float because of lost of precision. You have to cast:

由于精度丢失,第二行无法将 double 转换为浮点数。你必须投:

float g = (float) 1.1;

Third line does not need to convert.

第三行不需要转换。

回答by Justin Ardini

In Java, every floating-point number (any number with a decimal point) defaults to a double, which is more precise than a float. And by default, Java does not let you convert a doubleto a floatbecause of the loss of precision.

在 Java 中,每个浮点数(任何带小数点的数字)都默认为 a double,这比 a 更精确float。默认情况下,由于精度损失,Java 不允许您将 a 转换double为 a float

You can still assign a doubleto a floatby casting:

您仍然可以分配doublefloat通过浇注:

float g = (float) 1.1;