3.2. Numbers#
In this section we’ll:
Teach you about some numeric data types
Explain what operators are
Help you write programs using variables with numeric data types
You will write code for this section in a different directory to the one used for the Hello World program. Follow the following instructions below to do so:
Create a new directory called
Numbers
under yourValaProjects
directory.Under the
Numbers
directory you have just created, create a new file calledmain.vala
.
3.2.1. Introduction to Numbers#
In the last section, we showed an example of a variable:
string my_name = "Sara";
In your main.vala
file, create a main method with, an int
variable assignment, in the main method’s body:
public static void main () {
int num = 42;
}
The num
variable of you’ve created in the line above stores int values and you have set the value of it to 42
.
You can print the value of num
by adding using the print
method. Add this line below where you wrote the num
variable’s delcaration:
public static void main () {
int num = 42;
print ("Value of num: %d\n", num);
}
Now, compile your code in main.vala
and run the generated program. You’ll see the following output:
Value of num: 42
Note
%d
is a format specifier. We use this specify where we want an integer value to be displayed in the printed message. num
after the ,
is the variable that will be substituted for %d
.
We’ll cover format specifiers in more detail, in later sections.
3.2.2. Different numeric data types#
Warning
It’s pretty hard to find documentation on exactly how these GLib data types behave. As of now, the descriptions of each data type may be inaccurate.
Vala has many numeric data types such as:
char
- Stores 8 bit integer numbers.uchar
- Stores unsinged 8 bit numbersshort
- Stores 16-bit integer numbersushort
- Stores unsigned 16 bit numbersint
- Stores integer numbers (platform dependant behaviour)uint
- Stores unsigned integer numbers (platform dependant behaviour)long
- Stores “big” integer numbers (platform dependent behaviour).ulong
- Stores unsigned “big” integer numbers (platform dependent behaviour).size_t
- Stores at least 16 bit integer numbersssize_t
- Stores at least 16 bit unsigned integer numbersint8
- Stores 8 bit integer numbersuint8
- Stores unsinged 8 bit integer numbersint16
- Stores 16 bit integer numbersuint16
- Stores unsigned 16 bit integer numbersint32
- Stores 32 bit integer numbersuint32
- Stores unsinged 32 bit integer numbersint64
- Stores 64 bit integer numbersuint64
- Stores unsigned 64 bit integer numbersunichar
- Stores unsigned 32 bit integer numbersfloat
- Stores 32-bit floating-point numbersdouble
- Stores 64-bit floating-point numbers
Note
Floating-point numbers are numbers with decimal points in them like: 2.5
.
Note
The unsigned types above only store postive numbers, allowing them to store bigger numbers values that their signed variants can’t. Unsinged types also use the same amount of storage space as their signed variants too.
Tip
Some of the data types behave similarly to C data types of the same name.
3.2.3. Artithmetic Operations#
Rather than teaching you how each of these data types work in detail, we’ll give you pre-written code that shows you how a few of them behave.
Some of these data types will be covered in the rest of the tutorial.
Replace all the code in main.vala
with this code below:
public static void main () {
// Addition:
int sum = 5 + 5; // sum = 10
// Subtraction: difference = 5
int difference = 12-7; // difference = 5
// Multiplication: product = 4
int product = 2 * 2; // product = 4
// Division:
float quotient = 64.2f / 2f; // quotient = 32.1
double second_quotient = 99.9 / 11.1; // quotient = 9.0
// Integer division results in the decimal part of the quotient not being
// included:
int floored_quotient = 16 / 5; // floored_quotient = 3
// Modulus
int remainder = 1 % 2; // remainder = 1
int no_remainder = 2 % 2; // no_reminader = 0
// Reusing variables
int changing_num = 1;
changing_num = changing_num + 1;
changing_num = changing_num - sum;
changing_num = changing_num * difference;
changing_num = changing_num / product;
print ("Changing number: %d\n", changing_num);
}
Can you guess the value of changing_num
that gets printed?
Now run and compile the code, you should see the value -10
gets printed.
Note
You might see some warnings when compiling the code. Don’t worry about these, this is because there were variables we didn’t use.
The code will run fine.
Feel free to experiment with arithmetic operations by creating your own math expressions.