Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

testing if divisible by?

1,672 views
Skip to first unread message

Paul

unread,
May 1, 2010, 7:15:58 AM5/1/10
to perl
Hello all. How can I test to see if a number is divisible by say, 40?
Thanks.

Philip Potter

unread,
May 1, 2010, 7:45:17 AM5/1/10
to opens...@unixoses.com, perl
On 1 May 2010 12:15, Paul <opens...@unixoses.com> wrote:
> Hello all.  How can I test to see if a number is divisible by say, 40?
> Thanks.

Use the modulo operator %. Given integers $x and $y, the expression $x
% $y gives the remainder when $x is divided by $y. As a result, if
(and only if) $x is exactly divisible by $y, $x % $y is equal to 0.

#!perl
use 5.010; # for 'say'

say 5 % 2;
say 6 % 2;
say 7 % 2;

say 79 % 40;
say 80 % 40;
say 81 % 40;

For more information, see
http://perldoc.perl.org/perlop.html#Multiplicative-Operators

Phil

Shawn H Corey

unread,
May 1, 2010, 7:43:08 AM5/1/10
to opens...@unixoses.com, perl
Paul wrote:
> Hello all. How can I test to see if a number is divisible by say, 40?
> Thanks.
>
>

See `perldoc perlop` and search for /Multiplicative Operators/ Read the
part about the % operator.


--
Just my 0.00000002 million dollars worth,
Shawn

Programming is as much about organization and communication
as it is about coding.

I like Perl; it's the only language where you can bless your
thingy.

Eliminate software piracy: use only FLOSS.

Jamie L. Penman-Smithson

unread,
May 1, 2010, 2:08:17 PM5/1/10
to begi...@perl.org
On Sat, 2010-05-01 at 07:15 -0400, Paul wrote:
> Hello all. How can I test to see if a number is divisible by say, 40?

Use the modulo operator:

my $a = 40;
my $b = 1;

if ($a % $b == 0) {
print "$b is divisible by $a\n";
}

-j

Uri Guttman

unread,
May 1, 2010, 2:24:08 PM5/1/10
to Jamie L. Penman-Smithson, begi...@perl.org
>>>>> "JLP" == Jamie L Penman-Smithson <li...@silverdream.org> writes:

JLP> On Sat, 2010-05-01 at 07:15 -0400, Paul wrote:
>> Hello all. How can I test to see if a number is divisible by say, 40?

JLP> Use the modulo operator:

JLP> my $a = 40;
JLP> my $b = 1;

JLP> if ($a % $b == 0) {

no need for the == 0 if you invert the test with unless or change the
print text.

JLP> print "$b is divisible by $a\n";
JLP> }

uri

--
Uri Guttman ------ u...@stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------

Jay Savage

unread,
May 3, 2010, 10:03:42 AM5/3/10
to Perl Beginners, opens...@unixoses.com
On Sat, May 1, 2010 at 7:45 AM, Philip Potter <philip....@gmail.com> wrote:
> On 1 May 2010 12:15, Paul <opens...@unixoses.com> wrote:
>> Hello all.  How can I test to see if a number is divisible by say, 40?
>> Thanks.
>
> Use the modulo operator %. Given integers $x and $y, the expression $x
> % $y gives the remainder when $x is divided by $y. As a result, if
> (and only if) $x is exactly divisible by $y, $x % $y is equal to 0.
>

And there's the rub: "number" ne "integer".

% is fine if you're only interested in integers, but if you want to
compare other numbers use fmod() from POSIX.pm:

perl -MPOSIX -wle 'print POSIX::fmod(35, 17.5)'

It's considerably slower than %, but it gets the job done.

HTH,

-- jay
--------------------------------------------------
This email and attachment(s): [ ] blogable; [ x ] ask first; [ ]
private and confidential

daggerquill [at] gmail [dot] com
http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org

values of β will give rise to dom!

Philip Potter

unread,
May 3, 2010, 10:46:20 AM5/3/10
to Jay Savage, Perl Beginners, opens...@unixoses.com
2010/5/3 Jay Savage <dagge...@gmail.com>:

> On Sat, May 1, 2010 at 7:45 AM, Philip Potter <philip....@gmail.com> wrote:
>> On 1 May 2010 12:15, Paul <opens...@unixoses.com> wrote:
>>> Hello all.  How can I test to see if a number is divisible by say, 40?
>>
>> Use the modulo operator %. Given integers $x and $y, the expression $x
>
> And there's the rub: "number" ne "integer".
>
> % is fine if you're only interested in integers, but if you want to
> compare other numbers use fmod() from POSIX.pm:
>
>    perl -MPOSIX -wle 'print POSIX::fmod(35, 17.5)'

fmod is a fine replacement for % in general for testing remainders of
floating-point valued quotients, but using it as a divisibility test
requires caution and serious consideration of a different approach. It
has the same issues as a floating-point equality test: that is,
because floating-point is an inexact representation, the results can
depend on whether a value was rounded up or down:

D:\>perl -MPOSIX -wle "print POSIX::fmod(0.2, 0.1)"
0

D:\>perl -MPOSIX -wle "print POSIX::fmod(0.3, 0.1)"
0.1

D:\>perl -MPOSIX -wle "print POSIX::fmod(0.4, 0.1)"
0

D:\>perl -MPOSIX -wle "print POSIX::fmod(0.5, 0.1)"
0.1

These examples have strange results because 0.1 is not exactly
representable in binary floating-point.

Phil

0 new messages