Math, C# style

Advertisements

Hello everybody,

Michael here, and in today’s post-my last one for 2024-I thought we’d take a quick look at math, C# style.

What do I mean by that? Well, today we’re going to be exploring how C# performs basic math operations. Seems fun, right? Well, let’s dive in!

Math Class, C# style

Like several other programming languages we’ve covered on this blog, C# also has its own way of doing math. Let’s take a look at some of the many mathematical methods of C#:

Console.WriteLine(Math.Max(3, 12));
Console.WriteLine(Math.Abs(-333.22));
Console.WriteLine(Math.Min(3, 12));
Console.WriteLine(Math.Sqrt(49));

Here are the respective outputs of these four methods:

12
333.22
3
7

In this example, I demonstrated four basic methods of the C# Math class-Max, Abs, Min and Sqrt. These methods calculate the maximum value in a numerical range, the absolute value of a number, the minimum value in a numerical range, and the square root of a number, respectively.

  • If you want to print out the results of these calculations, remember to wrap each Math method call inside a Console.WriteLine() method call.

More complex C# calculations

So now that we’ve demonstrated simple C# math methods, let’s show some more complex methods:

Console.WriteLine(Math.Pow(2, 5));
Console.WriteLine(Math.Log10(14));
Console.WriteLine(Math.Cos(84));
Console.WriteLine(Math.Round(45.2423242152332, 2));

32
1.146128035678238
-0.6800234955873388
45.24

In this example, I’m demonstrating four of the Math class’s more complex calculation capabilities-Pow, Log10, Cos, and Round-which calculate the value of a number raised to a specific power, the base-10 logarithm of a number, the cosine of an angle, and a decimal rounded to 2 places, respectively.

  • You may recognize some of these concepts from the math lessons I posted earlier in this blog’s run.

A special note about very big and very small numbers

Now, what if you wanted to use a very big or very small number in your C# math calculation? Theoretically, you could write out the number in its entirety, such as 20,000,000,000 (20 billion). However, that doesn’t seem like the most efficient way to code, does it?

If you’ve ever taken pre-algebra, you might recall a little concept known as scientific notation, which is a convenient shorthand way of writing very big and very small numbers. For instance, 20 billion would be written as 2 x 10^10 in scientific notation, which seems much more readable than 20000000000.

C# works in a similar manner. However, it doesn’t use the 10^[something] notation that you might be familiar with from math class. Rather, C# uses what’s called e-notation, which serves the same purpose as conventional scientific notation, but replaces the 10^[something] with e[something]. So in the case of 20 billion, the e-notation for that number would be 2e10.

Also, similar to scientific notation, numbers smaller than 1 would be represented with e-[something]. For instance, the number 0.0000002 would be represented as 2 x 10^-7 in scientific notation and 2e-7 in e-notation.

Console.WriteLine(Math.Pow(4e-5, 5));
Console.WriteLine(Math.Log10(16e4));
Console.WriteLine(Math.Cos(8e1));
Console.WriteLine(Math.Round(42e5+2.16, 2));

1.0240000000000004E-22
5.204119982655925
-0.11038724383904756
4200002.16

As you can see, the calculations worked the same as they did with the regular numbers. The only difference is that if a number is too big or too small to feasibly display on the output, that number will be represented with e-notation (as shown by our first example with the Math.Pow method).

Also, if you want to learn more about the C# Math class, check out Microsoft’s documentation (all documentation for C# is provided by Microsoft)-https://learn.microsoft.com/en-us/dotnet/api/system.math?view=net-9.0.

Before I go, here’s the GitHub link to my script for this lesson-https://github.com/mfletcher2021/blogcode/blob/main/MathClass.cs.

Thanks for coding along with me in 2024! It’s been another great coding year, and I hope you and your loved ones have a happy and healthy and festive and joyous holiday season! With that said, Michael’s Programming Bytes will return with new content in…

What creative coding content will I come up with in 2025? You’ll just have to follow along and see.

Happy holidays and thanks as always for coding along.

Michael