Java math.min 实际上是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19006257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:18:40  来源:igfitidea点击:

How does math.min actually work?

javamathminbuilt-in

提问by Samuel L Hymanson

I understand that all of the math functions in java are built in. But I was wondering out of curiosity how Math.min()actually works?

我知道java中的所有数学函数都是内置的。但我出于好奇想知道Math.min()实际是如何工作的?

I checked the java documentation and couldn't find anything to help me. I'm quite new to java.

我检查了 java 文档,找不到任何可以帮助我的东西。我对java很陌生。

采纳答案by Gray

int

整数

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

long

public static long min(long a, long b) {
     return (a <= b) ? a : b;
}

float

漂浮

public static float min(float a, float b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0f) && (b == 0.0f) && (Float.floatToIntBits(b) == negativeZeroFloatBits)) {
         return b;
    }
    return (a <= b) ? a : b;
 }

double

双倍的

public static double min(double a, double b) {
    if (a != a) return a;   // a is NaN
    if ((a == 0.0d) && (b == 0.0d) && (Double.doubleToLongBits(b) == negativeZeroDoubleBits)) {
        return b;
    }
    return (a <= b) ? a : b;
}

More info: Here

更多信息:这里

回答by xlecoustillier

Just check the Math.java source file:

只需检查Math.java 源文件

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

回答by Ruben Serrate

Some efficient form of

一些有效的形式

math.min(a,b) = public static int min (a, b) {
  if(a<=b) return a;
  else return b;
}

回答by Naveen Kumar Alonekar

Reference

参考

The java.lang.Math.min(int a, int b)returns the smaller of two int values. That is, the result is the value closer to negative infinity. If the arguments have the same value, the result is that same value. If either value is NaN, then the result is NaN. Unlike the numerical comparison operators, this method considers negative zero to be strictly smaller than positive zero. If one argument is positive zero and the other is negative zero, the result is negative zero.

所述java.lang.Math.min(INT A,INT B)返回较小的两个int值。也就是说,结果是更接近负无穷大的值。如果参数具有相同的值,则结果是相同的值。如果任一值为 NaN,则结果为 NaN。与数值比较运算符不同,此方法认为负零严格小于正零。如果一个参数为正零,另一个为负零,则结果为负零。

For example

例如

System.out.println(Math.min(1111, 1000));

Output as

输出为

1000

It displays minimum value from the Math.min()

它显示最小值 Math.min()

回答by tilpner

Java 7 documentation:

Java 7 文档

Returns the smaller of two int values. That is, the result the argument closer to the value of Integer.MIN_VALUE. If the arguments have the same value, the result is that same value.

返回两个 int 值中较小的一个。也就是说,结果参数更接近于 Integer.MIN_VALUE 的值。如果参数具有相同的值,则结果是相同的值。

Behaviour:

行为

Math.min(1, 2) => 1
Math.min(1F, 2) => 1F
Math.min(3D, 2F) => 2D
Math.min(-0F, 0F) => -0F
Math.min(0D, -0D) => -0D
Math.min(Float.NaN, -2) => Float.NaN
Math.min(-2F, Double.NaN) => Double.NaN

java.lang.Math and java.lang.StrictMath Source:

java.lang.Math 和 java.lang.StrictMath 来源

public static int min(int a, int b) {
    return (a <= b) ? a : b;
}

java.lang.Math Bytecode (javap -c Math.classof Oracle's JDK's JRE's rt.jar):

java.lang.Math 字节码(javap -c Math.classOracle 的 JDK 的 JRE 的 rt.jar)

public static int min(int, int);
Code:
   0: iload_0           // loads a onto the stack
   1: iload_1           // loads b onto the stack
   2: if_icmpgt     9   // pops two ints (a, b) from the stack
                        // and compares them
                        // if b>a, the jvm continues at label 9
                        // else, at the next instruction, 5
                        // icmpgt is for integer-compare-greater-than
   5: iload_0           // loads a onto the stack
   6: goto          10  // jumps to label 10
   9: iload_1           // loads 
  10: ireturn           // returns the currently loaded integer

If the comparison at 5is true, awill be loaded, the jvm will jump to 10and return a, if the comparison yields false, it will jump to 9, which will load and return b.

如果5处的比较为真,a将被加载,jvm 将跳转到10并返回a,如果比较结果为假,它将跳转到9,这将加载并返回b

Intrinsics:

内在

This .hpp fileof the Java 8 Hotspot JVM hints that it optimizes Math.min even further with optimized machine code:

Java 8 Hotspot JVM 的这个 .hpp 文件暗示它使用优化的机器代码进一步优化 Math.min:

do_intrinsic(_min, java_lang_Math, min_name, int2_int_signature, F_S)

This means the above bytecode won't be executed by the Java 8 Hotspot JVM. However, this differs from JVM to JVM, which is why I also explained the bytecode!

这意味着 Java 8 Hotspot JVM 不会执行上述字节码。但是,这因JVM而异,这就是为什么我也解释了字节码的原因!

Hopefully, now you know all there is to know about Math.min! :)

希望现在您已经了解了有关 Math.min 的所有信息!:)