467,104 Members | 952 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,104 developers. It's quick & easy.

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
numberwhun
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
nithinpes
Expert 256MB
You can use printf() :
Expand|Select|Wrap|Line Numbers
  1. $div=1/3;
  2. printf("%.5f",$div);
  3.  
or sprintf():

Expand|Select|Wrap|Line Numbers
  1. $value = sprintf("%.5f",1/3);
  2. print $value;
  3.  
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
  1. #!/usr/bin/perl5.8.8 -w    #path to perl in my system  
  2.      #gives division $nom/$denom (1/3) to $prec (500) precise digits
  3.      #to the right of the decimal point.
  4. use strict;
  5.  
  6. use Math::BigInt;
  7. use Math::BigFloat;
  8. my $nom = 1;
  9. my $denom = 3;   
  10. my $x = Math::BigFloat->new($nom);           # scalar $nom -> BigFloat $x. 
  11. my $prec = -500;
  12. Math::BigFloat->precision($prec);                # sets precision    
  13. my $value = $x->copy()->bdiv($denom);       # BigFloat division $x/$denom
  14. print $value,"\n";
  15.  

Problem solved, thank you!
zakad
Aug 11 '08 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Julia Bell | last post: by
9 posts views Thread by Martin Foster | last post: by
2 posts views Thread by Brian van den Broek | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.