在Java中将Long转换为double,我发现了一些差异 [英] Converting Long to double in java, i found some discrepancy

查看:48
本文介绍了在Java中将Long转换为double,我发现了一些差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我尝试过的:

public class LongToDoubleTest{
    public static void main(String... args) {
        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MAX_VALUE/2);
        System.out.println(Math.floor(Long.MAX_VALUE/2));
        System.out.println(new Double(Math.floor(Long.MAX_VALUE/2)).longValue());
    }
}

以下是输出:

9223372036854775807
4611686018427387903
4.6116860184273879E18
4611686018427387904

我最初试图弄清楚,是否有可能将Long.MAX_VALUE的一半保留为两倍而不丢失数据,所以我对所有这些行(最后一行除外)进行了测试.所以看来我是对的,最后一个 3 丢失了.然后,为澄清起见,我添加了最后一行.并没有出现 3 ,而是出现了 4 .所以我的问题是,这些 4 从何处出现,为什么是 4 而不是 3 .因为 4 实际上是一个不正确的值.P.S.我对 IEEE 754 的了解很差,所以也许我发现的行为是绝对正确的,但是 4 在这里显然是错误的值.

I was initially trying to figure out, is it possible to keep half of the Long.MAX_VALUE in double without losing data, So I had a test with all those lines, except the last one. So it appeared that i'm right and last 3 was missing. Then, just to clarify it, I added last line. And not 3 appeared but 4. So my question is, from where those 4 appeared and why it's 4 and not 3. Because 4 is actually an incorrect value here. P.S. I'm very poor in knowledge of IEEE 754, so maybe behaviour I found is absolutely correct, but 4 is obviously a wrong value here.

推荐答案

您需要了解,并不是每一个 long 都可以精确地表示为 double -毕竟,有256个 8 long 值,最多 个很多 double 值(尽管其中许多保留用于非数字"值等).假设还有 double 个值,这些值显然不是 long 值(例如0.5-首先是任何非整数),这意味着每个 long 值都不可能有一个 double 值.

You need to understand that not every long can be exactly represented as a double - after all, there are 2568 long values, and at most that many double values (although lots of those are reserved for "not a number" values etc). Given that there are also double values which clearly aren't long values (e.g. 0.5 - any non-integer, for a start) that means there can't possibly be a double value for every long value.

这意味着,如果您以无法表示的 long 值开头,请将其转换为 double ,然后再返回为 long ,取回另一个号码是完全合理的.

That means if you start with a long value that can't be represented, convert it to a double and then back to a long, it's entirely reasonable to get back a different number.

相邻的 double 值之间的绝对差随着数字的大小变大而增加.因此,当数字很小时,两个数字之间的差异确实很小(的确很小),但是当数字变大时,例如高于 int 的范围-数字之间的间隔变得更大...甚至大于1.因此,靠近 Long.MAX_VALUE 的相邻 double 值可以相距很远.这意味着多个 long 值将映射到同一最接近的 double .

The absolute difference between adjacent double values increases as the magnitude of the numbers gets larger. So when the numbers are very small, the difference between two numbers is really tiny (very very small indeed) - but when the numbers get bigger - e.g. above the range of int - the gap between numbers becomes greater... greater even than 1. So adjacent double values near Long.MAX_VALUE can be quite a distance apart. That means several long values will map to the same nearest double.

这篇关于在Java中将Long转换为double,我发现了一些差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆