TExcelTAD
Home › Perl › Perl Numbers

Perl Numbers

Perl stores numbers internally as either integers or double-precision floating-point values and switches between them automatically. You rarely need to care which one you have, but it helps to know the rules.

Numeric literals

perl · try it yourself
use strict;
use warnings;

my $count    = 42;          # integer
my $negative = -17;
my $pi       = 3.14159;     # floating point
my $big      = 1_000_000;   # underscores improve readability
my $tiny     = 2.5e-3;      # scientific notation
my $hex      = 0xff;        # hexadecimal
my $octal    = 0o755;       # octal (also written 0755)
my $binary   = 0b1010;      # binary

print "$count $negative $pi $big $tiny\n";
print "$hex $octal $binary\n";
42 -17 3.14159 1000000 0.0025
255 493 10

Underscores in a numeric literal are ignored, so 1_000_000 is just one million. Perl prints numbers in a "natural" form, so the hexadecimal literal 0xff prints as 255.

Arithmetic operators

Operator Meaning Example Result
+ - * add, subtract, multiply 7 * 6 42
/ division (always floating point) 7 / 2 3.5
% modulus (remainder) 17 % 5 2
** exponentiation 2 ** 10 1024
++ -- increment, decrement $n++ adds 1
perl · try it yourself
use strict;
use warnings;

my $apples = 17;
my $people = 5;

print "Each person gets ", int($apples / $people), " apples\n";
print "Left over: ", $apples % $people, "\n";
print "Exact share: ", $apples / $people, "\n";
print "2 to the 16th: ", 2 ** 16, "\n";

my $visits = 9;
$visits++;
$visits += 5;     # same as $visits = $visits + 5
print "Visits: $visits\n";
Each person gets 3 apples
Left over: 2
Exact share: 3.4
2 to the 16th: 65536
Visits: 15

Note: Unlike C or Java, / never performs integer division: 7 / 2 is 3.5. Use int() to drop the fractional part.

Built-in math functions

Perl has a small set of numeric built-ins. More (like floor and ceil) live in the core POSIX module:

perl · try it yourself
use strict;
use warnings;
use POSIX qw(floor ceil);

print "abs(-8)    = ", abs(-8), "\n";
print "sqrt(144)  = ", sqrt(144), "\n";
print "int(-3.7)  = ", int(-3.7), "\n";
print "floor(-3.7)= ", floor(-3.7), "\n";
print "ceil(3.2)  = ", ceil(3.2), "\n";
printf "exp(1)     = %.5f\n", exp(1);
printf "log(100)   = %.5f\n", log(100);
printf "rounded    = %.2f\n", 2.71828;
abs(-8)    = 8
sqrt(144)  = 12
int(-3.7)  = -3
floor(-3.7)= -4
ceil(3.2)  = 4
exp(1)     = 2.71828
log(100)   = 4.60517
rounded    = 2.72

int truncates toward zero, while floor always rounds down. log is the natural logarithm. To round to a number of decimals, use sprintf or printf with a format such as %.2f (covered in its own lesson).

Floating-point surprises

Floating-point numbers are binary approximations, just like in every other language. Compare them with a tolerance, not with ==:

perl · try it yourself
use strict;
use warnings;

my $sum = 0.1 + 0.2;
print "sum = $sum\n";
print "equal? ", ($sum == 0.3 ? "yes" : "no"), "\n";
printf "full precision: %.17f\n", $sum;
print "close enough? ", (abs($sum - 0.3) < 1e-9 ? "yes" : "no"), "\n";
sum = 0.3
equal? no
full precision: 0.30000000000000004
close enough? yes

Perl prints the sum as 0.3 because it shows about 15 significant digits by default, but the stored value is slightly larger.

Random numbers

rand($n) returns a random floating-point number from 0 up to (but not including) $n, and int(rand(6)) + 1 simulates a die. Call srand with a fixed seed when you want a repeatable sequence, for example in tests. Because the exact numbers depend on the Perl build, examples in this course avoid printing random values.

Test yourself

What does 7 / 2 evaluate to in Perl?

  1. 3
  2. 3.5
  3. 4
  4. an error

What is int(-3.7)?

  1. -4
  2. -3
  3. 3
  4. -3.7

What does 2 ** 5 compute?

  1. 10
  2. 25
  3. 32
  4. 7