PDA

View Full Version : Binary Coded Decimal and Perl


rick-k
08-09-2005, 03:26 AM
I want to run some Perl scripts that will interpolate astronomical data. The big problem is the way computers handle numbers with long strings after the decimal point. Calculators have chips and spreadsheets have code that make them handle decimal math properly. But most computers ever made convert numbers to binary whose limitations are magnified when your script needs to calculate the difference between two very small numbers.

It's a problem similar to using 3.14 as the value of pi when you need to calculate the circumference of a 1000-foot wide circle but expecting accuracy down to a fraction of an inch. With 3.14 you end up about 19 inches off. The solution, of course, is to take pi out to more decimal places.

With a Perl script, the solution is to use binary coded decimal (BCD). Does FutureQuest have a BCD module with Perl? If so, how do I use it? If not, would someone who understands the issue please offer a code snippet?

Wassercrats
08-09-2005, 03:51 AM
Would Math::BigFloat (http://search.cpan.org/search?mode=module&query=Math%3a%3aBigFloat) work? It lets you select the accuracy and precision (http://search.cpan.org/~tels/Math-BigInt-1.77/lib/Math/BigFloat.pm#Accuracy_vs._Precision) and Futurequest supports it. In CNC, the Server Info page has a link to the modules installed on your server.

rick-k
08-10-2005, 02:01 AM
Thanks for the suggestion. I'm intrigued. I do think I'll be taking another solution though. I found Math::FixedPrecision and it seems to be a more fitting solution. But FQ doesn't have it. I'm not sure I want to ask for it. I still get 300 times the error running it with my own Perl compared to doing a spreadsheet. That's an improvement from the 500x error I had last night, but I need the accuracy. It'll be a pain, but I'm probably going to make the files I need with a spreadsheet. I can at least use Perl to read the ephemeris files and start the spreadsheet.

Thanks again.

Wassercrats
08-10-2005, 02:18 AM
You can install whatever modules you need on FutureQuest, as far as I know.

I remember a year or two ago, just as I thought I knew enough Perl to get by, I accidently read that you can't do accurate math even to within a decimal point or two without a module. I kept that in mind for the future. I heard that on http://www.perlmonks.com . You might want to ask about the proper module over there. Tell them Wassercrats sent you and you'll get special treatment...or something.

Wassercrats
08-10-2005, 02:28 AM
And see this (http://perlmonks.com/index.pl?node_id=238531) thread.

rick-k
08-10-2005, 06:29 PM
Continuing to experiment with different solutions, I find that Math::FixedPrecision works after all if I change the equation a little bit. (At least it does running ActivePerl on my Windows XP machine at home.)

$n*$n*$n is handled differently than $n^3 even though they should be the same. I changed from the latter to the former and the inaccuracy is reduced to 5 times that of the spreadsheet. That's within the margin I want for professional-quality results.

Thanks for the input. So, how do I go about adding a module to FQ?

Bruce
08-10-2005, 06:34 PM
$n*$n*$n is handled differently than $n^3 even though they should be the same.Most likely this means that (x^y) is calculated as e^(log(x)*y). This allows for y to be any arbitrary number (not just integer), but is likely a fairly painful calculation to make on fixed point numbers.

Wassercrats
08-10-2005, 07:15 PM
So, how do I go about adding a module to FQ?I guess it's like any other script, but I'm rusty with Perl and I've never added a module to my Futurequest account. Maybe you'd have to call it as a package so you can specify the the location. Terra told me I could do it in this (http://www.aota.net/forums/showthread.php?postid=101373#post101373) post.

Wassercrats
08-10-2005, 07:27 PM
There's a way to specify the location of Perl modules. I don't know if I ever used it, but I found this:o USING MODULES NOT INSTALLED IN THE STANDARD LOCATION

You can explicitly tell perl where to look for modules by using the
lib module which comes standard with perl.

Example:

#!/usr/bin/perl

use lib "/home/users/dag/My_Local_Perl_Modules/";
use Bio::Seq;

<...insert whizzy perl code here...>

Wassercrats
08-10-2005, 07:29 PM
Melissa's about to tell you to read this (http://www.aota.net/forums/showthread.php?t=9656).

Melissa
08-10-2005, 07:31 PM
If a module you would like to use is not installed, you can email a request specifying the module and the reason you would like it installed to the Service Desk (Service@FutureQuest.net). The sysAdmin will then review the request.

If the module is not believed to be one that will be in demand by many others but is otherwise considered to be fine, the sysAdmin will grant permission to install it locally on your own account.

In that case, an excellent guide that was written by another FutureQuest Site Owner which should be helpful in completing the task can be found at:
http://www.aota.net/forums/showthread.php?s=&threadid=9656
(Please be sure to read it in its entirety as it contains a post by the sysAdmin as well.)

Melissa
08-10-2005, 07:32 PM
Melissa's about to tell you to read this (http://www.aota.net/forums/showthread.php?t=9656).LOL...stop spying...you're creeping me out :rasberry: