.NET Array Dictionary List String 2D Async DataTable Dates DateTime Enum File For Foreach Format IEnumerable If IndexOf Lambda Parse Path Process Property Regex Replace Row Sort Split Static StringBuilder Substring Switch Tuple Window

**Tip:** The System namespace must be included at the top of the file to directly access the Math type.

Based on:.NET 4.5C# program that uses Math methodusing System; class Program { static void Main() {// Use Math method.double sin =Math.Sin(2.5); Console.WriteLine(sin); } }Output0.598472144103957

**Tip:** Using a lookup table to cache the result of Math methods is a beneficial performance enhancement.

**Ceiling and floor:** In mathematics, these don't have much to do with actual buildings. They compute the next highest or lowest integer.

**Info:** The result -1 means negative. Zero means the number is zero. And 1 means it is positive.

**Next:** This program demonstrates the Math.Sign method with ints and doubles as arguments.

C# program that uses Math.Sign methodusing System; class Program { static void Main() { int a =Math.Sign(-5); int b = Math.Sign(5); int c = Math.Sign(0); Console.WriteLine(a); Console.WriteLine(b); Console.WriteLine(c); a = Math.Sign(-5.5); b = Math.Sign(5.5); Console.WriteLine(a); Console.WriteLine(b); } }Output-1 1 0 -1 1

**Implementation:** Sign is implemented with branch statements that test the formal parameter against zero.

**Zero:** Sign returns 0 when the argument is 0, which reflects the fact that zero is neither positive nor negative.

**Here:** To begin, this program calls the Math.Exp method and then calls the Math.Pow method such that they have equivalent results.

**Note:** The Math.Exp always uses Math.E (e) as the specified number. The results of these two method calls are the same.

C# program that uses Exp methodusing System; class Program { static void Main() {// Compute e ^ 2.double a =Math.Exp(2); double b = Math.Pow(Math.E, 2);// Write results.Console.WriteLine(a); Console.WriteLine(b); } }Output7.38905609893065 7.38905609893065

**Here:** We see that for IEEERemainder, two positive numbers can have a negative remainder.

**Tip:** This occurs when the first number is smaller than the second number. In many cases, the modulo operator returns the same result.

C# program that uses IEEERemainderusing System; class Program { static void Main() { double a = Math.IEEERemainder(4, 3); Console.WriteLine("Part 1"); Console.WriteLine(a); Console.WriteLine(4 % 3); double b = Math.IEEERemainder(3, 4); Console.WriteLine("Part 2"); Console.WriteLine(b); Console.WriteLine(3 % 4); } }OutputPart 1 1 1 Part 2 -1 3

**Here:** This program's goal is to multiply the int.MaxValue value (2147483647) by itself. If we call Math.BigMul, the result will be correct.

**But:** If we just use the * operator, the result will overflow and must be compiled in an unchecked context.

C# program that calls BigMulusing System; class Program { static void Main() {// Call BigMul.long product1 =Math.BigMul(int.MaxValue, int.MaxValue);// Use multiplication operator.long product2 = unchecked(int.MaxValue * int.MaxValue);// Display values.Console.WriteLine(product1); Console.WriteLine(product2); } }Output4611686014132420609 1

**Note:** If you query Google for 2147483647 squared, it will give you the correct result.

**So:** You should probably use BigMul anywhere you are multiplying large integers.

**Tip:** The out parameter in the DivRem method is always assigned when DivRem returns. It will be ready to use.

**Long:** The Math.DivRem method can also accommodate much larger numbers stored as longs.

C# program that calls DivRem methodusing System; class Program { static void Main() { const int a = 1000; const int b = 300; int result; int quotient =Math.DivRem(a, b, out result); Console.WriteLine(quotient); Console.WriteLine(result); } }Output3 100