In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article is about why Math.min () is larger than Math.max () in javascript. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Why many people have this kind of doubt is because they have made the mistake of taking it for granted. Math.min (), as a function, does not return the minimum value; similarly, Math.max () does not return the maximum value. They return the maximum and minimum values of the parameters, but we do not pass any parameters.
The maximum and minimum constants of the values in javascript are Number.MAX_VALUE and Number.MIN_VALUE respectively, and the running results of V8 on my computer are 1.7976931348623157e+308 and 5e-324.
There is also a pit, that is, MIN_VALUE is not the smallest number, but the absolute minimum number, it is a positive number. The return value of Math.min () is even larger than that of Number.MAX_VALUE.
Math.min () returns Infinity, which corresponds to the Number.POSITIVE_INFINITY constant. Math.max () returns-Infinity, which corresponds to the Number.NEGATIVE_INFINITY constant.
As in mathematics, positive infinity and negative infinity are not exact numbers, but concepts in sets. We can use 0 minus positive infinity to get negative infinity: 0-Infinity =-Infinity, or 0 minus negative infinity to get positive infinity: 0-(- Infinity) = Infinity. But when we add positive infinity to negative infinity, we don't get zero. In fact, what we get is NaN.
When we really understand that Math.min and Math.max only return the maximum value of all parameters, it is easy to understand this seemingly bizarre result.
I wanted to check the source code of V8, but I searched around and found only a few test cases (file: test/mjsunit/math-min-max.js):
AssertEquals (Infinity, Math.min ())
AssertEquals (1, Math.min (1))
AssertEquals (1, Math.min (1, 2))
AssertEquals (1, Math.min (2, 1))
AssertEquals (1, Math.min (1,2,3))
AssertEquals (1, Math.min (3,2,1))
AssertEquals (1, Math.min (2, 3, 1))
As you can see from the test case, Math.min () does return Infinity. Continue to read this test case and you'll find something more interesting:
AssertEquals (- Infinity, Infinity / Math.min (- 0, + 0))
AssertEquals (- Infinity, Infinity / Math.min (+ 0,-0))
AssertEquals (- Infinity, Infinity / Math.min (+ 0,-0,1))
Compare the following results:
AssertEquals (Infinity, Infinity / Math.max (- 0, + 0))
AssertEquals (Infinity, Infinity / Math.max (+ 0,-0))
AssertEquals (Infinity, Infinity / Math.max (+ 0,-0,-1))
In other words, when Math.min and Math.max make numerical comparisons (javascript non-integral and floating point), negative zeros are less than positive zeros.
The concepts of-0, + 0,-Infinity, + Infinity and NaN can be talked about for a day and a night. If the space is limited, we will not start to talk about it, and we will have time to fill in the hole later.
Back to the point, now let's try to consider the maximum and minimum from the point of view of the algorithm.
How do we find the maximum of N parameters? For simplicity of description, we use arrays (thanks to @ f2er front-end encyclopedia)
Var max = _
Arr.forEach (function (n) {
If (n > max) {
Max = n
}
});
If you fill in the blanks, what will you fill in the blanks? In this way, the return value is reasonable.
It was originally thought that the unit of the operation was returned when there was no parameter call, but after careful consideration, it seemed to have nothing to do with it, 2333.
For the binary operation * on the operation set S, if axix = x = x, then an is the unit of the * operation. The unit of the addition operation is 0, because 0x = x = x. The unit of multiplication is 1, because 1 is x = x = x. But if we define the function add () or mult () function, it is also unreasonable to return unitary elements if we do not pass parameters.
Thank you for reading! This is the end of the article on "Why Math.min () is bigger than Math.max () in javascript". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.