How can I set the level of precision for Perl's bignum?
Asked Answered
I

1

9

I'm trying to use the bignum module in Perl and want to set the precision. I know this can be done via a one liner as detailed on the module's CPAN page:

$ perl -Mbignum=p,-50 -le 'print sqrt(20)'

...which will print out the square root of 20 to 50 digits of precision, but what I'm wondering is if there's anyway to set the precision within a script, i.e. something like:

#!/usr/bin/perl
use bignum;

setPrecision(-50);
print sqrt(20);

I've searched around here, Google, and PerlMonks without any luck so far. Thanks in advance.

Ingenuity answered 25/1, 2010 at 22:57 Comment(3)
Specify it as an argument to the package in the use bignum line.Linville
See perldoc perlrun; perl -MPackage=foo,bar is equivalent to starting the program with use Package qw(foo bar).Tret
Related: #1839308Balsa
B
12

Per Anon.'s suggestion:

#!/usr/bin/perl

use strict;
use warnings;

use bignum ( p => -50 );

print sqrt(20);

You might like to look at the docs for Math::BigFloat and Math::BigInt which bignum makes use of.

Balsa answered 25/1, 2010 at 23:6 Comment(1)
That does not work for me, Perl is wrong here: perl -e 'use bignum(p=>-100);printf("%1.50f\n",sqrt(23));' outputs 4.79583152331271911350540904095396399497985839843750 This is right: perl -Mbignum=p,-50 -le 'print sqrt(23)' 4.79583152331271954159743806416269391999670704190413 compared with the correct calculation from bc: echo "scale=50;sqrt(23)"|bc 4.79583152331271954159743806416269391999670704190412Psychotechnology

© 2022 - 2024 — McMap. All rights reserved.