Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Math::BigInt Newbee question.

by Plankton (Vicar)
on Jan 27, 2005 at 16:39 UTC ( [id://425611]=perlquestion: print w/replies, xml ) Need Help??

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

Friends,

I have never used Math::BigInt before. So I came up with an excuse to write a script that would use it. I wanted to write a simple script that would tell me how many CDs would be needed to hold X Gigabytes of data. Here's the script ...

#!/usr/bin/perl -w use strict; use Math::BigInt; #BigInt KiloByte Unit my $KB = Math::BigInt ( "1024" ); #BigInt MegaByte Unit my $MB = Math::BigInt ( $KB->bmul( $KB ) ); #BigInt GigaByte Unit my $GB = Math::BigInt ( $MB->bmul( $KB ) ); my $arg1 = Math::BigInt->new( shift ); # arg1 is in GBs my $GBarg1 = Math::BigInt->new( $arg1->bmul( $GB ) ); my $arg2 = Math::BigInt->new( shift ); # arg2 is in MBs my $MBarg2 = Math::BigInt->new( $arg2->bmul( $MB ) ); # why rc is 0? my $rc = $GBarg1->bdiv( $MBarg2 ); print "rc is [$rc]\n";
When I run this I get the output of:
$ ./whomany.pl 2 675 rc is [0]
What am I doing wrong here?

Thanks

update: Fixed typo (missing my $arg2) pointed out by Paladin.

Replies are listed 'Best First'.
Re: Math::BigInt Newbee question.
by Paladin (Vicar) on Jan 27, 2005 at 16:57 UTC
    When I run that code, I get
    $ perl whomany.pl 2 675 Global symbol "$arg2" requires explicit package name at test.pl line 2 +0. Execution of test.pl aborted due to compilation errors.

    So I am guessing that the code you pasted is not the code that's actually running. You don't declare $arg2 and are missing ->new in a few places.

    Also, bdiv and bmul modify inplace. I believe what you were trying to write is:

    #!/usr/bin/perl -w use strict; use Math::BigInt; #BigInt KiloByte Unit my $KB = Math::BigInt->new( "1024" ); #BigInt MegaByte Unit my $MB = $KB->copy()->bmul( $KB ); #BigInt GigaByte Unit my $GB = $MB->copy()->bmul( $KB ); print "$KB, $MB, $GB\n"; my $arg1 = Math::BigInt->new( shift ); my $arg2 = Math::BigInt->new( shift ); # arg1 is in GBs my $GBarg1 = $arg1->copy()->bmul( $GB ); # arg2 is in MBs my $MBarg2 = $arg2->copy()->bmul( $MB ); print $GBarg1, " ", $MBarg2, "\n"; # why rc is 0? my ($rc, $rem) = $GBarg1->copy()->bdiv( $MBarg2 ); print "rc is [$rc]\n";
    Note: Also, to be really correct, you will need to add 1 more CD to the final result to take up the remainder of the data.

    Update: Clean up code a bit

      I think you can also do my $MB = $KB * $KB; instead of my $MB = $KB->copy()->bmul( $KB ); thanks to overloaded operators.

Re: Math::BigInt Newbee question.
by ikegami (Patriarch) on Jan 27, 2005 at 17:23 UTC
    Keep in mind that $GBarg1 / $MBarg2 will truncate decimals, giving you an incorrect result almost every time. For example, 2GB / 675MB will give 3, but you would actually need 4 CDs. ($GBarg1 + $MBarg2 - 1) / $MBarg2 will give you the correct answer.
Re: Math::BigInt Newbee question.
by hv (Prior) on Jan 28, 2005 at 12:10 UTC

    Math::BigInt code reads much more simply if you treat them as numbers; you can also take advantage of the fact that when one of the operands of a numeric operator is a Math::BigInt the other is automatically upgraded:

    use Math::Bigint; my $KB = Math::BigInt->new(1024); my $MB = $KB * $KB; my $GB = $MB * $KB; my $required = shift(@ARGV) * $GB; my $CDsize = shift(@ARGV) * $MB; print +($required + $CDsize - 1) / $CDsize;

    Hugo

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 01:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found