MorkaLork Development

Interesting stuff I've picked up over the years...

Math

2009-04-16 17:54:06 | 215 views | math mathclass static operators

The Basics



Math is a class with math routines to handle mathematics that goes beyond the normal +-/* operators. The mathclass is a sealed class and can't be instantiated and the classes and datamembers are static so you can't create a math object. What you do is that you use the math methods that comes with math.

These are the math methods:

MethodReturns
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.


Two constants also comes with the math class, PI and E.PI.
PI returns the value of pi; 3.14159265358979323846.
E.PI returns the value of the logarithmic base; 2.7182818284590452354.



Advanced



Here are some examples of how to use the math class and it's methods:



Console.WriteLine("1. The square root of {0} is {1}.", 9, Math.Sqrt(9));
Console.WriteLine("2. The ceiling of {0} is {1}.", 9.4, Math.Ceiling(9.4));
Console.WriteLine("3. {0} raised to {1} is {2}.", 9.5, 3, Math.Pow(9.5,3));
Console.WriteLine("4. {0} rounded is {1}.", 10.7, Math.Round(10.7));

Console.Read();


This will look like this:

imagehttp://morkalork.com/images/screenshots/math1.jpg





Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.