Reputation: 185
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.
Upvotes: 12
Views: 27950
Reputation: 1
In Java, the Math.min(a, b)
function is used to determine the minimum value between two numbers a
and b
. It returns the smaller of the two values.
The Math.min()
method is a static method provided by the Math
class in Java's standard library. It accepts two parameters of numeric data types (such as int
, double
, float
, etc.) and returns the smaller value among them.
Here's how the Math.min()
method works:
If a
is less than b
, the method returns a
.
If b
is less than or equal to a
, the method returns b
.
If a
and b
are equal, the method returns either a
or b
.
Here's an example usage of Math.min()
in Java:
int a = 5; int b = 3; int minimum = Math.min(a, b); System.out.println("The minimum value is: " + minimum);
In this example, the output will be The minimum value is: 3
, as b
is the smaller value between a
and b
.
Upvotes: -1
Reputation: 4328
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.
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:
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):
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.
Intrinsics:
This .hpp file of the Java 8 Hotspot JVM hints that it optimizes Math.min even further with optimized machine code:
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!
Hopefully, now you know all there is to know about Math.min! :)
Upvotes: 10
Reputation: 7668
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.
For example
System.out.println(Math.min(1111, 1000));
Output as
1000
It displays minimum value from the Math.min()
Upvotes: 3
Reputation: 7130
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
public static long min(long a, long b) {
return (a <= b) ? a : b;
}
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;
}
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
Upvotes: 22
Reputation: 2783
Some efficient form of
math.min(a,b) = public static int min (a, b) {
if(a<=b) return a;
else return b;
}
Upvotes: 2
Reputation: 16351
Just check the Math.java source file :
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
Upvotes: 3