繁体   English   中英

在不损失精度的情况下安全地将“float”转换为“double”

[英]Safely convert `float` to `double` without loss of precision

我有以下头部刮刀,在jshell 11.0.1217.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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM