2.4. Arithmetic

Arithmetic operations are usually included in learning a programming language because the constructs are similar to computation and therefore easier for the learners to associate. The C# Math class has many methods that allows you to perform arithmetic tasks on numbers.

2.4.1. The C# REPL (csharprepl)

We want to use start by using csharprepl so that we can run some quick-and-dirty C# code for practice and testing purposes. Open a terminal and enter the command csharprepl, You should see:

tychen@mac:~/workspace/tests$ csharprepl
Welcome to the C# REPL (Read Eval Print Loop)!
Type C# expressions and statements at the prompt and press Enter to evaluate them.
Type help to learn more, exit to quit, and clear to clear your terminal.

>

The > prompt tells you that the C# interpreter has started and is awaiting input. This allows you to test C# code interactively without having to create a project, modify it, save it, and run dotnet run to test it.

When you are done with csharprepl, you can enter exit to quit the shell.

The repl part in csharprepl is an acronym for the Read-Evaluate-Print Loop (REPL). A REPL is a language shell provides simple interactive computer programming environment that runs code piecewise. It evaluates expressions immediately and prints the result on a line without a prompt. The REPL can evaluate arbitrary C# expressions. It is very handy for testing as you get used to C# syntax.

2.4.2. Addition, Subtraction, and Multiplication

Inside csharprepl, enter what comes after the prompt (REPL prints the results directly without having to use the Console.WriteLine() method):

> 2 + 3
5
> Console.WriteLine(1+1)
2

Now let us do some arithmetic operations:

> 10 - 3
7

In the Math class, you could enter something like 4(10) for multiplication, but in C#, you need to use the multiplication operator *:

> 4(10);
  ┌─────────CompilationErrorException─────────┐
  │ (1,1): error CS0149: Method name expected │
  └───────────────────────────────────────────┘
> 4 * 10
40

C# uses the normal precedence of arithmetic operations: Multiplications, divisions, and negations are done before addition and subtraction, unless there are parentheses forcing the order:

> 2 + 3 * 4
14
> -(2+3)*4
-20

2.4.3. Division

Division can be a little tricky in C#. For example:

> 5.0/2.0;
2.5
> 14.0/4.0;
3.5

But C# will implicitly turn the following expression to an int type:

> 14/4
3

Adding a decimal point would inform C# that you are using double instead of int:

> 14.0/4.0
3.5
> 14.0 / 4      // one of the operands is double type
3.5
> 6.0/3.0       // when the remainder is 0, the quotient is an int.
2
>

Note that C# stores values with only a limited precision, so the results of mathematical operations are only approximate in general. The following is an example that shows the double type has a precision of ~15-17 digits:

> 1.0/3
0.333333333333333

2.4.4. Remainders

Remainder is used to check if a number is divisible by another number. The remainder operator % computes the remainder after dividing its left-hand operand by its right-hand operand.

Try in the csharprepl:

> 14 % 7
0
> 14 % 4
2
> int x = 0;
> x = 7 % 5;  // x now contains 2
2
> x = 9 % 5;  // x now contains 4
4
> x = 5 % 5;  // x now contains 0
0
> x = 4 % 5;  // x now contains 4
4
> x = -4 % 5; // x now contains -4
-4
> x = 4 % -5; // x now contains 4
4

Note

The precedence of % is the same as / and *, and hence higher than addition and subtraction, + and -.

2.4.5. Exercise

Unix time is a date and time representation widely used in computing. It measures time by the number of non-leap seconds that have elapsed since 00:00:00 UTC on 1st January 1970, the Unix epoch.

In csharprepl, we can find the current time and it’s timestamp:

> DateTime.Now
8/23/2024 1:43:33 AM
> var timeStamp =  new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds();

> timeStamp
1724395921
>

The timestamp above is the number of seconds since the Unix epoch. Use the remainder operator to figure out how many minutes, hours, and days has it been since 00:00:00 UTC on 1st January 1970.