Scribunto: An Introduction/Numbers

From Wikibooks, open books for an open world
Jump to navigation Jump to search

In the last chapter we learned how to use the debug console, and how to display text from a module on a normal wiki page. The module we wrote to display the text used quite a few new concepts, and you may be feeling a little confused after reading it. However, please don't panic just yet; in part two of this book we will learn all the concepts necessary to understand that module, and more. Until we have covered all the concepts used in the module that we wrote, we will concentrate on using the debug console, which allows us to write simpler programs.

So, without further ado, let's get started on learning some basic Lua concepts. First, we will cover something that is likely very familiar to you: the use of numbers.

Inputting numbers[edit]

The syntax for inputting numbers in Lua is very simple. In fact, there is no special syntax; all you do is type the number in. Try the following example in the debug console:

Crystal128-terminal.svg Code:

= 2

Crystal Clear app kscreensaver.svg Output:

2

The debug console should print the same numbers as the ones you typed. Now, let's see what happens if we add some zeroes:

Crystal128-terminal.svg Code:

= 002

Crystal Clear app kscreensaver.svg Output:

2

Crystal128-terminal.svg Code:

= 2.0

Crystal Clear app kscreensaver.svg Output:

2

Even though we input 002 and 2.0, the output is still 2. This is because Lua interprets the value of the number; it doesn't remember the exact characters that you type.

To insert a negative number, just prefix it with a hyphen.

Crystal128-terminal.svg Code:

= -5

Crystal Clear app kscreensaver.svg Output:

-5

Calculation[edit]

Now, let's do some calculations. Lua can do any calculation that your pocket calculator can, and quite a few that it can't. To make calculations, Lua uses arithmetic operators. This is just a complicated way of saying "symbols that you do arithmetic with". For example, addition is performed with the + operator.

Crystal128-terminal.svg Code:

= 2 + 2

Crystal Clear app kscreensaver.svg Output:

4

Similarly, subtraction, multiplication and division are performed with the -, * and / operators, respectively.

Crystal128-terminal.svg Code:

= 100 - 50

Crystal Clear app kscreensaver.svg Output:

50

Crystal128-terminal.svg Code:

= 5 * 4

Crystal Clear app kscreensaver.svg Output:

20

Crystal128-terminal.svg Code:

= 10 / 3

Crystal Clear app kscreensaver.svg Output:

3.3333333333333

The result of 10 / 3 may surprise some readers, as in some other programming languages this same code produces "3", and to get a result of "3.3333333333333" you need to write something like 10.0 / 3. However, in Lua there is no distinction between 10 / 3 and 10.0 / 3.

It is also possible to find the remainder after division by using the modulo (or mod) operator, %.

Crystal128-terminal.svg Code:

= 17 % 5

Crystal Clear app kscreensaver.svg Output:

2

This example means, "Divide 17 by 5 and output what is left over." 17 doesn't divide by 5 exactly; the next highest number that divides exactly by 5 is 15. The difference between 17 and 15 is 2, so this is what Lua outputs.

The modulo operator has many applications. For example, you can find whether a number is odd or even by using % 2.

Crystal128-terminal.svg Code:

= 123 % 2

Crystal Clear app kscreensaver.svg Output:

1

Crystal128-terminal.svg Code:

= 124 % 2

Crystal Clear app kscreensaver.svg Output:

0

If you use % 2 with an odd number, it will give a result of 1, and if you use it with an even number, it will give a result of 0.

The final operator that we will introduce here is ^, which is used to make calculations with exponents (or powers). For example, 2^3 means "2 to the power 3", or 2 * 2 * 2.

Crystal128-terminal.svg Code:

= 2 ^ 3

Crystal Clear app kscreensaver.svg Output:

8