![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| 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 |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
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;
Please tell me. Last edited by Euler04; 10-18-2005 at 04:12 AM. |
| Forum Sponsor | ||
|
|
|
|||
|
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
|