Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
MathF.Min() Method in C# with Examples
The MathF.Min() method in C# returns the smaller of two specified float values. This method is part of the MathF class, which provides mathematical functions optimized for single-precision floating-point numbers.
Syntax
Following is the syntax −
public static float Min(float val1, float val2);
Parameters
-
val1 − The first
floatnumber to compare. -
val2 − The second
floatnumber to compare.
Return Value
Returns a float value representing the smaller of the two input parameters. If either value is NaN (Not a Number), the method returns NaN.
Using MathF.Min() with Positive Numbers
using System;
public class Demo {
public static void Main() {
float val1 = 6.89f;
float val2 = 4.45f;
Console.WriteLine("First value: " + val1);
Console.WriteLine("Second value: " + val2);
Console.WriteLine("Minimum Value = " + MathF.Min(val1, val2));
}
}
The output of the above code is −
First value: 6.89 Second value: 4.45 Minimum Value = 4.45
Using MathF.Min() with Different Value Ranges
using System;
public class Demo {
public static void Main() {
float val1 = 1.00f;
float val2 = 0.11f;
Console.WriteLine("Comparing " + val1 + " and " + val2);
Console.WriteLine("Minimum Value = " + MathF.Min(val1, val2));
float val3 = -5.5f;
float val4 = 2.3f;
Console.WriteLine("Comparing " + val3 + " and " + val4);
Console.WriteLine("Minimum Value = " + MathF.Min(val3, val4));
}
}
The output of the above code is −
Comparing 1 and 0.11 Minimum Value = 0.11 Comparing -5.5 and 2.3 Minimum Value = -5.5
Using MathF.Min() with Special Values
using System;
public class Demo {
public static void Main() {
float val1 = float.PositiveInfinity;
float val2 = 100.5f;
Console.WriteLine("Min of Infinity and 100.5: " + MathF.Min(val1, val2));
float val3 = float.NaN;
float val4 = 50.0f;
Console.WriteLine("Min of NaN and 50.0: " + MathF.Min(val3, val4));
float val5 = -0.0f;
float val6 = 0.0f;
Console.WriteLine("Min of -0.0 and 0.0: " + MathF.Min(val5, val6));
}
}
The output of the above code is −
Min of Infinity and 100.5: 100.5 Min of NaN and 50.0: NaN Min of -0.0 and 0.0: -0
Conclusion
The MathF.Min() method provides an efficient way to find the smaller of two float values. It handles special cases like NaN, infinity, and signed zero appropriately, making it reliable for mathematical computations involving single-precision floating-point numbers.
