C# Math Functions
Mathematical functions can be performed using the System.Math class. System.Math is a static class that is included within .NET as part of the System namespace. .NET is a free, cross-platform, open source developer platform created by Microsoft.
Syntax
The Math class can be accessed by including the System namespace within a .cs file by adding the following line:
// Include the System namespace
using System;
The Math class is static and is accessed without needing to create an instance.
Example
The following example uses the Math.Pow() method to return the result of 4 raised to the power of 2:
// Include the System namespaceusing System;public class Example{public static void Main(string[] args){// Raise 4 to power of 2double x = Math.Pow(4,2);System.Console.WriteLine("x = {0}", x);// Output: x = 16}}
In cases where access to System.Math is required but access to the entire System namespace is not, an alternative syntax can be used instead. The following example uses an alternative syntax and returns the floor of 3/2 using the System.Math.Floor() method:
public class Example{public static void Main(string[] args){// Round down the result of 3/2double x = System.Math.Floor((double)3/2);System.Console.WriteLine("x = {0}", x);// Output: x = 1}}
Below are a selected list of System.Math methods:
Math Functions
- .Abs()
- Returns the absolute value of a specified number.
- .Acos()
- Returns the inverse cosine of the argument.
- .Asin()
- Returns an angle in radians whose sine is a specified number.
- .Atan()
- Returns the inverse tangent of the argument.
- .Atan2()
- Returns the angle, in radians, between the positive x-axis and the vector to point (x, y).
- .BigMul()
- Multiplies two integer values and returns the full extended result to prevent overflow.
- .Ceiling()
- Returns the smallest integer which is greater than or equal to a given number.
- .Cos()
- Returns the cosine of a given angle.
- .Cosh()
- Returns the hyperbolic cosine of a given angle.
- .DivRem()
- Calculates the quotient of two numbers and returns the remainder in an output parameter.
- .Exp()
- Returns the result of raising e to the power of a specific number.
- .Floor()
- Returns the largest whole integer which is less than or equal to the given number.
- .Max()
- Returns the greater of two specified numbers.
- .Min()
- Returns the smaller of two specified numbers.
- .Pow()
- Returns the result of a given number raised to the power of a second number.
- .Round()
- Returns a value rounded to the nearest integer.
- .Sign()
- Returns an integer value indicating the sign of a number.
- .Sin()
- Returns the sine of a given angle.
- .SinCos()
- Returns both the sine and cosine of a given angle.
- .Sinh()
- Returns the hyperbolic sine of a given angle.
- .Sqrt()
- Returns the square root of the given number.
- .Tan()
- Returns the tangent of a given angle.
- .Tanh()
- Computes the hyperbolic tangent of a given angle in radians.
- .Truncate()
- Returns the integer part of a specified number by removing any fractional digits.
- Acosh()
- Returns the inverse hyperbolic cosine of a specified number.
- Asinh()
- Returns the inverse hyperbolic sine of a number, in radians.
- Atanh()
- Returns the inverse hyperbolic tangent of a number.
- Cbrt()
- Returns the cube root of the given number.
- Clamp()
- Restricts a value to lie within a specified minimum and maximum range.
- CopySign()
- Returns a value with the magnitude of the first operand and the sign of the second operand.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn C# on Codecademy
- Jumpstart your career with this skill path, first by learning the C# language, then building web apps with ASP.NET Core and the Razor Pages.
- Includes 4 Courses
- With Certificate
- Intermediate.44 hours
- Learn Microsoft's popular C# programming language, used to make websites, mobile apps, video games, VR, and more.
- Beginner Friendly.15 hours