.. index:: arithmetic .. _arithmetic: 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. 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: .. code-block:: none 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. Addition, Subtraction, and Multiplication ------------------------------------------- .. index:: single: operator; + with numbers single: +; with numbers Inside csharprepl, enter what comes after the prompt (REPL prints the results directly without having to use the ``Console.WriteLine()`` method): .. code-block:: none > 2 + 3 5 > Console.WriteLine(1+1) 2 .. index:: operator; - single: -; subtraction Now let us do some arithmetic operations: .. code-block:: none > 10 - 3 7 .. index:: operator; * single: * multiplication In the Math class, you could enter something like 4(10) for multiplication, but in C#, you need to use the multiplication operator ``*``: .. code-block:: none > 4(10); ┌─────────CompilationErrorException─────────┐ │ (1,1): error CS0149: Method name expected │ └───────────────────────────────────────────┘ > 4 * 10 40 .. Unfortunately the error messages are not always easy to follow: .. it is hard to guess the .. intention of the user making a mistake. .. index:: single: ( ); grouping grouping ( ) single: -; negation 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: .. code-block:: none > 2 + 3 * 4 14 > -(2+3)*4 -20 .. index:: single: remainder % single: % remainder single: operator; /, % division single: / ; division single: . ; double literal double int type; int type; double .. _Division-and-Remainders: Division -------------------------------- Division can be a little tricky in C#. For example: .. code-block:: none > 5.0/2.0; 2.5 > 14.0/4.0; 3.5 But C# will implicitly turn the following expression to an ``int`` type: .. code-block:: none > 14/4 3 Adding a decimal point would inform C# that you are using ``double`` instead of ``int``: .. code-block:: none > 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: .. code-block:: none > 1.0/3 0.333333333333333 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``: .. code-block:: none > 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 ``-``. 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: .. code-block:: none > 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.