Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

grep integer and fractional part from a division (modulus ?)

by jeanluca (Deacon)
on Aug 11, 2006 at 11:40 UTC ( [id://566787]=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

I was wondering if it is possible to grep the integer and fractional part, from a division, into 2 different variables. I thought that the following example should be close to the real solution:
my ($a, $b) = 301 % 60 ; # or my ($a, $b) %= 301 / 60 ;
However this doesn't work at all, but I hope it explains what I would like todo (everything in 1 line :)
Any suggestions ?

Thnx
LuCa

Replies are listed 'Best First'.
Re: grep integer and fractional part from a division (modulus ?)
by Hofmator (Curate) on Aug 11, 2006 at 11:51 UTC
    my ($int, $remain) = (int 301/60, 301%60);

    This is a solution, but I doubt that this is what you were after. I don't see any clear way of doing it while not repeating the 301 and 60.

    Minor update: Changed variable name $frac to better name $remain as this is what it is, a remainder instead of a fraction. Samy_rio++ for spotting this.

    -- Hofmator

      I think this is the best way to write it. You could assign the numbers to variables first (they likely are already), but the principle of your solution is clear.

      One could write

      ($q, $r) = ( int( ($n=301) / ($d=60) ) , $n % $d );
      but that is definitely less clear, even though it avoids the repetion of the numbers.
Re: grep integer and fractional part from a division (modulus ?)
by rhesa (Vicar) on Aug 11, 2006 at 11:59 UTC
    use Math::BigInt; my ($n,$d) = (301, 60); my ($q, $r) = Math::BigInt -> new( $n ) -> bdiv( $d ); print "$n divided by $d equals $q with $r remaining";
Re: grep integer and fractional part from a division (modulus ?)
by Samy_rio (Vicar) on Aug 11, 2006 at 11:53 UTC

    to grep the integer and fractional part, from a division, into 2 different variables
    (everything in 1 line :)

    Try like this,

    use strict; use warnings; my ($a, $b) = (301 / 60) =~ m/^([^\.]+)\.?([^\.]*)$/; print "$a\t$b\n";

    Regards,
    Velusamy R.


    eval"print uc\"\\c$_\""for split'','j)@,/6%@0%2,`e@3!-9v2)/@|6%,53!-9@2~j';

Re: grep integer and fractional part from a division (modulus ?)
by swampyankee (Parson) on Aug 11, 2006 at 17:51 UTC

    There are a couple of points to be wary of in any processing like this. One is that floating point numbers are not equivalent to rational numbers. Some results will look something like

    $x = 75; $y = sprintf("%1.15f", 75/5); print $y; # 14.999999 ($i, $f) = split(/\./, $y); print "Integer part: $i\nFractional part: $f\n"; # Integer part 14 # Fractional part 0.999999

    Note:  this is an example, not necessarily an actual result.


    Which is, of course, right, and is also, probably, useless. (I'm currently ignoring the bignum pragma).

    What I would tend to do would be something like this:

    • Use sprintf with an appropriate format to round to a desired precision.
    • Split on the radix point (in the US, this is the period, ".")

    As an example:
    #!perl use strict;use warnings; my $fmt = "%1.5f"; # set 5 decimal places while(<DATA>){ chomp; (my $dividend, my $divisor) = split(/\s+/,$_,2); (my $int, my $frac) = split(/\./, (sprintf($fmt, ($dividend/$div +isor))),2); print "Integer part: $int\tFractional part: $frac\n"; } __DATA__ 1 2 2 1 3 4 4 3 127 5 75 5

    Produces:

    Integer part: 0 Fractional part: 50000
    Integer part: 2 Fractional part: 00000
    Integer part: 0 Fractional part: 75000
    Integer part: 1 Fractional part: 33333
    Integer part: 25        Fractional part: 40000
    Integer part: 15        Fractional part: 00000
    

    The other possible 'gotchas' include the possibility that results may not be consistent between platforms or between Perls built with different compilers on the same platform. Floating point arithmetic is fraught with peril.

    emc

    Experience is a hard teacher because she gives the test first, the lesson afterwards.

    Vernon Sanders Law
Re: grep integer and fractional part from a division (modulus ?)
by injunjoel (Priest) on Aug 11, 2006 at 16:31 UTC
    How about...
    my ($a, $b) = split(/\./, (301/60));
    That?

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forego their use." -Galileo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://566787]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 13:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found