[英]Safely convert `float` to `double` without loss of precision
我有以下头部刮刀,在jshell
11.0.12
和17.0.1
中演示
jshell> 13.9f
$1 ==> 13.9
jshell> 13.9
$2 ==> 13.9
jshell> (double) 13.9f
$3 ==> 13.899999618530273
同样的事情发生在两个版本的简单编译的 class 中
strictfp public class Test {
public strictfp static void main(String[] args) {
System.out.format("%s%n", (double) 13.9f);
System.out.format("%s%n", 13.9f);
}
}
╰─➤ java Test
13.899999618530273
13.9
虽然 17 警告不再需要strictfp
warning: [strictfp] as of release 17, all floating-point expressions are evaluated strictly and 'strictfp' is not required
JLS在第 5.1.2 节中说明了这一点
A widening primitive conversion does not lose information about the overall
magnitude of a numeric value in the following cases, where the numeric value is
preserved exactly:
• from an integral type to another integral type
• from byte, short, or char to a floating-point type
• from int to double
• from float to double
在 14 中,它在项目符号列表之后包含以下内容
A widening primitive conversion from float to double that is not strictfp may
lose information about the overall magnitude of the converted value.
根据我的阅读,这是实施中的错误吗? 我发现执行此转换的唯一可靠方法是Double.parseDouble(Float.toString(13.9f))
。
根据我的阅读,这是实施中的错误吗?
不,这是您期望中的错误。 您看到的double
精度值与float
值完全相同。 精确值为 13.8999996185302734375。
这与“最接近 13.9 的double
精度值”不同,后者是 13.9000000000000003552713678800500929355621337890625。
您将值 13.8999996185302734375 分配给double
精度值,然后打印字符串表示形式 - 即 13.899999618530273,因为这足以将其与其他double
精度值完全区分开来。 如果要打印 13.9,那将是一个错误,因为有一个更接近 13.9 的double
精度值,即 13.90000000000000003552713678800500929355621337890625。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.