473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

BIGNUM Precision in PERL Script

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
5 5617
numberwhun
3,509 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
410 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
zakad
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
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
zakad
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

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

Similar topics

3
by: dpackwood | last post by:
Hello, I have two different scripts that do pretty much the same thing. The main perl script is on Windows. It runs and in the middle of it, it then calls out another perl script that then...
1
by: Julia Bell | last post by:
I would like to run the same script on two different platforms. The directory in which the script(s) will be stored is common to the two platforms. (I see the same directory contents regardless...
9
by: Martin Foster | last post by:
Hi. I would like to be able to mimic the unix tool 'uniq' within a Perl script. I have a file with entries that look like this 4 10 21 37 58 83 111 145 184 226...
2
by: Brian van den Broek | last post by:
Hi all, I guess it is more of a maths question than a programming one, but it involves use of the decimal module, so here goes: As a self-directed learning exercise I've been working on a...
9
by: 8anos | last post by:
Hello, I am new at the community and newbie at programming :) As you may know rapidshare provides a perl script for linux, to upload files at their servers. You can find the original scripts at...
21
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Uploading files from a local computer to a remote web server has many useful purposes, the most...
6
by: Matthew | last post by:
Hi, I want to change the precision level of floating point variables and calculations which is done in php.ini. However the server I rent for my domain does not give me access to php.ini,...
20
by: mike3 | last post by:
Hi. (Xposted to both comp.lang.c++ and comp.programming since I've got questions related to both C++ language and general programming) I've got the following C++ code. The first routine runs in...
1
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.