user8624950
user8624950

Reputation:

What is the meaning of the % operator?

I saw recently in on an answer here on Stack Overflow where a calculation was done to determine whether a value was odd or even. There was a % used which always returned 0 or 1 depending on the value it was compared with.

I am trying to figure out what the usage is of this % and even though I searched on google, I could only find % used in hash.

$result = $str % 2;

Upvotes: 2

Views: 17406

Answers (4)

Vicky
Vicky

Reputation: 1338

This % is a modulus operator which returns the remainder of the left operand divided by the right operand, which is zero in the case where the value on the left is divisible by the value on the right. It does not represents a hash sigil if it is between two operands.

See Perl & modulus on perlmonks.org.

Upvotes: 7

choroba
choroba

Reputation: 242123

It's the "modulo" operator. See perlop.

[...] computes the division remainder of its first argument with respect to its second argument.

Upvotes: 9

HoldOffHunger
HoldOffHunger

Reputation: 20931

As stated by the others, % is the modulo operator, and it returns the remainder after doing arithmetic division. It's rather common to want the remainder (the results of the modulus) and the quotient (the results of the division). Fortunately, it's very easy to get both in Perl!

I will take a working, mathematical form as: dividend / divisor = quotient + remainder.

enter image description here

You can always get the quotient and remainder by means of...

my $dividend = 100;
my $divisor = 3;
my ($quotient, $remainder) = (int $dividend / $divisor, $dividend % $divisor);
print($dividend . ' / ' . $divisor . ' = ' . $quotient . ' (quotient) + ' . $remainder . ' (remainder)');

And this gives the results:

100 / 3 = 33 (quotient) + 1 (remainder)

This is a nice, useful one-line solution. Here's a practical demonstration of it with a time example...

my $diff = 642;
my $divisor = 60;
my ($minutes, $seconds) = (int $diff / $divisor, $diff % $divisor);
print($minutes . ':' . $seconds);

Upvotes: 1

Dave Cross
Dave Cross

Reputation: 69314

The modulus operator gives you the remainder from an integer division.

You saw it being used to work out if a number is odd or even. When you divide an integer by 2, you get a remainder of 0 (if the number is even) or 1 (if the number is odd).

my $is_odd = $num % 2;

In general, the modulus will give you an integer between 0 and one less than the right-hand operand in the expression.

Like all operators, it is documented in perldoc perlop, which says:

Binary "%" is the modulo operator, which computes the division remainder of its first argument with respect to its second argument. Given integer operands $m and $n : If $n is positive, then $m % $n is $m minus the largest multiple of $n less than or equal to $m. If $n is negative, then $m % $n is $m minus the smallest multiple of $n that is not less than $m (that is, the result will be less than or equal to zero). If the operands $m and $n are floating point values and the absolute value of $n (that is abs($n)) is less than (UV_MAX + 1), only the integer portion of $m and $n will be used in the operation (Note: here UV_MAX means the maximum of the unsigned integer type). If the absolute value of the right operand (abs($n)) is greater than or equal to (UV_MAX + 1) , "%" computes the floating-point remainder $r in the equation ($r = $m - $i*$n) where $i is a certain integer that makes $r have the same sign as the right operand $n (not as the left operand $m like C function fmod()) and the absolute value less than that of $n. Note that when use integer is in scope, "%" gives you direct access to the modulo operator as implemented by your C compiler. This operator is not as well defined for negative operands, but it will execute faster.

Upvotes: 8

Related Questions