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

my program deals with large integers and random numbers calculations and printing them to files.

I want to use printf() function to write in particular file using file handle and a array variable which contains all rows that i have to print. It is fine when i code like this

use bigint.pm $File1=">xyz.txt"; OPEN File1; ### calculations.... #### print(File1 ,@array);### @array having 20 and more digits of scalars close File1;
but i want to avoid use of bigint, to speed up my program and print to a file with proper format.

Replies are listed 'Best First'.
Re: how to avoid Bigint module
by moritz (Cardinal) on Mar 18, 2011 at 10:10 UTC
      Thanks, but GMP is for Unix based system, what about windows, is there any alternative?
Re: how to avoid Bigint module
by BrowserUk (Patriarch) on Mar 18, 2011 at 10:21 UTC

    Consider Math::Int128 & Math::GMPn.

    The former will handle integers up to 128 bits which is 38 decimal digits. Max: 340,282,366,920,938,463,463,374,607,431,768,211,456

    With the latter you can choose the number of bits you require--in steps of 2**n.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: how to avoid Bigint module
by ambrus (Abbot) on Mar 18, 2011 at 13:15 UTC

    Create bigint values explicitly eg.

    use Math::BigInt; my $big = Math::BigInt->new( "2539885693349640669028422069904549631176 +10523116274484848568321143714323882724980122910689" );
    Computations involving at least one such big integer will get infected and also become bigints. Other values, however, remain plain (non-big) perl number. If you wish to explicitly convert a big value back to a plain number, you can do one of
    my $small = $big->numify; # OR my $small = "" . $big;
    (this may of course lose precision if that number is large).

    Update: you may also restrict use bignum; to smaller scopes, such as bare brace blocks, in which case numeric literals in those scopes are automatically upgraded to big integers.

Re: how to avoid Bigint module
by JavaFan (Canon) on Mar 18, 2011 at 09:56 UTC
    If you don't want bigint there, then don't put it there!

    Of course, this may mean your calculations will now produce floats instead of integers (if the results were big enough to have warranted bigint in the first place), but that'll be the price you pay for the speed you gain.

Re: how to avoid Bigint module
by ikegami (Patriarch) on Mar 18, 2011 at 15:41 UTC

    You could build a perl configured to use quadruple precision floating-point format. Something like 34 digits of precision.