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

I need high level of precision in my Perl script, so I'm using BigFloat. Everything works well within subroutines, but when I try to pass a BigFloat number from one sub to another it reverts to a regular float. Specifically, I don't think I have a problem putting big numbers into a subroutine, just making getting the output to be a big number. The numbers could be reverting to floats when I put them in the return array, or during the return process. Does anyone know how to: 1) Put BigFloat numbers into an array and 2) Pass BigFloat numbers between subroutines Thanks! Here's a snippet of the code:
#!/usr/bin/perl use strict; use Math::BigFloat; Math::BigFloat->accuracy(20); sub simulate_lifespan{ my $r = Math::BigFloat->new($_[0]); my $org_muv = Math::BigFloat->new($_[1]); my $org_muy = Math::BigFloat->new($_[2]); my $scaler = Math::BigFloat->new($_[3]); my $ext_death = Math::BigFloat->new($_[4]); my $lifetime_fitness = Math::BigFloat->new(0); my $log_lifetime_fitness = $lifetime_fitness->copy(); $log_lifetime_fitness ->blog(); my @return_array = ($time, $log_lifetime_fitness, $mean_v, $mean_y +); return \@return_array; }

Replies are listed 'Best First'.
Re: Passing a BigFloat number through a subroutine
by Marshall (Canon) on Jul 17, 2016 at 20:01 UTC
    Echoing comments from stevieb, post some "working" code that shows the precision problem. This code doesn't do that for me.
    #!/usr/bin/perl use strict; use Math::BigFloat; Math::BigFloat->accuracy(20); sub simulate_lifespan{ my $r = Math::BigFloat->new($_[0]); my $org_muv = Math::BigFloat->new($_[1]); my $org_muy = Math::BigFloat->new($_[2]); my $scaler = Math::BigFloat->new($_[3]); my $ext_death = Math::BigFloat->new($_[4]); my $lifetime_fitness = Math::BigFloat->new(0); my $log_lifetime_fitness = $lifetime_fitness->copy(); $log_lifetime_fitness ->blog(); my @return_array = ($time, $log_lifetime_fitness, $mean_v, $mean_y +); return \@return_array; } __END__ prints: Global symbol "$time" requires explicit package name at C:\Projects_Pe +rl\testing\junk.pl line 16. Global symbol "$mean_v" requires explicit package name at C:\Projects_ +Perl\testing\junk.pl line 16. Global symbol "$mean_y" requires explicit package name at C:\Projects_ +Perl\testing\junk.pl line 16. Execution of C:\Projects_Perl\testing\junk.pl aborted due to compilati +on errors. Process completed with exit code 255
Re: Passing a BigFloat number through a subroutine
by stevieb (Canon) on Jul 17, 2016 at 18:49 UTC

    Welcome to the Monastery, pgnelson307!

    It would help us if you would edit your question and add in a short code example that demonstrates your problem so we can easily duplicate and test what you're seeing.

    Please put said code within <code></code> tags within the post.

Re: Passing a BigFloat number through a subroutine
by Anonymous Monk on Jul 17, 2016 at 20:01 UTC

    You suspect the numbers could be reverting to floats when assigned to the return array? Now that's easy to check: just put in debugging statements where needed.

    use Data::Dumper; ... my @return_array = ...; print Dumper \@return_array;

    See also the Basic debugging checklist.

Re: Passing a BigFloat number through a subroutine
by pgnelson307 (Initiate) on Jul 17, 2016 at 20:27 UTC
    So it looks like the rounding if occurring when the number is put in an array. Is there a way to have an array reference point to a BigFloat number?

      This is impossible. Putting a scalar or an object into an array changes nothing about the scalar or object. I'm guessing you are probably operating on the numbers somewhere and accidentally converting them. You need to provide code that reproduces the problem to be certain of a correct answer though.