简体   繁体   English

Math.max似乎正在回答错误的答案

[英]Math.max seems to be returning the wrong answer

I have a list of double values that I don't know the range of and I want to find the maximum value. 我有一个双值列表,我不知道其范围,我想找到最大值。 However, the Math.max function is giving a curious result for this sample code: 但是,Math.max函数为此示例代码提供了一个奇怪的结果:

double a = -100.0;
double maxA = Double.MIN_VALUE;
maxA = Math.max(maxA, a);
System.out.println(maxA);

And the output is: 输出是:

4.9E-324

So for some reason, Double.MIN_VALUE is being considered the max when compared to -100.0. 因此,出于某种原因,与-100.0相比,Double.MIN_VALUE被认为是最大值。

Why? 为什么?

MIN_VALUE is: MIN_VALUE是:

A constant holding the smallest positive nonzero value of type double, 2^(-1074). 保持double类型的最小正非零值的常量,2 ^( - 1074)。

Not the most negative possible value. 不是最负面的可能值。

很明显-100小于4.9E-324

-100 , -99 ..... -1 , 0 , 4.9E-324 , 1 , 2 , 3......

MIN_VALUE A constant holding the smallest positive nonzero value of type double. MIN_VALUE一个常量,保持double类型的最小正非零值。

Note "positive" value. 注意“正面”值。

You are comparing it with a negative value is 1 > -1. 您将它与负值进行比较是1> -1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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