By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,773 Members | 1,724 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,773 IT Pros & Developers. It's quick & easy.

BIGNUM Precision in PERL Script

P: 5
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
Share this Question
Share on Google+
5 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
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 100+
P: 410
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

P: 5
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

P: 14
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

P: 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.