The Math Class

Every self-respecting foundational API has facilities to deal with math. The BCL is no different in this respect with the System.Math static class that provides a plethora of handy mathematical operations on various numeric types. We don’t cover all of them in detail, so let’s highlight the most important aspects.


Anyone with any background in math knows two of the most important constants used in various applications. Those are e (2.71828...) and pi (3.14269...). Both of them are available as constants on the Math class, with double precision:

Console.WriteLine(Math.Pow(Math.E, Math.Log(5)));Console.WriteLine(Math.PI * r * r);

In the first example, we’re taking the natural logarithm ( ...

Get C# 5.0 Unleashed now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.