C# Math

Number Math

Math written on chalkboard

Arithmetic expressions can implement mathematical functions. The C# language also provides many Math methods. These methods are easier to use than arithmetic expressions. They are tested and built into the .NET Framework. They solve problems in many programs.

These C# examples demonstrate the Math type. This type offers many mathematical functions ready to deploy.

Methods

As an introduction, let's examine a program that uses the Math.Sin method. This method returns the sine of a specified angle measured in radians. Most of the Math methods can be used in this way.

Program that uses Math method [C#]

using System;

class Program
{
    static void Main()
    {
	// Use Math method.
	double sin = Math.Sin(2.5);
	Console.WriteLine(sin);
    }
}

Output

0.598472144103957

Trignometry. You can use the trigonometric identities in your C# program with the Math.Cos, Math.Sin, and Math.Tan methods. Also, there are hyperbolic and inverse methods available for your use.

Cos, Sin, Tan

Absolute values. Next, we present information about how to find absolute values in the C# language. Since absolute values are always positive numbers, this example may help make you happier.

AbsFloor numeric function

Ceiling and floor. In mathematics, ceilings and floors don't have much to do with actual buildings. Instead, they are functions that compute the next highest or next lowest integer. In the C# language, the Math.Ceiling method and the Math.Floor method are available.

Ceiling Floor

Signs. Is a number positive or negative? The Math.Sign method will help you determine this in a declarative way, as we see here.

SignMathematical constant e (Euler's number)

E. The mathematical constant E is available in the Math type as well. This is not the most useful article, but it contains an interesting anecdote about Google. Also, the Math.Exp method uses the E constant internally.

E Exp

Note: The Math.E constant is of type double.

Pi mathematical symbol

PI. In the C# language, the simplest way to acquire pi is with the Math.PI constant in the System namespace. This yields a double type of the maximum number of digits allowed by doubles.

PI

Minimum and maximum. Suppose you have two numbers and want to use an expression to find the smaller or the larger of the two. The Math.Max and Math.Min methods provide this functionality; they give you the ability to find minimums and maximums declaratively.

Max Min

Square root. You can compute the square root of a number with the Math.Sqrt method. We also introduce a way to improve performance of nearly all Math methods.

SqrtPow

Powers and logs. What is three to the power of two? Instead of actually computing this in your head, you could use the C# language and Math.Pow method. For scientific problems the Pow method is useful.

Pow Log

Rounding numbers. Should you round 1.5 to 2 or 1? The Math.Round method will help you decide: you should use a formalized system for your rounding, to make it consistent. The Math.Truncate method, on the other hand, simply returns the integral part with no rounding.

Round Truncate

Multiply big integers. When you multiply large integers together, the result will overflow and be invalid. With the Math.BigMul method, you can avoid this problem and store the result in a long variable.

BigMulDivision illustration

Divide numbers. Sometimes you want to compute the remainder as a separate value when you divide two numbers. With the Math.DivRem method, you can accomplish this task in a clear way.

DivRem IEEERemainder

Summary

The C# programming language

Instead of implementing in your C# program these functions yourself, we suggest using the Math type and its many public static methods: this is a good way to ensure your code is correct. Occasionally, using a lookup table to cache the result of Math methods is a beneficial performance enhancement.

.NET