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

C# Variables, Comments & Data Types

Advertisements

Hello everyone,

Michael here, and in today’s post, we’ll explore C# variables, comments (notes that developers leave on code to explain it to other developers), and data types.

For many of the other programming tools we’ve covered on this blog’s journey like Python and Java, C# has its own ways of dealing with variables, comments, and data types.

In Python, here are some common variables, comments, and data types:

Variables

a = 3
b = 2

print(a+b)

5

Comments (single hashtag)

# This will print out the sum of 3+2

a = 3
b = 2

print(a+b)

5

Data types

complex
int 
str
bool 
dict

Likewise, in Java, here are some common variables, comments, and data types

Variables

String name = "Michael";
int year = 2024;

Comments

// This is a code comment
System.out.println("Hello world")

Data types

int
float
String
char

The variables, comments, and data types (C# version)

Now that we’ve reviewed variables, comments, and data types in Java and Python, let’s explore how these concepts work in C#.

Take this simple C# script, filled with variables, comments and data types:

// This code is writing a simple sentence with both string and int variables
int year = 2024;
string name = "Michael";
string month = "December";

Console.WriteLine(name + " is writing this post in " + month + " " + year);

Let’s examine this script line-by-line:

  • The top line with the double backslashes // signifies a comment being added to the script. Like Java and Python, C# comments are single-line deals.
  • int year = 2024; indicates that the int value in this script will be 2024
  • The two string variables in this script indicate that the two string values I will use in this script are Michael and December, respectively.
  • The Console.WriteLine() section of the script will print out my output to a command line-in this case, the string I want to output is Michael is writing this post in December 2024.
  • Another thing worth noting-to concatenate values in C#, use the + (plus) sign to do so. As you can see, I used the plus sign several times to concatenate my string values with other text in order to generate the following line of code: name + " is writing this post in " + month + " " + year.

Now that we’ve explored some basic examples of variables, data types, and comments in C#, what are some other data types in C#:

  • long-think of this as the 8-bit version of the int data type in C# (int is a 4-bit data type) that stores a broader range of integers
  • float-a 4-bit decimal type that stores values up to 6-7 decimal places
  • double-an 8-bit decimal type that stores values up to 15 decimal places
  • bool-the C# boolean type which stores true or false values
  • char-the C# character type which stores single characters
  • string-the C# string type which stores string values

Variables, Data Types, and Comments in C#: Things to Note

What are some important things to note about variables, data types, and comments in C# that are either similar to or perhaps quite different from variables, data types, and comments in other languages we’ve covered throughout this blog’s run?

  • In C#, comments are always indicated with the // (double backslash) symbol.
  • Just like in Java, you must specify a variable’s data type in the script (like I did with int year = 2024; for example).
  • Unlike Python, C# has no data types to handle complex/imaginary numbers (like 3+5i for example).
  • The boolean truth table rules that apply to languages like Java and Python also apply to C# (e.g. individual statements A & B must be true for a boolean statement to be true).

Also, here’s my script from this lesson on my GitHub-https://github.com/mfletcher2021/blogcode/blob/main/IntroToCSharp.cs.

Thanks for reading,

Michael