Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Misunderstanding with Math::BigInt

by Smoke (Initiate)
on Jun 30, 2005 at 20:47 UTC ( [id://471471]=perlquestion: print w/replies, xml ) Need Help??

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

Hello to everyone, In the documentation of Math::BigInt it's mentioned that it can work with numbers that have thousands od digits. Though, i ran to a slight proplem with the following code :

... my ($in_out) = @_; my ($out,$diver,$grade) = split(/,/, $in_out, 3); my $in = Math::BigInt->new(($out)**(1/$grade)*$diver); ...
when $in_out is 2.84894533078051,5,0.002 , but when i try to print $in, i get :
10990761002367800000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000
my first question is WHY ?
and the second question is how do i make it print the real reasult which should be :
10990761002372220870980990761002372220870980980980990761002372220 87098099076100237222087098099076100237222087098098098099076100237 22208709809907610023722208709809907610023722208709809809809907610 0237222087098099076100237222087098
???

Large numbers broken up to multiple lines to prevent long lines causing horizontal scrolling, by davido.

Replies are listed 'Best First'.
Re: Misunderstanding with Math::BigInt
by BrowserUk (Patriarch) on Jun 30, 2005 at 21:08 UTC

    The first problem is that 2.87... & 0.002 are not ints, so BigInt doesn't help. You need Math::BigFloat.

    use Math::BigFloat; my($out,$diver,$grade) = map{ Math::BigFloat->new( $_ ) } split(/,/, ' 2.84894533078051,5,0.002', 3); print $out ** ( 1/$grade ) * $diver; 109907610023675317191879876436497649438076948727516195849531 529984640080900905500623346430661197750153976478077957100021 178235709357073884306056471657477469213296476869314035526387 8485678475840878789876340877189092206814894928600.3227150712 343412750871362367932483896648862164560115552012310931635017 ... <lots more snipped> ... 198626328593786831872991187091247473250005 perl>

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      thanks a lot !!! it works perfectly.
      but is there a way to get only the whole number (without what after the dot) ? i tried int() but it rounds the whole number somewhy...

        Here's one way:

        print +( split '\.', $out ** ( 1/$grade ) * $diver )[ 0 ];

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: Misunderstanding with Math::BigInt
by salva (Canon) on Jun 30, 2005 at 20:59 UTC
    because your code is equivalent to
    my ($in_out) = @_; my ($out,$diver,$grade) = split(/,/, $in_out, 3); my $float = ($out)**(1/$grade)*$diver; my $in = Math::BigInt->new($float);
    so you are making the calculation with floats and then converting the result to a bigint.

    You have to convert first the numbers to bigints and then make the math!!!

      what do you mean by 'convert first the numbers to bigints' ?
        As your code stands now, your variables are handled solely by perl's own internal representation, and aren't converted into bigints until the very last minute, after the exponentation and multiplication. At that point it is too late to preserve the extra tens of digits that you want, because they aren't there to be preserved.

        Convert your $out, $driver, and $grade variables to bigints before you do any exponentation and multiplying.

        By the way, don't you mean to use Math::BigFloat instead?

        John
        my ($in_out) = @_; my ($out,$diver,$grade) = split(/,/, $in_out, 3); $out = Math::BigInt->new($out); my $rgrade = Math::BigInt->new(1/$grade); $diver = Math::BigInt->new($diver); my $in = $out**$rgrade*$diver;
Re: Misunderstanding with Math::BigInt
by Anonymous Monk on Jun 30, 2005 at 21:10 UTC
    You probably want to append ':constant' to your use Math::BigInt declaration (see the section entitled "Autocreating Constants" in the documentation). But it doesn't look like you are doing integer math, so maybe BigFloat is more appropriate. Try...
    #!/usr/bin/perl -w use strict; use Math::BigFloat ':constant'; my ($out,$diver,$grade) = (2.84894533078051,5,0.002); my $in = Math::BigFloat->new(($out)**(1/$grade)*$diver); print $in;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-19 11:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found