Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

hello everyone,
I am very new to perl , I wanted to perform the following when I want to a div between to integers like the way we do in java so say if it's 5/3 i want the result to be 2, is there a mathematical operator that does that in perl

Title edit by tye

  • Comment on mathematical operator for integer division, "div" ?

Replies are listed 'Best First'.
Re: mathematical operators
by djantzen (Priest) on Jun 16, 2003 at 22:01 UTC

    You want the modulus operator (%), same as in Java. I.e., perl -e 'print(5 % 3, $/)'. See perlop.


    "The dead do not recognize context" -- Kai, Lexx
Re: mathematical operators
by freddo411 (Chaplain) on Jun 16, 2003 at 22:26 UTC
    Perhaps you mean the function modulo which in perl is

    5%3 = 2

    What we used to call a remander in grade school.

    If you really mean division, then I don't grok what you mean. Because 5/3 is either 1.66666 or 1, floating point or integer respectively.

    If you still really want 5/3 = 2 in perl then:

    use strict; print " 5/3 = 2 \n"; # coder flunked math
    will work.

    -------------------------------------
    Nothing is too wonderful to be true
    -- Michael Faraday

Re: mathematical operators
by BazB (Priest) on Jun 16, 2003 at 21:59 UTC

    Not math, but perl -le 'print sprintf("%.0f", 5/3);'.

    Try perldoc -q round for a more complete answer.

    Update: monsieur_champs++ might well be right, but the AnonyMonk's question is unclear. 5/3 rounded to the nearest integer is 2, but so is 5 % 3 :-)


    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

      Dear BazB

      Our anonymous fellow was asking about the remainder operator (%). What I see here is a (useful, indeed) number formatting tip. Please correct this one. ++BazB for his sincere and humble signature asking for other monks to teach him.

      =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
      Just Another Perl Monk

Re: mathematical operators
by Itatsumaki (Friar) on Jun 16, 2003 at 23:07 UTC

    Did you want the remainder (i.e. the modulo, as given by other posters) or just the result rounded up? You can get the latter with:

    use POSIX qw/ceil/; print POSIX::ceil( 5/3 );
    -Tats
Re: mathematical operators
by Mr. Muskrat (Canon) on Jun 16, 2003 at 21:57 UTC

    With Perl 5.8, you can do the following:

    use integer; my $result = 5/3; print "$result\n";

    Otherwise, you can do something like:

    my $result = int(5/3); print "$result\n";

    Update: I believe my head is screwed on backwards today. ;-) Both result in 1. D'oh!

Re: mathematical operator for integer division, "div" ?
by adamcrussell (Hermit) on Jun 17, 2003 at 20:15 UTC
    I am not sure what you are asking for? In java, 5/3 does not yield a result of 2. Indeed, the following example which when compiled and run yields a 1.
    public class temp{ public static void main(String[] args){ int a=5; int b=3; int c=a/b; System.out.println(c); } }
    Others have suggested that you mean the remainder of the divsion of the two integers but possible you may mean to round up? Perhaps you may clarify this some?