467,104 Members | 952 Online

BIGNUM Precision in PERL Script

 Hello, I have a working program in C++ that I am trying to rewrite in PERL. The program involves a division of two large integers with the result containing a predetermined number of decimal digits to the right of the dot. I have found several working examples of setting a fixed precision from the command line, such as: perl -Mbignum=p,-50 -le 'print sqrt(20)' This example also works with a division such as ... 'print 1/3'. However, I have been unable to find any example of how to set the precision of division to a value assigned to a variable such as \$n inside a PERL program. Help will be appreciated. zakad Aug 8 '08 #1
• viewed: 5304
Share:
5 Replies
 Expert Mod 2GB I don't know if this is what you are looking for, but Perl does have printf and sprintf functions to define the output precision. Regards, Jeff Aug 8 '08 #2
 Expert 256MB You can use printf() : Expand|Select|Wrap|Line Numbers \$div=1/3; printf("%.5f",\$div);   or sprintf(): Expand|Select|Wrap|Line Numbers \$value = sprintf("%.5f",1/3); print \$value;   Aug 8 '08 #3
 Thank you for the replies! Perhaps an example would clarify the float precision I am interested in. The division of 1/3 to a precision of 500 decimal digits would result in 500 3s to the right of the decimal point. The functions printf and sprintf do not influence precision. Using either of these functions with "%.500f" would give 500 digits in the output, but only the first 16 are precise 3s (in my system). It is possible to increase the precision to the full 500 decimal digits: 1. In C++/GMP, the division must be preceded by: mpf_set_default_prec(500*log(10)/log(2)); # GMP computes precision in binary digits. 2. In PERL, from the command line,: perl -Mbignum=p,-500 -le 'print 1/3' My question is: what is the syntax to be used in PERL script? I shall be grateful for the answer. zakad Aug 10 '08 #4
 You can try 'Math' module from CPAN (Match::BigInt, Math::BigFloat). This might help to achieve your goal. Regards, Pawan LinKs: http://search.cpan.org/~tels/Math-BigInt-1.89/lib/Math/BigInt.pm http://search.cpan.org/~tels/Math-BigInt-1.89/lib/Math/BigFloat.pm Aug 10 '08 #5
 Thank you Pawan for the Links. They solved my problem. I am new to both Linux and Perl. As far as I can make out, whereas BigInt is transparent in Perl, BigFloat is not--at least not yet. A scalar variable has to be changed explicitly to a BigFlaot variable before it can be used as such. The following code gives the division of 1/3 as 500 3s to the right of the decimal point (please note this is a beginner's program): Expand|Select|Wrap|Line Numbers #!/usr/bin/perl5.8.8 -w    #path to perl in my system        #gives division \$nom/\$denom (1/3) to \$prec (500) precise digits      #to the right of the decimal point. use strict;   use Math::BigInt; use Math::BigFloat; my \$nom = 1; my \$denom = 3;    my \$x = Math::BigFloat->new(\$nom);           # scalar \$nom -> BigFloat \$x.  my \$prec = -500; Math::BigFloat->precision(\$prec);                # sets precision     my \$value = \$x->copy()->bdiv(\$denom);       # BigFloat division \$x/\$denom print \$value,"\n";   Problem solved, thank you! zakad Aug 11 '08 #6