### Working with Math Routines in C#

**Environment:** C#, .NET

Basic math operators—such as plus, minus, and modulus—can get you only so far. It is only a matter of time before you find that you need more robust math routines. C# has access to a set of math routines within the base classes. These are available from within the `System.Math`

namespace. Table 1 presents a number of the math methods available.

**Table 1**: *Math Routines in the Math Class*

**Method**

Returns

`Abs`

Returns the absolute value of a number.

`Ceiling`

Returns a value that is the smallest whole number greater than or equal to a given number.

`Exp`

Returns E raised to a given power. This is the inverse of Log.

`Floor`

Returns a value that is the largest whole number that is less than or equal to the given number.

`IEEERemainder`

Returns the result of a division of two specified numbers. (This division operation conforms to the remainder operation stated within Section 5.1 of ANSI/IEEE Std. 754-1985; IEEE Standard for Binary Floating-Point Arithmetic; Institute of Electrical and Electronics Engineers, Inc; 1985.)

`Log`

Returns a value that is the logarithmic value of the given number.

`Log10`

Returns a value that is the base 10 logarithm of a given value.

`Max`

Returns the larger of two values.

`Min`

Returns the smaller of two values.

`Pow`

Returns the value of a given value raised to a given power.

`Round`

Returns a rounded value for a number. You can specify the precision of the rounded number. The number .5 would be rounded down.

`Sign`

Returns a value indicating the sign of a value. 1 is returned for a negative number, 0 is returned for zero, and 1 is returned for a positive number.

`Sqrt`

Returns the square root for a given value.

`Acos`

Returns the value of an angle whose cosine is equal to a given number.

`Asin`

Returns the value of an angle whose sine is equal to a given number.

`Atan`

Returns the value of an angle whose tangent is equal to a given number.

`Atan2`

Returns the value of an angle whose tangent is equal to the quotient of two given numbers.

`Cos`

Returns a value that is the cosine of a given angle.

`Cosh`

Returns a value that is the hyperbolic cosine for a given angle.

`Sin`

Returns the sine for a given angle.

`Sinh`

Returns the hyperbolic sine for a given angle.

`Tan`

Returns the tangent of a specified angle.

`Tanh`

Returns the hyperbolic tangent of a given angle.

**Listing 1**: *MathApp.cs — Using Some of the Math Routines*

1: // MathApp.cs - Using a Math routine

2: //-----------------------------------------------

3: using System;

4:

5: class MathApp

6: {

7: public static void Main()

8: {

9: int val2;

10: char disp;

11:

12: for (double ctr = 0.0; ctr <= 10; ctr += .2)

13: {

14: val2 = (int) Math.Round( ( 10 * Math.Sin(ctr))) ;

15: for( int ctr2 = -10; ctr2 <= 10; ctr2++ )

16: {

17: if (ctr2 == val2)

18: disp = 'X';

19: else

20: disp = ' ';

21:

22: Console.Write("{0}", disp);

23: }

24: Console.WriteLine(" ");

25: }

26: }

27: }

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

## No comments:

Post a Comment