The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Pack current folder WebWatch UNIX for Dummies Questions & Answers 3 12-17-2007 01:46 AM
Aix - Service Pack BabylonRocker AIX 1 10-18-2006 01:54 AM
Perl + pack() + spaceing question Optimus_P Shell Programming and Scripting 1 10-02-2002 07:03 AM
How use #pragma pack() in HP unix ? bdyjm High Level Programming 1 03-04-2002 05:26 AM
HP-UX UNIX Software v.10.20 Pack normreeves UNIX for Dummies Questions & Answers 1 01-07-2002 09:59 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-18-2005
Registered User
 

Join Date: Jul 2005
Posts: 31
Stumble this Post!
Perl help!! (pack()?)

Hello everyone.

I wrote a perl script to get the two answers from a value: x.
By this, I want to do sqrt($x) in different precision.
Code:
#!/usr/bin/perl

print "Input the initial value x:\n";
chomp($x=<STDIN>);
$comp=sqrt($x);
$float_value=pack("f", $comp);
$double_value=pack("d", $comp);
print "The answer by flaot is $float_value.\n";
print "The answer by double is $double_value.\n";
exit;
I don't know how to use "pack" function.
Please tell me.

Last edited by Euler04; 10-18-2005 at 04:12 AM.
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 10-18-2005
Moderator
 

Join Date: Sep 2002
Location: Hong Kong, China
Posts: 1,432
Stumble this Post!
No, you don't use pack() to do it. It packs the number in a binary representation that is never portable. And it's not printable.

Just use sprintf() and it should be fine. You can cast it to arbitrary precision as needed (provided that is supported):

Code:
D:\Documents and Settings\bernardchan>perl -e "print sqrt(5);"
2.23606797749979
D:\Documents and Settings\bernardchan>perl -e "print sprintf('%f', sqrt(5));"
2.236068
D:\Documents and Settings\bernardchan>perl -e "print sprintf('%.3f', sqrt(5));"
2.236
I can use printf() directly - just to show you that sprintf() returns a string representation that can be assigned to a scalar variable for later printing, storage or other processing.
Reply With Quote
  #3 (permalink)  
Old 10-18-2005
Registered User
 

Join Date: Jul 2005
Posts: 31
Stumble this Post!
Thank you very much, cbkihong.
I understood a lot.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 08:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0