Number Math

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.
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.598472144103957Trignometry. 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, TanAbsolute 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.
Abs
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 FloorSigns. Is a number positive or negative? The Math.Sign method will help you determine this in a declarative way, as we see here.
Sign
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 ExpNote: The Math.E constant is of type double.

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.
PIMinimum 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 MinSquare 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.
Sqrt
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 LogRounding 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 TruncateMultiply 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.
BigMul
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
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.