{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## C# Math: Operators and Functions for Fun and Profit\n", "\n", "Now that we have some basic types and can create variables, it would sure be nice to have them interact with each other.  [Operators](https://docs.microsoft.com/dotnet/csharp/language-reference/operators/) can be used to interact with our variables.\n", "\n", "Let's start by declaring two variables, `playerOneHand` and `playerTwoHand` and interact with them using different operators.  Try changing some of the values and tinkering with the operators in the following code snippets." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var playerOneHand = 5; // 5 cards in player 1's hand\n", "var playerTwoHand = 4;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Basic arithmetic operators `+ - * /` are available:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var totalCards = playerOneHand + playerTwoHand;\n", "var whoHasMoreCards = playerOneHand - playerTwoHand;\n", "var takeAnotherCard = playerTwoHand + 1;\n", "var cardsPerSuit = 52 / 4;\n", "var lotsOfCards = 52 * 10;" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The remainder or modulo operator `%` is also available when working with floating point numbers:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var angleCardIsTurned = 45.2m;\n", "var fractionalPart = angleCardIsTurned % 1; // returns 0.2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Incrementing values\n", "\n", "Numeric values can be incremented by using the `++` operator. This will increment the value of the variable after the operation concludes. It's typically used like this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "// Draw a card from the deck\n", "var cardsInHand = 3;\n", "cardsInHand++;\n", "display(cardsInHand);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Additionally, the `++` operator can also be pre-pended to the variable they will effect but with a slightly different behavior. In this scenario, the value is incremented BEFORE the operation concludes. Let's use the `display()` method in Polyglot notebooks to show these values:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var cardsInHand = 3;\n", "\n", "display(cardsInHand++); // Display the value then increment to 4, output is 3\n", "display(cardsInHand); // output is 4\n", "display(++cardsInHand); // output is 5\n", "display(cardsInHand); // output is 5" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There is also a decrementing operator available, `--` that can be used in the same way to decrement a value by decorating a variable with `--` as a prefix or suffix." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var cardsInHand = 5;\n", "\n", "// play a card\n", "cardsInHand--;\n", "display(cardsInHand);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can use the equals symbol `=` with the four standard arithmetic operators to calculate with the current value and assign a value back to a variable. These are called the **compound assignment** operators. " ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var playerOneHand = 5;\n", "var playerTwoHand = 4;\n", "\n", "// Take another card player two\n", "playerTwoHand += 1;\n", "display(playerTwoHand); // output is 5\n", "\n", "// Play a card player one\n", "playerOneHand -=1;\n", "display(playerOneHand); // output is 4" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Equality and Inequality testing\n", "C# makes the inequality operators available as well, and a test for equality using `==`. This expression resolves to a boolean `true` or `false` value." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "display(playerOneHand < playerTwoHand);\n", "\n", "display(playerTwoHand > playerOneHand);\n", "\n", "display(playerOneHand >= playerTwoHand);\n", "\n", "display(playerOneHand <= playerTwoHand);\n", "\n", "display(playerOneHand == playerTwoHand);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## The Standard .NET Math library\n", "There are a ton of [math functions available for you on the `System.Math` object](https://learn.microsoft.com/en-us/dotnet/api/system.math?view=net-8.0) available in all C# programs. Between trigonometric functions and rounding simple statistic functions and even logarithmic, exponents and square roots you'll have a great collection of functions available to you once you start working with `System.Math`\n", "\n", "Try some of these operations on our two player's hands of cards:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" }, "polyglot_notebook": { "kernelName": "csharp" } }, "outputs": [], "source": [ "var smallerHand = Math.Min(playerOneHand, playerTwoHand);\n", "\n", "var largerHand = Math.Max(playerOneHand, playerTwoHand);\n", "\n", "var angleCardTurned = 45.25m;\n", "var estimatedAngle = Math.Round(angleCardTurned, 1);\n", "\n", "var closeEnoughAngle = Math.Truncate(angleCardTurned);" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "name": "polyglot-notebook" }, "polyglot_notebook": { "kernelInfo": { "defaultKernelName": "csharp", "items": [ { "aliases": [], "languageName": "csharp", "name": "csharp" } ] } } }, "nbformat": 4, "nbformat_minor": 2 }