为什么自动装箱/拆箱在这里失败?

发布于 2024-11-26 01:00:31 字数 554 浏览 1 评论 0原文

在下面的程序中,结果是 0.0 被视为小于 Double.MIN_VALUE。为什么?

我们有一个解决方案(仅使用 Doubles 并使用 compareTo),我想了解为什么拆箱在这里失败。

import java.util.Date;
import java.util.Calendar;
import java.math.BigDecimal;

public class Test {

  public static void main(String[] args) {
    double max = 99999.9999;
    double min = Double.MIN_VALUE;
    Double test = 0.0;

    System.out.println(max > test); // expect true; is true
    System.out.println(test > min); // expect true; is false
  }
}

In the program below, the result is that 0.0 is considered less than Double.MIN_VALUE. Why?

We have a solution (work with Doubles only and use compareTo) and I want to understand why unboxing is failing here.

import java.util.Date;
import java.util.Calendar;
import java.math.BigDecimal;

public class Test {

  public static void main(String[] args) {
    double max = 99999.9999;
    double min = Double.MIN_VALUE;
    Double test = 0.0;

    System.out.println(max > test); // expect true; is true
    System.out.println(test > min); // expect true; is false
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

根据 Javadocs

MIN_VALUE

一个常量,保存双精度类型的最小正非零值 2-1074

换句话说,它大于0。

According to the Javadocs :

MIN_VALUE

A constant holding the smallest positive nonzero value of type double, 2-1074.

In other words, it is bigger than 0.

他夏了夏天 2024-12-03 01:00:31

您应该阅读 Double.MIN_VALUE规格。
它是最小可能但正的 Double 值,这意味着它大于 0.0。

A constant holding the smallest positive nonzero value of type double, 2-1074.
It is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022
and also equal to Double.longBitsToDouble(0x1L). 

You should read the Double.MIN_VALUE specification.
It's a minimum possible but positive Double value which means it's larger than 0.0.

A constant holding the smallest positive nonzero value of type double, 2-1074.
It is equal to the hexadecimal floating-point literal 0x0.0000000000001P-1022
and also equal to Double.longBitsToDouble(0x1L). 
醉态萌生 2024-12-03 01:00:31

Double.MIN_VALUE = 4.9E-324 这仍然是一个正数。我认为您正在寻找 min = - Double.MAX_VALUE

Double.MIN_VALUE = 4.9E-324 which is still a positive number. I think you are looking for min = - Double.MAX_VALUE

夜司空 2024-12-03 01:00:31

据我所知,自动装箱没有问题。
也许您只需要使用 Double.NEGATIVE_INFINITY 或 Double.POSITIVE_INFINITY 之类的东西,它们应该可以很好地与 < 配合使用。和>运营商。例如请注意

-Double.MAX_VALUE > Double.NEGATIVE_INFINITY

is true!

According to me autoboxing has no problems.
Perhaps you simply need to use something like Double.NEGATIVE_INFINITY or Double.POSITIVE_INFINITY that should work well with the < and > operators. For example note that

-Double.MAX_VALUE > Double.NEGATIVE_INFINITY

is true!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文