Perl - Math

By lingy | Lingy | 5 Sep 2022


Banner

One of the most important things in any programming language is its support for mathematical operations. With Perl it's no different: the language supports most of the mathematical operations you'll need in your academic and professional life, and I'm going to comment on the most basic ones here, so you'll be able to go on your own.

Perl supports several numeric data types. Below are some usage examples:

10 # integer
-10 # negative integer
123.45 # floating point (float)
6.2E2 # scientific notation
0xffff # hexadecimal (starting with "0x")
0377 # octal (starts with "0")
0b100111101 # binary (starts with "0b")
5_232_999_435 # large numbers arranged with underscores

Internally, however, it works as if all numbers are floating point. That is, 1 becomes 1.02 becomes 2.0, and so on.

Perl also supports different math functions such as addition, subtraction, multiplication, division, exponentiation and remainder of division. Let's see what each of them does.

Addition, as the name implies, is about adding two numbers to form a third. The mathematical expression 23 + 32, for example, returns 55. We also have to keep in mind that negative numbers can also be added, but the effect of this addition will be the same as a subtraction of a positive integer, that is, the expression 32 + (-23) will result in 9. See the examples below:

print 23 + 32, "\n"; # 55
print 32 + (-23), "\n"; # 9

Now the subtraction. Subtraction is about doing the inverse operation of division, that is, if 23 + 32 = 55, then 55 - 32 = 23. The same rule of negative number prevails, that is, if the number is negative, the function will be changed, so 9 - (-23) = 32. Check out the examples below:

print 55 - 32, "\n"; # 23
print 9 - (-23), "\n"; # 32

Multiplication would be the same as a process of consecutive additions, that is, 2 * 3 is the same thing as 2 + 2 + 2, or 3 + 3. In this case, having a negative number in the operation will only make the final result negative (i.e. 2 * -2 = -4), while having two negative numbers will make the result positive (i.e. -2 * -2 = 4). Look at the examples below:

print 2 * 3, "\n"; # 6
print 2 * -3, "\n"; # -6
print -2 * -3, "\n"; # 6

The division is something a little more complex. Dividing one number by another would be finding which number that multiplied by the divisor would give the number that was divided (yes, the concept is confusing, but I'll give you examples). Think of the following: 4 / 2 = 2 because 2 * 2 = 48 / 2 = 4 because 4 * 2 = 8, and so on.

However, in this case, there is a small difference. What if we try to do 2 / 4? The result in this case will be 0.5, a floating point number, and 0.5 * 4 = 2. Look at the examples:

print 4 / 2, "\n"; # 2
print 2 / 4, "\n"; # 0.5
print 8 / 2, "\n"; # 4

But what about the rest of the division? To know this, we will have to understand the concept of integer division. In computing mathematics, there are two types of division: conventional, floating-point, and integer, which deals only with integers. That is, while in a conventional division, 2 / 4 = 0.5, in an integer division it would be 2 / 4 = 0. Okay, but what about the rest of the division? Simple, as the operation of taking the remainder of the division in Perl is defined by the character %, the final operation will look like this: 2 % 4 = 2.

This type of operation is used, for example, to check whether a number is even or odd: if the remainder of the division of a number by 2 is 0, the number is even, otherwise, it is odd. See some examples:

print 4 % 2, "\n"; # 0
print 4 % 3, "\n"; # 1
print 2 % 5, "\n"; # 2
print 3 % 2, "\n"; # 1
print 3 % 5, "\n"; # 3

print 4 % 2 == 0 ? "true" : "false", "\n"; # true
print 3 % 2 == 0 ? "true" : "false", "\n"; # false
print 2 % 2 == 0 ? "true" : "false", "\n"; # true
print 0 % 2 == 0 ? "true" : "false", "\n"; # true
print 50 % 2 == 0 ? "true" : "false", "\n"; # true

Potentiation (identified by ** in Perl) is about multiplying a number by itself x times, i.e. 2 ** 2 is the same thing as 2 * 2, and 2 ** 3 is the same as 2 * 2 * 2. See some examples below:

print 2 ** 2, "\n"; # 4
print 5 ** 2, "\n"; # 25
print 2 ** 3, "\n"; # 8
print 2.5 ** 2, "\n"; # 6.25
print 0.5 ** 5, "\n"; # 0.03125

Now that we have examples of numbers, how about trying some math operations? Let's start with the simplest. Try running the following formulas, and see your results:

  • 4 ** 3
  • 5 + 4 + 3 + 2 -1
  • 2 / 4
  • 3 * 25
  • 5 % 3

Now let's learn a little about precedence of operations. What is the result of 1 + 2 * 3? For some it might be 9, because they will first calculate 1 + 2 (which will give 3), and then 3 * 3, which will give 9, while for others the result will be 7, because they will first calculate 2 * 3, which will give 6, and then 1 + 6, which will give 7. But what is the correct way? Let's try using Perl. Run the following command in your shell:

perl -e 'print 1 + 2 * 3, "\n";'

The result was 7, and this is because of operator precedence. Precedence in Perl (and most languages) follows the following order of precedence:

dbca4c7f9ecf62fce6bf93a5516a686bd942f3be9fd17b04d67ee3ff0b3cb0af.png

Traders you don't know, don't worry: you'll learn in the next tutorials.

And then? Was there any doubt? Do you have any criticism, praise or comment? Leave it below, your opinion is very important to me 😄☺️

References

How do you rate this article?

5



Lingy
Lingy

My personal blog about technology

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.