Numbers
If you have programmed in a different programming language before, you will probably have some memory of the first program you ever wrote. It might have looked like this:
Or like this:
Regardless of which language you learned, you probably glossed over this first program very quickly, only to get stuck as you tried to create more complex programs.
SonarLang takes a different, better approach. By building your knowledge of syntax (i.e the "grammar" of SonarLang) from the simplest possible program up, you'll learn to create more complex programs intuitively, in no time.
In order to follow along with the examples in this tutorial, you'll need to enter these commands in your SonarLang REPL program.
The simplest program you can write in Sonar is just one digit:
Entering this just after the arrows in your REPL will just output "1". It might not look like much, but this is a solid foundation for your future programs.
Addition and Subtraction
In Sonar, numbers behave exactly like they do in the real world. They can be used to count objects, and they can be added and subtracted from each other.
Decimal Numbers
In the real world, some numbers can be expressed as fractions using decimal notation.
Decimal notation refers to the use of a "." to separate a number into two parts: a "whole number" (or "integer) and a "decimal" (or fraction).
Examples of decimal numbers are: 1.1, 2.3, and 3.233.
SonarLang allows you to use decimal numbers the same way you would use them in the real world:
Decimal numbers are called "floats" or "floating-point numbers".
Multiplication and Division
Numbers in Sonar can be multiplied or divided by other numbers.
To multiply numbers in Sonar, use the asterisk (*) symbol.
To divide, use the slash (/) symbol.
Multiplying or dividing an integer (or whole number) by a float (or decimal number), or a float by an integer yields a float.
An easy way to remember this is:
If the numbers are the same type, the same type is yielded
If the numbers are different types, "float" is yielded
This also applies to addition/subtraction operations.
Positive/negative Numbers
In the real world, numbers can be either positive or negative.
A positive number is a number (whether integer or float) that is larger than 0.
All the numbers we have added, subtracted, multiplied and divided before now have been positive numbers because they have all been larger than 0, however sometimes it is important to have numbers that are smaller than 0 (negative numbers). SonarLang supports these too:
Addition, subtraction, multiplication and division is also possible with negative numbers.
Integers and Floats in Sonar
In Sonar, unlike many other languages, integers and floats can be used together in arithmetic operations (e.g addition and subtraction).
Multiplication and division works, as well:
Dividing zero by other numbers
Dividing zero by any number (except zero) returns zero.
Dividing by zero
Dividing by zero is an invalid operation in Sonar.
Dividing any number by zero returns a ZeroDivisionError.
Last updated