Starting next year, I'm hoping to begin a new series of posts in this column that I'm going to call the 'Back to Basics' posts. The idea will be that each post will re-visit a common .NET base class and give you a refresher in the methods and features provided by it.
I'm going to start by visiting the 'Math Class', which has been a staple part of the .NET framework since day one, if you like the format, or even if you don't, let me know what worked and didn't work in the comments below. Any comments I receive will help shape future posts on .NET basics.
On with the Show
The .NET Math BCL contains routines to help you perform trigonometric functions and perform reliable maths operations on large numbers. There are methods to help you round numbers in specific ways, and examine the negativity of a given number.
The main MSDN page as of the date of this post can be found here, and is currently set for .NET 4.5.
There are two constants available in the class:
- PI: Returns the value represented by the Greek π (Pi) symbol
- E: Returns the natural logarithmic base value
The rest of the class consists of methods designed to provide various bits of information, or number operations on a given parameter or parameters.
The Abs method is used to return the absolute version of the parameter passed to it. In layman's terms, the Abs method always returns the given number's positive version. If you give the operand as '-9', for example, Abs will return '9'.
Abs is normally used to ensure the number passed to it is always in the positive range, irrespective of the sign of the number passed to it. Abs has several overrides and can take Decimal, Double, Integer, and byte and will return a positive rendition of the same type.
Acos, Asin, Atan(operand)
Given a cosine, sine, or tangent value, these three methods will return the angle representing the given value in radians. The supplied value must be in the range -1 to 1.
The output value can be converted into degrees (0 to 360) by 180/Math.PI.
BigMul is used to give the full value of the multiplication of two 32-bit integer numbers. If you're working on a 64-bit machine, you might wonder why this is important. However, even on a 64-bit machine, if you try to multiply 32-bit integers that are close to the maximum value for a 32-bit integer, it's possible to get rounding errors.
BigMul ensures that no conversion errors result from the multiplication and that the result is a valid useable integer.
Ceiling is used to 'clamp' floating point or decimal values to a nearest whole integer. In the case of Ceiling, this integer is the smallest whole number that is greater than or equal to the given operand.
In other languages, this method is often called a "Round Up" method because it will "Round Up" to the next closest whole number.
Floor is the exact opposite of ceiling, rounding down instead of rounding up. Floor will return the nearest whole number that is lower than or equal to the supplied operand.
Cos, Sin, Tan(Operand)
These three methods accept an angle in radians and return the related Cosine, Sine, or Tangent value.
Being the opposite of the Axxx versions, these methods should, if given the value produced by the Axxx version, return the value passed into the Axxx method.
Max, Min(Operand, Operand)
The max and min methods take two operands, and return either the maximum or the minimum of the operands provided. For example, 'Max(20,10)' will return 20 because 20 is the larger of the two operands.
Round is used to round a floating point number to its nearest whole number representation. If the second operand (Optional) is provided, the first operand is rounded to the given number of decimal places.
Without the second operand, round will either produce the same result as 'Floor' or 'Ceiling', depending how close the operand is to the halfway point between the upper and lower whole numbers producible from the first operand.
Sign will return either -1 or 1, signifying whether or not the provided operand was a negative or a positive number.
I've listed only the most common methods used in the Math class in the preceding list. There are, however, many other methods in the class.
Truncate, for example, is used to truncate a floating point number to a given number of decimal places but without rounding the number as round does.
You can calculate tangents and square roots as well as control things like rounding with very precise control.
The math class is an integral part of many common triganomic and algebraic functions, such as computing dot products and Cartesian values, especially if you're doing a large amount of work with 3D graphics.
I encourage you to try a small console mode program, and just try the various methods available. The methods in the math class are all static, so no 'new' operator is required to use them.
Got a problem with .NET you can't solve, or are you just looking for some inspiration on how to solve common problems? Track me down on Twitter as @shawty_ds or leave a comment below and I'll look at writing a future post on the subject.