# .NET Back to Basics: The Math Class

### WEBINAR:On-Demand

Application Security Testing: An Integral Part of DevOps

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.

### Abs(operand)

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(operand1, operand2)

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(Operand)

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(Operand)

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(operand, operand)

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

#### Peter Shaw

As an early adopter of IT back in the late 1970s to early 1980s, I started out with a humble little 1KB Sinclair ZX81 home computer. Within a very short space of time, this small 1KB machine became a 16KB Tandy TRS-80, followed by an Acorn Electron and, eventually, after going through many other different machines, a 4MB, ARM-powered Acorn A5000. After leaving school and getting involved with DOS-based PCs, I went on to train in many different disciplines in the computer networking and communications industries. After returning to university in the mid-1990s and gaining a Bachelor of Science in Computing for Industry, I now run my own consulting business in the northeast of England called Digital Solutions Computer Software, Ltd. I advise clients at both a hardware and software level in many different IT disciplines, covering a wide range of domain-specific knowledge—from mobile communications and networks right through to geographic information systems and banking and finance.

## Related Articles

• #### RE: great idea for the post series

Posted by Peter Shaw on 01/30/2016 03:24am

Hi Tania, I'm glad your finding them useful and informative, I'll take your comments into account. This year is largely going to be experimental with posts like this, and the only way I can add things is if I know what you folks are looking for.

• #### RE: Programmer Analyst

Posted by Peter Shaw on 01/30/2016 03:22am

Thanks for the thanks mark :-)

• #### Programmer Analyst

Posted by Mark Yzermans on 01/11/2016 11:30am

Thanks for the refresher.

• #### great idea for post series

Posted by Tania on 01/05/2016 06:39am

Hi, I have been reading your posts here at Code Guru for a while now, and I enjoy them, and often learn something new-Thanks! I love this idea of a series of posts on .Net base classes. I would only suggest that you pick a few examples from the class, and show some code-even if trivial use cases. Thanks again, and have a great day!

• You must have javascript enabled in order to post comments.

## Top White Papers and Webcasts

• As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
• The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
• Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

## Most Popular Programming Stories

• There have been no articles posted today.