Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 8th, 2008, 09:55 AM
Newbie
 
Join Date: Aug 2008
Posts: 4
Default 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
Reply
  #2  
Old August 8th, 2008, 10:39 AM
numberwhun's Avatar
Forum Leader
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,156
Default

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
Reply
  #3  
Old August 8th, 2008, 12:29 PM
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Age: 24
Posts: 366
Default

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.  
Reply
  #4  
Old August 10th, 2008, 06:22 PM
Newbie
 
Join Date: Aug 2008
Posts: 4
Default

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
Reply
  #5  
Old August 10th, 2008, 06:52 PM
pawanrpandey's Avatar
Newbie
 
Join Date: Feb 2007
Location: Bangalore
Posts: 11
Default

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
Reply
  #6  
Old August 11th, 2008, 07:09 PM
Newbie
 
Join Date: Aug 2008
Posts: 4
Default

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
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles