The Math Class


Math defines several standard mathematical operations, such as square root, sine, cosine, and logarithms. The Math class is sealed, which means that it cannot be inherited. The methods defined by Math are shown in Table 20-1. All angles are in radians. Notice that all of the methods defined by Math are static. Thus, there is no need to construct a Math object, and no public Math constructors are provided.

Table 20-1: The Methods Defined by Math

Method

Meaning

public static double Abs(double v)

Returns the absolute value of v.

public static float Abs(float v)

Returns the absolute value of v.

public static decimal Abs(decimal v)

Returns the absolute value of v.

public static int Abs(int v)

Returns the absolute value of v.

public static short Abs(short v)

Returns the absolute value of v.

public static long Abs(long v)

Returns the absolute value of v.

public static sbyte Abs(sbyte v)

Returns the absolute value of v.

public static double Acos(double v)

Returns the arc cosine of v. The value of v must be between 1 and 1.

public static double Asin(double v)

Returns the arc sine of v. The value of v must be between 1 and 1.

public static double Atan(double v)

Returns the arc tangent of v.

public static double Atan2(double y, double x)

Returns the arc tangent of y / x.

public static long BigMul(int x, int y)

Returns the result of x * y as a long value, thus avoiding overflow.

public static double Ceiling(double v)

Returns the smallest integer (represented as a floating-point value) not less than v. For example, given 1.02, Ceiling( ) returns 2.0. Given 1.02, Ceiling( ) returns 1.

public static double Ceiling(decimal v)

Returns the smallest integer (represented as a decimal value) not less than v. For example, given 1.02, Ceiling( ) returns 2.0. Given 1.02, Ceiling( ) returns 1. (Added by C# 2.0.)

public static double Cos(double v)

Returns the cosine of v.

public static double Cosh(double v)

Returns the hyperbolic cosine of v.

public static int DivRem(int x, int y, out int rem)

Returns the result of x / y. The remainder is returned in rem.

public static long DivRem(long x, long y, out long rem)

Returns the result of x / y. The remainder is returned in rem.

public static double Exp(double v)

Returns the natural logarithm base e raised to the v power.

public static double Floor(double v)

Returns the largest integer (represented as a floating-point value) not greater than v. For example, given 1.02, Floor( ) returns 1.0. Given 1.02, Ceiling( ) returns 2.

public static double

IEEERemainder(double dividend, double divisor)

Returns the remainder of dividend / divisor.

public static double Log(double v)

Returns the natural logarithm for v.

public static double Log(double v, double base)

Returns the logarithm for v using base base.

public static double Log10(double v)

Returns the base 10 logarithm for v.

public static double Max(double v1, double v2)

Returns the greater of v1 and v2.

public static float Max(float v1, float v2)

Returns the greater of v1 and v2.

public static decimal Max(decimal v1, decimal v2)

Returns the greater of v1 and v2.

public static int Max(int v1, int v2)

Returns the greater of v1 and v2.

public static short Max(short v1, short v2)

Returns the greater of v1 and v2.

public static long Max(long v1, long v2)

Returns the greater of v1 and v2.

public static uint Max(uint v1, uint v2)

Returns the greater of v1 and v2.

public static ushort Max(ushort v1, ushort v2)

Returns the greater of v1 and v2.

public static ulong Max(ulong v1, ulong v2)

Returns the greater of v1 and v2.

public static byte Max(byte v1, byte v2)

Returns the greater of v1 and v2.

public static sbyte Max(sbyte v1, sbyte v2)

Returns the greater of v1 and v2.

public static double Min(double v1, double v2)

Returns the lesser of v1 and v2.

public static float Min(float v1, float v2)

Returns the lesser of v1 and v2.

public static decimal Min(decimal v1, decimal v2)

Returns the lesser of v1 and v2.

public static int Min(int v1, int v2)

Returns the lesser of v1 and v2.

public static short Min(short v1, short v2)

Returns the lesser of v1 and v2.

public static long Min(long v1, long v2)

Returns the lesser of v1 and v2.

public static uint Min(uint v1, uint v2)

Returns the lesser of v1 and v2.

public static ushort Min(ushort v1, ushort v2)

Returns the lesser of v1 and v2.

public static ulong Min(ulong v1, ulong v2)

Returns the lesser of v1 and v2.

public static byte Min(byte v1, byte v2)

Returns the lesser of v1 and v2.

public static sbyte Min(sbyte v1, sbyte v2)

Returns the lesser of v1 and v2.

public static double Pow(double base, double exp)

Returns base raised to the exp power(baseexp).

public static double Round(double v)

Returns v rounded to the nearest whole number.

public static decimal Round(decimal v)

Returns v rounded to the nearest whole number.

public static double Round(double v, int frac)

Returns v rounded to the number of fractional digits specified by frac.

public static decimal Round(decimal v, int frac)

Returns v rounded to the number of fractional digits specified by frac.

public static double Round(double v, MidpointRounding how)

Returns v rounded to the nearest whole number using the rounding mode specified by how. (Added by C# 2.0.)

public static decimal Round(decimal v, MidpointRounding how)

Returns v rounded to the nearest whole number using the rounding mode specified by how. (Added by C# 2.0.)

public static double Round(double v,int frac, MidpointRounding how)

Returns v rounded to the number of fractional digits specified by frac. It uses the rounding mode specified by how. (Added by C# 2.0.)

public static decimal Round(decimal v, int frac, MidpointRounding how)

Returns v rounded to the number of fractional digits specified by frac. It uses the rounding mode specified by how. (Added by C# 2.0.)

public static int Sign(double v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(float v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(decimal v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(int v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(short v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(long v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static int Sign(sbyte v)

Returns 1 if v is less than zero, 0 if v is zero, and 1 if v is greater than zero.

public static double Sin(double v)

Returns the sine of v.

public static double Sinh(double v)

Returns the hyperbolic sine of v.

public static double Sqrt(double v)

Returns the square root of v.

public static double Tan(double v)

Returns the tangent of v.

public static double Tanh(double v)

Returns the hyperbolic tangent of v.

public static double Truncate(double v)

Returns the whole number portion of v. (Added by C# 2.0.)

public static decimal Truncate(decimal v)

Returns the whole number portion of v. (Added by C# 2.0.)

Math also defines these two fields:

 public const double E public const double PI

E is the value of the natural logarithm base, commonly referred to as e. PI is the value of pi.

Here is an example that uses Sqrt( ) to help implement the Pythagorean theorem. It computes the length of the hypotenuse given the lengths of the two opposing sides of a right triangle.

 // Implement the Pythagorean Theorem. using System; class Pythagorean {   public static void Main() {     double s1;     double s2;     double hypot;     string str;     Console.WriteLine("Enter length of first side: ");     str = Console.ReadLine();     s1 = Double.Parse(str);     Console.WriteLine("Enter length of second side: ");     str = Console.ReadLine();     s2 = Double.Parse(str);     hypot = Math.Sqrt(s1*s1 + s2*s2);     Console.WriteLine("Hypotenuse is " + hypot);   } }

Here is a sample run:

 Enter length of first side: 3 Enter length of second side: 4 Hypotenuse is 5

Next is an example that uses the Pow( ) method to compute the initial investment required to achieve a desired future value given the annual rate of return and the number of years. The formula to compute the initial investment is shown here:

 InitialInvestment = FutureValue / (1 + InterestRate)Years 

Because Pow( ) requires double arguments, the interest rate and the number of years are held in double values. The future value and initial investment use the decimal type.

 /* Compute the initial investment needed to attain    a known future value given annual rate of return    and the time period in years. */ using System; class InitialInvestment {   public static void Main() {     decimal InitInvest; // initial investment     decimal FutVal;     // future value     double NumYears;    // number of years     double IntRate;     // annual rate of return as a decimal     string str;     Console.Write("Enter future value: ");     str = Console.ReadLine();     try {       FutVal = Decimal.Parse(str);     } catch(FormatException exc) {       Console.WriteLine(exc.Message);       return;     }     Console.Write("Enter interest rate (such as 0.085): ");     str = Console.ReadLine();     try {       IntRate = Double.Parse(str);     } catch(FormatException exc) {       Console.WriteLine(exc.Message);       return;     }     Console.Write("Enter number of years: ");     str = Console.ReadLine();     try {       NumYears = Double.Parse(str);     } catch(FormatException exc) {       Console.WriteLine(exc.Message);       return;     }     InitInvest = FutVal / (decimal) Math.Pow(IntRate+1.0, NumYears);     Console.WriteLine("Initial investment required: {0:C}",                       InitInvest);   } }

Here is a sample run:

 Enter future value: 10000 Enter interest rate (such as 0.085): 0.07 Enter number of years: 10 Initial investment required: $5,083.49




C# 2.0(c) The Complete Reference
C# 2.0: The Complete Reference (Complete Reference Series)
ISBN: 0072262095
EAN: 2147483647
Year: 2006
Pages: 300

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net