简体   繁体   English

math.min实际上如何工作?

[英]How does math.min actually work?

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. 我检查了java文档,找不到任何可以帮助我的东西。 I'm quite new to java. 我对java很新。

int 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 更多信息: 这里

Java 7 documentation : Java 7文档

Returns the smaller of two int values. 返回两个int值中较小的一个。 That is, the result the argument closer to the value of Integer.MIN_VALUE. 也就是说,结果参数更接近Integer.MIN_VALUE的值。 If the arguments have the same value, the result is that same 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.class of Oracle's JDK's JRE's rt.jar) : java.lang.Math Bytecode(Oracle的JDK的JRE rt.jar的javap -c Math.class

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 5 is true, a will be loaded, the jvm will jump to 10 and return a , if the comparison yields false, it will jump to 9 , which will load and return b . 如果在5比较结果为真, 一个将被装载时,JVM将跳到10返回 ,如果比较产生false,它将跳到9,这将载入和返回b。

Intrinsics : 内在性

This .hpp file of 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. 这意味着Java 8 Hotspot JVM将不会执行上述字节码。 However, this differs from JVM to JVM, which is why I also explained the bytecode! 但是,这与JVM到JVM不同,这也是我解释字节码的原因!

Hopefully, now you know all there is to know about Math.min! 希望你现在知道所有关于Math.min的知识! :) :)

Just check the Math.java source file : 只需检查Math.java源文件

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

Reference 参考

The java.lang.Math.min(int a, int b) returns the smaller of two int values. java.lang.Math.min(int a,int b)返回两个int值中较小的一个。 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. 如果任一值为NaN,则结果为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. 如果一个参数为正零而另一个参数为负零,则结果为负零。

For example 例如

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

Output as 输出为

1000

It displays minimum value from the Math.min() 它显示Math.min()最小值

Some efficient form of 一些有效的形式

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

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

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