This is a class
in the System
namespace. The .NET Framework provides many built-in mathematical methods. These are easier to use than custom expressions.
These methods are tested and easy to access. This page contains information on some less-common math methods like Sign and DivRem
.
Is a number positive or negative? The Math.Sign
method will help you determine this. Math.Sign
accepts many numeric types as arguments. It returns one of three values: -1, 0 or 1.
Math.Sign
method with ints and doubles as arguments.using System; 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);-1 1 0 -1 1
Here we compute the logarithms of some known numbers. The logarithm of 1 with base "e" is always zero—the method matches this result.
using System; double a = Math.Log(1); Console.WriteLine(a); // Ten to the power of 3 is 1000. double b = Math.Log10(1000); Console.WriteLine(b); double c = Math.Log(1000, 10); Console.WriteLine(c);0 3 3
Math.Exp
is the exponential function. To apply the exponential function, we either use Math.Pow
to raise E to a specified power, or call Math.Exp
with the exponent as an argument.
Math.Exp
method and then calls the Math.Pow
method such that they have equivalent results.Math.Exp
always uses Math.E
(e) as the specified number. The results of these two method calls are the same.using System; // Compute e ^ 2. double a = Math.Exp(2); double b = Math.Pow(Math.E, 2); // Write results. Console.WriteLine(a); Console.WriteLine(b);7.38905609893065 7.38905609893065
IEEERemainder
This is similar to a modulo expression, but the results are different when we use a larger second number. In many cases, the modulo operator returns the same result.
IEEERemainder
method returns a remainder of 1 for the arguments 4, 3. This is the same result as the modulo expression.IEEERemainder
, two positive numbers can have a negative remainder.using System; // Part 1: use larger first number. double a = Math.IEEERemainder(4, 3); Console.WriteLine("Part 1"); Console.WriteLine(a); Console.WriteLine(4 % 3); // Part 2: use smaller first number. double b = Math.IEEERemainder(3, 4); Console.WriteLine("Part 2"); Console.WriteLine(b); Console.WriteLine(3 % 4);Part 1 1 1 Part 2 -1 3
BigMul
This multiplies two integers. With regular multiplication, as with the star operator, the result may overflow. BigMul
avoids this problem. It returns a long.
int.MaxValue
value (2147483647) by itself. If we call Math.BigMul
, the result will be correct.BigMul
anywhere you are multiplying large integers.using System; // 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);4611686014132420609 1
DivRem
Math.DivRem
divides two numbers and returns the remainder. With the division operator we do not get the remainder in a separate variable. But with DivRem
we get both parts.
Math.DivRem
method.DivRem
method is always assigned when DivRem
returns. It will be ready to use.using System; const int a = 1000; const int b = 300; int result; int quotient = Math.DivRem(a, b, out result); Console.WriteLine(quotient); Console.WriteLine(result);3 100
To access Math.E
, use "System.Math.E
" or include the using System
directive at the top. The constant is encoded as a Float64
type in the Framework. Float64
has a 64-bit data space.
using System; double e = Math.E; // Get E constant Console.WriteLine("--- Math.E ---"); Console.WriteLine(e); // Write E constant--- Math.E --- 2.71828182845905
This example calls Acos, Cos, Cosh, Asin, Sin, Sinh, Atan, Tan and Tanh. All of these methods receive arguments in units of radians represented by the double
type.
double
arguments.using System; Console.WriteLine(Math.Acos(0)); // Inverse cosine Console.WriteLine(Math.Cos(2)); // Cosine Console.WriteLine(Math.Cosh(5)); // Hyperbolic cosine Console.WriteLine(Math.Asin(0.2)); // Inverse sine Console.WriteLine(Math.Sin(2)); // Sine Console.WriteLine(Math.Sinh(-5)); // Hyperbolic sine Console.WriteLine(Math.Atan(-5)); // Inverse tangent Console.WriteLine(Math.Tan(1)); // Tangent Console.WriteLine(Math.Tanh(0.1)); // Hyperbolic tangent 1.5707963267949 -0.416146836547142 74.2099485247878 0.201357920790331 0.909297426825682 -74.2032105777888 -1.37340076694502 1.5574077246549 0.0996679946249558
Instead of implementing these functions yourself, I suggest using the Math type. It has many public static
methods. You may be able to improve the quality of your code.