Numbers and Mathematical Operations in Perl

In this tutorial from our Progressive Perl tutorial, we will learn how to handle numbers and mathematical operations.

Perl numeric data types

Perl supports a wide range of numeric values, such as:
2112                     # integer
-2112                    # negative integer
12345.67              # float or decimal
6.02E23               # in scientific notation
0xffff                   # hexadecimal always starts with 0x
0377                    # octal always starts with 0
0b10011101        # binary starts with 0b
4_294_967_296  # big numbers arranged by underline

Internally, however, Perl works as if they were all float type, with double precision.
That is, it does not have a value of 7 but a 7.0, for example.
If you've studied languages like C, C ++, or Java, for example, it's like Perl treats all numbers as double.

Mathematical Operations in Perl

The computer is nothing but a calculator, very fast and that makes several calculations at the same time. So, let's use Perl a bit to make our machine calculate?

The main mathematical operations are:

  • addition: using the + operator
  • subtraction: using the - operator
  • multiplication: using the * operator (asterisk and not x)
  • division: using the / operator
  • exponentiation: using the operator **
  • remainder of division or module: using operator %


Let's make a simple script that takes two numbers, 21 and 12, and does all these operations:
#!/usr/bin/perl
print 21 + 12, "\n"; # Addition
print 21 - 12, "\n"; # Subtraction
print 21 * 12, "\n"; # Multiplication
print 21 / 12, "\n"; # Division
print 21 **12, "\n"; # Exponentiation
print 21 % 12, "\n"; # Remainder
The result
Operações matemáticas no Perl

Note that Perl directly printed the result of the operations, he read and interpreted that these were operations and solved them, showing the resulting value.

Remembering that rest of the division is that 'rest' that was left over when you made division bins at school:

Perl tutorial


Note that at the end of each line is a hash symbol # and something written.
The name of this is comment, it is only for programmers to read, Perl totally ignores it, ok? It's just to better explain and organize Perl scripts.

Operator Precedence

Calculate, head on, the following operation:

  • 1 + 2 * 3


For some the result may be 9: 1 + 2 = 3, 3 * 3 = 9
For others it may be 7: 2 * 3 = 6, 1 + 6 = 7

So what's the right?
Let's let Perl solve:
#!/usr/bin/perl
print (1 + 2 * 3);
And the result he shows is 7. And the reason is the precedence of operators.

The multiplication operator has higher precedence in the operation.
Now create a script to calculate:

  • (1 + 2) * 3

#!/usr/bin/perl
print ( (1 + 2)*3 );
See that the result was 9. Changed just for a couple of parentheses, check?
This is because parentheses have a higher precedence than the multiplication operator.

There is an importance table of operator precedence.
It is (top to bottom, and in the same line from left to right):
( )
++ --
**
*   /   %
+   -
<   <=   >   >=
==   !=
=   +=   -=   *=   /=   %=   **=

Operators you don't know, don't worry, let's study calmly throughout our Perl course tutorials.

No comments:

Post a Comment