From this article, we are going to talk about the different types of numeric/mathematical functions provided by C#. System namespace provides an inbuilt library to called Math, which provides a valuable set of methods for numeric calculations. As these methods are static methods, we don’t need to add any reference to it. These methods are widely used in mathematical calculations, statistics, trigonometry, complex equations etc. Nevertheless, we are covering several methods from the Math class in this article.
Numeric Functions in C#
To start with, we use some fields provided by the Math class. Thereby, we don’t need to explicitly define the fields, as we can use Math class for that.
Console.WriteLine("E is = " + Math.E); // E is = 2.71828182845905 Console.WriteLine("PI is = " + Math.PI); // PI is = 3.14159265358979
Math.Min() and Math.Max() overloads in C#
From the name of the method suggests, Min() method returns the smallest number out of the 2 arguments that passed into the method and Max() returns the largest number out of the two arguments. These two methods can be overloaded with different argument types as we wish. Few of them are:
byte Max(byte, byte); decimal Max(decimal, decimal); double Max(double, double); short Max(short, short); int Max(int, int); decimal Min(decimal, decimal); double Min(double, double); short Min(short, short); int Min(int, int); long Min(long, long);
Let’s take an example to illustrate the 2 methods. For the 1st argument, we pass number 20 and for the next argument, we pass 32. In this example, we are using the method which takes 2 integer arguments and returns an integer.
Console.WriteLine("Max value is: " + Math.Max(20, 32)); //Max value is: 32 Console.WriteLine("Min value is: " + Math.Min(20, 32)); //Min value is: 20
Rounding Numbers in C#
Further, we can use functions like Round(), Floor() and Ceiling() for the rounding of the numbers. These 3 methods take a decimal number as the parameter. Round() method is used to round a number to the nearest whole number and method can be overloaded using precision as the 2nd argument.
Console.WriteLine("Rounded number is: " + Math.Round(5.39)); // Rounded number is: 5 /*Overloaded Method*/ Console.WriteLine("Rounded number is: " + Math.Round(5.55, 1)); // Rounded number is: 5.6
Floor() is used to return the largest small number which is smaller than the original number passed to the method. But Floor() method acts differently to the negative values than what we expect. Ceiling() method is the direct opposite of the Floor() method. Ceiling() method returns the smallest whole number which is greater than the original number that is passed on to the method. Both these methods are extensively used in the trigonometry and statistic calculations.
Console.WriteLine("Floor is: " + Math.Floor(5.39)); // Floor is: 5 Console.WriteLine("Floor is: " + Math.Floor(-5.7)); // Floor is: -6 Console.WriteLine("Ceiling is: " + Math.Ceiling(5.39)); // Ceiling is: 6 Console.WriteLine("Ceiling is: " + Math.Ceiling(-5.7)); // Ceiling is: -4
Trigonometric Functions in C#
Math class provides valuable methods that used in Mathematics frequently. Those methods are used in trigonometry, to do complex calculations especially related to angles.
All these 3 methods take double data type as the argument and return the specific double value. The input parameter 1st need to be converted to radians and that converted value should be used as the argument for the method.
/*Sine Function*/ double degree = 30; double radians = (degree * (Math.PI)) / 180; Console.WriteLine(Math.Sin(radians)); // 0.5 /*Cosine Function*/ double degree = 70; double radians = (degree * (Math.PI)) / 180; Console.WriteLine(Math.Cos(radians)); // 0.34 /*Tan Function*/ double degree = 12; double radians = (degree * (Math.PI)) / 180; Console.WriteLine(Math.Tan(radians)); // 0.21
Sqrt() and Pow() Functions in c#
Another useful numeric functions we come across is Sqrt() and Pow(). These are too used frequently in calculations to avoid overhead in calculating them manually. Method Sqrt() used to get the square root of a given number and only positive numbers are allowed to be passed into it. Pow() method takes 2 arguments and the 1st argument is considered to be the based value and 2nd argument is the power value. Let’s see these 2 methods using an example;
Console.WriteLine("Square Root of 100:" + Math.Sqrt(100)); //Square Root of 100: 10 Console.WriteLine("Power of 4:" + Math.Pow(4, 3)); //"Power of 4: 64
Furthermore, Math class provides methods to calculate log values and exponential values of a given number. Exp() is expected to return Euler’s number raised to the argument(exponent) passed to the method.
Log() and Log10() functions in C#
The difference between the Log() and Log10() is that; Log10() returns the algorithm value with respect to the base 10.
Console.WriteLine(Math.Log(4.55)); //1.515 Console.WriteLine(Math.Log10(4.55)); //0.658
Math class provides a large set of methods that use extensively in the programming. Because of this, the amount of manual work on doing a calculation got drastically reduced. Moreover, we can use +, -, / and * directly in the code to do the basic calculations. Also, to take the remainder after a division; we normally use modulus operator available in the C#, i.e. %. Likewise, there are several functions available to make our life easy. In this article, we have only discussed frequently used methods available in the Math class. For in-depth knowledge, we can refer to the documentation of it.