Search code examples
javamathminbuilt-in

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?

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


Solution

  • 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