The Math library in C# provides developers with various general, trigonometric, statistical, & logarithmic functions and properties in mathematics. This is ready to use, plug and play library. The library inherits from the Object class the super parent class in C#. It resides in the System namespace.
Note: One very important thing to keep in mind is that the properties and methods in Math class are all static, meaning you need not create any object of Math class to invoke them.Let us look at the various mathematical properties in the Math library.
E is the logarithmic base, specified by the small letter ‘e’ in mathematical equations. This static property holds the value of the natural logarithmic base.
Code:
using System; public class Program { public static void Main() { Console.WriteLine("The value of logarithmic base E is " + Math.E); } }
Output:
Pi, popularly written as the symbol p, is the ratio of the circumference of the circle to the diameter (roughly 3.14). This static constant holds the value of p.
Code:
using System; public class Program { public static void Main() { Console.WriteLine("The value of PI is " + Math.PI); } }
Output:
Let us look at the various mathematical functions of the C# Math library available at our disposal:
Returns the absolute value of a given number (integer, decimal, floating-point, etc.). The absolute value of any number is the maximum possible decimal value greater than or equal to 0 but lesser than or equal to the number itself.
Code:
using System; public class Program { public static void Main() { int num1 = 231; double num2 = -1.23456789; Console.WriteLine("The absolute value of {0} is {1} ", num1, Math.Abs(num1)); Console.WriteLine("The absolute value of {0} is {1} ", num2, Math.Abs(num2)); } }
Output:
This Function returns the full multiplication result of two very large integer numbers. It takes two 32 bit integers and returns a 64-bit multiplication result.
Code:
using System; public class Program { public static void Main() { int num1 = Int32.MaxValue; Console.WriteLine("Multiplication of {0}x{0} without Math function - {1}",num1, num1*num1); Console.WriteLine("Multiplication of {0}x{0} by Math BigMul function - {1}",num1, Math.BigMul(num1, num1)); } }
Output:
The floor() and the ceiling() functions return the floor and ceiling values of a specified number. The floor value of any number is the largest integer smaller than or equal to the number itself. The ceiling value of any number is the smallest integer greater than or equal to the number itself.
Code:
using System; public class Program { public static void Main() { double num1 = 548.65; Console.WriteLine("Floor value of {0} is {1}", num1, Math.Floor(num1)); Console.WriteLine("Ceil value of {0} is {1}", num1, Math.Ceiling(num1)); } }
Output:
These trigonometric functions provide the sine, cosine and tangent value of the specified angle.
Code:
using System; public class Program { public static void Main() { double angle = 120.5; Console.WriteLine("Sine value of {0} is {1}", angle, Math.Sin(angle)); Console.WriteLine("Cosine value of {0} is {1}", angle,Math.Cos(angle)); Console.WriteLine("Tangent value of {0} is {1}", angle, Math.Tan(angle)); } }
Output:
These trigonometric functions provide the hyperbolic sine, cosine and tangent value of the specified angle.
Code:
using System; public class Program { public static void Main() { double angle = 120.5; Console.WriteLine("Hyperbolic Sine value of {0} is {1}", angle, Math.Sinh(angle)); Console.WriteLine("Hyperbolic Cosine value of {0} is {1}", angle, Math.Cosh(angle)); Console.WriteLine("Hyperbolic Tangent value of {0} is {1}", angle,Math.Tanh(angle)); } }
Output:
These trigonometric functions return the angle to which the specified number is the sine, cosine or tangent value.
Code:
using System; public class Program { public static void Main() { double value = 1; Console.WriteLine("The angle of sin({0}) is {1}", value, Math.Asin(value)); Console.WriteLine("The angle of cos({0}) is {1}", value, Math.Acos(value)); Console.WriteLine("The angle of tan({0}) is {1}", value, Math.Atan(value)); } }
Output:
This function calculates the result of a division of two integers. The result is not returned in a fractional value. Rather, the quotient is returned as the return value of the function and the remainder as an output parameter.
Code:
using System; public class Program { public static void Main() { int divisor = 8; int dividend = 45; int remainder = 0; int quotient = Math.DivRem(dividend, divisor, out remainder); Console.WriteLine("{0} divided by {1} results in {2} as the quotient and {3} as the remainder.", dividend, divisor, quotient, remainder); } }
Output:
The exp function returns e to the power of the specified number.
Code:
using System; public class Program { public static void Main() { int power = 4; Console.WriteLine("{0} to the power of {1} is {2}.", Math.E, power, Math.Exp(power)); } }
Output:
The log function returns the logarithm of a specified number to a specified base. If no base is specified, the default base is e, resulting in the natural logarithm.
Note: Log2 was introduced in .Net Core. This method is not available in the .Net Framework.
Code:
using System; public class Program { public static void Main() { double num1 = 4.5; int new_base = 12; Console.WriteLine("Log({0}) to the base 'e' is {1}.", num1, Math.Log(num1)); Console.WriteLine("Log({0}) to the base 10 is {1}.", num1,Math.Log10(num1)); Console.WriteLine("Log({0}) to the base 2 is {1}.", num1,Math.Log(num1, 2)); Console.WriteLine("Log({0}) to the base {1} is {2}.", num1,new_base, Math.Log(num1, new_base)); } }
Output:
These functions compare the two numbers provided and return the smaller number or the larger number of the two.
Code:
using System; public class Program { public static void Main() { double num1 = 4.5; double num2 = -3.4; int num3 = 981; int num4 = 123; Console.WriteLine("Minimum of {0} and {1} is {2}.", num1, num2,Math.Min(num1, num2)); Console.WriteLine("Maximum of {0} and {1} is {2}.", num1, num2,Math.Max(num1, num2)); Console.WriteLine("Minimum of {0} and {1} is {2}.", num3, num4,Math.Min(num3, num4)); Console.WriteLine("Maximum of {0} and {1} is {2}.", num3, num4,Math.Max(num3, num4)); } }
Output:
The pow() function returns the specified number to the specified power.
Code:
using System; public class Program { public static void Main() { int num1 = 11; double num2 = 3.4; Console.WriteLine("{0} to the power {1} is {2}.", num1, num2, Math.Pow(num1, num2)); Console.WriteLine("The cube of {0} is {1}.", num1, Math.Pow(num1, 3)); } }
Output:
The round() function, as the name suggests, rounds the specified number to the nearest integer or specified decimal places after the integer.
There are a few important variations of round() function. It takes either two or three arguments.
The two modes are:
If not specified, the mode AwayFromZero is the default mode.
Code:
using System; public class Program { public static void Main() { double num1 = 2.45; double num2 = 24.5; Console.WriteLine("{0} rounded to the nearest integer is {1}", num1, Math.Round(num1)); Console.WriteLine("{0} rounded to the nearest single-point decimal is {1}", num1, Math.Round(num1, 1)); Console.WriteLine("{0} rounded to the nearest single-point decimal away from zero is {1}", num1, Math.Round(num1, 1, MidpointRounding.AwayFromZero)); Console.WriteLine("{0} rounded to the nearest single-point decimal to even is {1}", num1, Math.Round(num1, 1, MidpointRounding.ToEven)); Console.WriteLine("\n{0} rounded to the nearest integer away from zero is {1}", num2, Math.Round(num2, MidpointRounding.AwayFromZero)); Console.WriteLine("{0} rounded to the nearest integer to even is {1}", num2, Math.Round(num2, MidpointRounding.ToEven)); } }
Output:
This function returns the square root of the given number.
Code:
using System; public class Program { public static void Main() { int num1 = 196; double num2 = 404.1; Console.WriteLine("Square root of {0} is {1}.", num1,Math.Sqrt(num1)); Console.WriteLine("Square root of {0} is {1}.", num2, Math.Sqrt(num2)); } }
Output:
The truncate function returns an integral part of the specified number. So, in simple terms, it discards anything after the decimal point and returns everything before the decimal point.
Note: Note that this is different from Round function. The round function returns an integer nearest to the number. It may be an integer greater than the number itself. Whereas, Truncate function would always return the integer part of the number as is. E.g. – Round(4.9) results in 5. Truncate(4.9) results in 4.Code:
using System; public class Program { public static void Main() { double num1 = 404.92; Console.WriteLine("Truncated value of {0} is {1}.", num1, Math.Truncate(num1)); Console.WriteLine("Rounded-off value of {0} is {1}.", num1, Math.Round(num1)); } }
Output:
This article covered almost all the mathematical functions provided in the C# Math library. This library proves to be very useful due to the plug-n-play mathematical properties and functions, thereby making development easier.
The above is the detailed content of Math Functions in C#. For more information, please follow other related articles on the PHP Chinese website!