Re: testing if divisible by?

2010-05-03 Thread Jay Savage
On Sat, May 1, 2010 at 7:45 AM, Philip Potter philip.g.pot...@gmail.com wrote:
 On 1 May 2010 12:15, Paul opensou...@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!

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: testing if divisible by?

2010-05-03 Thread Philip Potter
2010/5/3 Jay Savage daggerqu...@gmail.com:
 On Sat, May 1, 2010 at 7:45 AM, Philip Potter philip.g.pot...@gmail.com 
 wrote:
 On 1 May 2010 12:15, Paul opensou...@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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




testing if divisible by?

2010-05-01 Thread Paul
Hello all.  How can I test to see if a number is divisible by say, 40? 
Thanks.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: testing if divisible by?

2010-05-01 Thread Shawn H Corey

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.0002 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.

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: testing if divisible by?

2010-05-01 Thread Philip Potter
On 1 May 2010 12:15, Paul opensou...@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

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: testing if divisible by?

2010-05-01 Thread Jamie L. Penman-Smithson
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


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: testing if divisible by?

2010-05-01 Thread Uri Guttman
 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 -

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/