Wednesday, December 31, 2008

Looping in Hex

In Linux, you have seq utility (btw, I realised OpenSolaris 200811 comes with seq) that can print a sequence of numbers. This is a very useful command especially if you need to do loops. With seq, you can avoid doing any increment of counters. You can do this with
for i in `seq 1 10`; do echo $i; done

However, seq can only handle decimal (base 10) input arguments. What if I want seq to do stuff in hex (base 16), eg. loop from 00 to FF in step of 5. At first I was thinking of creating two functions (hex2dec and dec2hex) so that I can do this:

  1. input arguments in hex convert to decimal (use hex2dec)
  2. increment the looping counter in decimal
  3. convert any ouput to hex (use dec2hex)

The above steps work fine but it will involve too many processes to be forked out from the parent shell. Although I was using bc (arbitrary precision calculator language) to do conversion between base10 and base16, I did not explore the looping within the bc.

Do you know that bc language provides "for" loop construct ? Now I can bring my loop out of the shell script into bc and I do not have to do any counter increment. Since bc cannot handle both ibase=16 and obase=16 together, I need to convert the input hex number to decimal and let bc to work out all the output in hex. I even include an optional -w flag to equalise the width by padding with leading zeroes.

Here is my seq2hex.sh

#! /bin/sh


hex2dec()
{
    hex=`echo $1 | tr '[a-z]' '[A-Z]'`
    echo "ibase=16;$hex" | bc
}


usage()
{
    echo "Usage: $0 [-w width] start end"
    echo "Usage: $0 [-w width] start step end"
}
    

width=""
while getopts w: c
do
    case $c in
    w)   width=$OPTARG ;;
    \?)  usage; exit 1  ;;
    esac
done
shift `expr $OPTIND - 1`


# input arguments in hex, convert to decimal
if [ $# -eq 2 ]; then
    start=`hex2dec $1`
    end=`hex2dec $2`
    step=1
elif [ $# -eq 3 ]; then
    start=`hex2dec $1`
    step=`hex2dec $2`
    end=`hex2dec $3`
else
    usage
    exit 2
fi


echo "obase=16; for(i=$start;i<=$end;i+=$step) { i }" | \
    bc | \
    awk '{printf("%0'$width's\n",$1)}'

seqhex.sh in action:

$ ./seqhex.sh
Usage: ./seqhex.sh [-w width] start end
Usage: ./seqhex.sh [-w width] start step end

$ ./seqhex.sh 6 F
6
7
8
9
A
B
C
D
E
F

$ ./seqhex.sh AA BB
AA
AB
AC
AD
AE
AF
B0
B1
B2
B3
B4
B5
B6
B7
B8
B9
BA
BB

$ ./seqhex.sh AA 5 BB
AA
AF
B4
B9

$ ./seqhex.sh -w 4 AA 5 BB
00AA
00AF
00B4
00B9

Labels: ,

Friday, June 01, 2007

Maths Olympics

Recently my daugther attended the Mathematics Olympics for Secondary 2. The questions are very tough if you have not been exposed to the type of questions before. One of the questions is pretty interesting.

Find the latest integer n so that n6021 < 20072007.

Not many people know how big is 20072007. If you use bc (arbitrary precision arithmetic language) to calculate it, it is a 6629 digit number.

So how can we approach this problem without having to deal with 6000+ digits.
6021 is 3 times of 2007, so we can express
n6021 as
n32007
Using the trail and error method, we can tabulate the result as:
n=10, n3=1000 < 2007
n=11, n3=1331 < 2007
n=12, n3=1728 < 2007
n=13, n3=2197 > 2007

So, the answer is 12

$ echo 2007^2007 | bc
16314605714687673100219161988221590518292210898740437841876506892750\
32306039423940369656182671626918374428665007728310641726207038681918\
23136702404167821902979921698186677089033622516321277953102421415380\
52881817063065329725414186570424721165880384220836219727730342712190\
37547038078493298854228413412450765335802752810679631982070064711707\
56994904826360657191473471275712342626409252812000487654952457462581\
25202397471862500575253203957986559953029179221497185241787894245109\
51506284252195025848010707100693306754335210537773553923807001377973\
58190841023173651478340362529204455978523734694649333587437048551880\
84344987860972778473042539830626220900910030243133077748096636794353\
16457852288885788183704321212603620156876288242227673033902489461859\
80807205983674431470103187228509985963442849376025881515659139204354\
57334048169514769124351395926915548900005107939870885748926502983652\
48220739239828923170534565961757683913268494073903341978940401041341\
24158904437217633744227156627420692310366588876904474906119640656336\
00909055629673774341172101831114453521702586164711667890001015971356\
60276401463142043929976624709724328792829957422043087537178795758647\
72185461767393859241999232092091753999161030318269214754924571963159\
35520624812251694955876845371629366273520636698693605424704420404631\
75277434551058391143650208928358475502339174967420269679902664658114\
23441325364666899042860424269755985756717730999047598611204004019487\
05037492513989337593522479668752257755675941902099185237554143748651\
76084285432698562312888918263670859517256741096260219051804661166619\
77156782563640767810527747055628531062056538632420611318856864435605\
98573568085436649055893711511908754786711917259735123805058932226875\
09020048745112007771764955307557550322001919379536452269156637570639\
67686895988403526418230539376546037003876670242151992373967447858965\
32371190719225599092102935960432876642206996867191908197903254065184\
66772118566829478154758078361829805339842140536877388530591895758845\
74615755305866464237994838787908227762252835497894144677604759903050\
56214887675010008762806122865755121275564759677904950783893207351021\
21793547077526595680554400368459226450944178849240467865779814317007\
96876505317193541397691960050013423154588306446442511274723228366073\
99740758368268646735204080379101950673539512645517752083915627087436\
66342779749004724047925157702126529475456581827940549061219833542771\
72666791641130825385603816826728793107694318470910676883660956902086\
59579147945702522853473552635534140382309303777950425443446724647072\
84039885465320015844579846408011500806127198942953456479287429126124\
58187025149566132957578262005878966556171671947012422179587596926224\
48413221909265362849341101538896719374297181506945626681830899361082\
17588258048154216467741811634089431627204908200906712352236452851164\
60870051555430826004956456747145900922832888740550701604291930516794\
53804271088240995714495287536761378107585304471702025690381237435699\
62867717292953981124669065011683502890758991080085205157633970212891\
98777309368701179857410923501969865110478773653903699197912297908533\
71149722723292678177205635678230292598408904569266566529096965940687\
19800187410667900893753388917161120090709121225671327994837556358866\
13795364895358454179682944936819599409259644069296990066176672218326\
10569446092009445906651399279429326070744029719496103157306940293382\
08652605749532755522690122699195850782355410608547470555886945614660\
28849763565012828241503887371454415825187174123047124428965559186350\
65661067953888197801888330944283778895151118014938632162825657485422\
26873379147497009487850977528543889031689799484755702907033188341688\
57086162026884996378694574719417715017198218671864779969442373083947\
84224584936854774880083798459580587181441861962025043001696668942099\
98369361531152251402670787887047071049415711093008135194582239773277\
57730018467737893821357739016393737981517063929164948629278989863490\
66462204249411800881288288291970583303627128619570872408141516094456\
83837112137017595507346794006856000647005501898922929846153939218796\
28234593579234420657377664780055273852674449396670561737910057331770\
53405452811154572480773839659370346376909955589954534463995402397547\
55090554901741064115038670213867607618630149352412903118245252687275\
28109561678498436676798923797165313359732911821400623859409924152069\
06300260638828041177167702847188712306493492372142866618404908913176\
54030952366549559176082480786655059388809520507182637615683247714988\
05191492633132029599544921486661970299947588786678189871088966477407\
52512996563850623228109536022666509972476172556459154412193755524859\
28452331588277656632220424016664113248809498813370962561820162791582\
64121448823383689967724103592671517706815568744426523608840138911714\
53958944518234290650299585576981492919941107102609478933946462619302\
48865632368726622732906937659050660925986430541337820914319653668221\
87349651540078364469161738550494782857953599406614823850511779698786\
91838865273359122035277482634624991021675924624312254360293884842917\
65838307747253780974059548132381415125202990686958323920598730920623\
83252041705054542282692895103797418414876506955754092950593171511526\
03651256685226830058555083998194070710847307757088197896172035753443\
57325024752384875269065693702994828638438768496534182574105672247872\
16889073273120095695064749134557169837596440817787277202590296656949\
48573652672557136771335615479698964295228224311865290533314649278929\
66378562879105977303922021842036171103420525191259589291180958162828\
53849025184090592088836475653212594663891594930011800278301810320167\
71271739342026383819163505909340986688942636949341034442899607560777\
48743959210409197647213006839217933959866202483201890809519278801288\
89774489640070710220962719764159839606000869526896455498122625138520\
03473593771023384202960841337266822055244894318115626747268831051684\
93368590796032830924999538553468389382057082607007101680887865179417\
56230281766141812283600778826785130821021267795745725254521386017285\
26298885351305296325067178476137349370135741313308883039679816779268\
89819182812687810885966489071842746337245202749132624149354170939165\
16259303899012457012515608202326844412337496478945032118391923014778\
60791546940874717049537071793531373218290246791145310292221059372892\
64816915738818462542773732790559090793017150142535888474230492964601\
87531820919736515493904236213757675608136303680989586717189185046309\
78276773061725084597786832034498958720078262844355265962017842479132\
62269519023741579612330789171572662737099118044136909873639373744006\
57859538070736932317321882339952581950548422144343493289702293960474\
93455994166543113414231137214737093262742407103151085798658468378983\
026266371632383860581109563509543
To count how many digit there, we need to get rid of the backslash and the newline
$ echo 2007^2007 | bc | sed -e 's/\\//' | awk '{printf("%s",$1)}' | wc -c
6629

Labels: ,

Thursday, June 01, 2006

Reliable Computing, scripting language perspective

I am happy that I attended the Sun HPC Consortium Singapore 2006. This was held in conjunction with GridAsia 2006.

One of the presentations (Reliable Computing and Petascale systems, by Ruud van der Pas, Sun Microsystems) interested me to explore further, from the viewpoint of scripting languages. Although his presentation material is not available, I managed to find a similar paper that he presented last year in Australia.
The Future Of Floating-Point Computing Is In Reliable Computing, in Australian Partnership for Advanced Computing Conference and Exhibition on Advanced Computing, Grid Applications and eResearch, 26-30 September 2005.

The equation in question is:
F(a,b) = (333.75-a^2)*b^6 + a^2*(11*a^2*b^2-121*b^4-2) + 5.5*b^8 + a/(2*b)
a = 77617.0; b = 33096.0

The answer (using 32-bit precision): 1.172604. Question: How Good Is This Result?

In thr APAC 2005 paper, it said:

"If we bump up the precision and the numbers match, we're okay" Not Necessarily .......

% ./rump.exe
Computed Results
32-bit precision: 1.172604
64-bit precision: 1.1726039400531786
128-bit precision: 1.1726039400531786949244406059733592


% ./rump.exe
Computed Results
32-bit precision: 1.172604
64-bit precision: 1.1726039400531786
128-bit precision: 1.1726039400531786949244406059733592
Correct Results
32-bit precision: -0.82739603
64-bit precision: -0.8273960599468213
128-bit precision: -0.8273960599468213050755593940266408

Okay, let's look at how scripting languages handle the above equation on my notebook running Cygwin:

Perl (5.8.7)
[ans=1.17260394005318] NOT CORRECT

$ cat rump.pl
#! /usr/bin/perl

$a=77617.0;
$b=33096.0;
$a2=$a*$a;
$b2=$b*$b;
$b4=$b2*$b2;
$b6=$b4*$b2;
$b8=$b6*$b2;
$ans=(333.75-$a2)*$b6 + $a2*(11*$a2*$b2-121*$b4-2) + 5.5*$b8 + $a/(2*$b);
print $ans;

$ perl rump.pl
1.17260394005318

Tcl (8.4.1)
[ans=1.17260394005] NOT CORRECT

$ cat rump.tcl
#! /usr/bin/tclsh

proc f { a b } {
        set a2 [expr {$a*$a}]
        set b2 [expr {$b*$b}]
        set b4 [expr {$b2*$b2}]
        set b6 [expr {$b4*$b2}]
        set b8 [expr {$b6*$b2}]
        set ans [expr {(333.75-$a2)*$b6 + $a2*(11*$a2*$b2-121*$b4-2) +                 5.5*$b8 + $a/(2*$b)}]
        return $ans
}
puts [f 77617.0 33096.0]

$ ./rump.tcl
1.17260394005

Apparently all these calculations are based on IEEE floating point standard. If we were to calculate it long hand, it looks like this:

(333.75 - a^2)*b^6=
-7917110903377385049079188237280149504.00

a^2*(11a^2*b^2-121*b^4-2)=
       -437291576312021946464244793346.00

5.5*b^8=
 7917111340668961361101134701524942848.00

a/(2*b)=
                                     1.17260394005317863185
-------------------------------------------------------------------------------
F(a,b)=
                                    -0.8273960599468213681411650954798162919991


-7917110903377385049079188237280149504.00 +
-437291576312021946464244793346.00 +
7917111340668961361101134701524942848.00 = 2

Can we do it correctly with scripting languages (if we consider "bc" also) ? Let see

bc (1.06) [ANS=-.8273960599468213681411650954798162919991] CORRECT

$ cat rump.bc
scale=40
a=77617.0
b=33096.0
(333.75-a^2)*b^6+a^2*(11*a^2*b^2-121*b^4-2)+5.5*b^8+a/(2*b)

$ bc -l < rump.bc
-.8273960599468213681411650954798162919991
Perl (5.8.7) (same script as above, but run with Bignum module)
[ans=-0.827396059946821368141165095479816291999] CORRECT
$ cat rump.pl
#! /usr/bin/perl

$a=77617.0;
$b=33096.0;
$a2=$a*$a;
$b2=$b*$b;
$b4=$b2*$b2;
$b6=$b4*$b2;
$b8=$b6*$b2;
$ans=(333.75-$a2)*$b6 + $a2*(11*$a2*$b2-121*$b4-2) + 5.5*$b8 + $a/(2*$b);
print $ans;

$ perl -Mbignum rump.pl
-0.827396059946821368141165095479816291999
Tcl (math::bigfloat)
[ans=-0.827396059946821368141165095479816291999] CORRECT
The tricky part is how much precision one has to set to get the correct answer. Also, the implementation is very clumsy.
package require math::bigfloat
namespace import math::bigfloat::*

set precision 77
set a [fromdouble 77617.0 $precision]
set b [fromdouble 33096.0 $precision]

set c333_75 [fromdouble 333.75 $precision]
set c11 [fromdouble 11.0 $precision]
set c121 [fromdouble 121.0 $precision]
set c2 [fromdouble 2.0 $precision]
set c5_5 [fromdouble 5.5 $precision]

set a2 [mul $a $a]
set b2 [mul $b $b]
set b4 [mul $b2 $b2]
set b6 [mul $b4 $b2]
set b8 [mul $b6 $b2]

set term1 [mul [sub $c333_75 $a2] $b6]
set term2 [mul $a2 [sub [sub [mul [mul $c11 $a2] $b2] [mul $c121 $b4]] $c2]]
set term3 [mul $c5_5 $b8]
set term4 [div $a [mul $c2 $b]]

set ans [add [add [add $term1 $term2] $term3] $term4]
puts [tostr $ans]
Tcl (Mpexpr)
[ans=-0.827396059946821368141165095479816291999] CORRECT
Tcl module (Mpexr ) is able to do multiple precision arithmatic. Below is Tcl 8.1.1 with Mpexr on Solaris, because I have problem compiling that with the latest Tcl8.4 on Cygwin
$ cat rump.tcl
package require Mpexpr
proc f { a b } {
        set a2 [mpexpr {$a*$a}]
        set b2 [mpexpr {$b*$b}]
        set b4 [mpexpr {$b2*$b2}]
        set b6 [mpexpr {$b4*$b2}]
        set b8 [mpexpr {$b6*$b2}]
        set ans [mpexpr {(333.75-$a2)*$b6 + $a2*(11*$a2*$b2-121*$b4-2) +                 5.5*$b8 + $a/(2*$b)}]
        return $ans
}
set mp_precision 40
puts [f 77617.0 33096.0]

$ tclsh rump.tcl
-0.827396059946821368141165095479816291999

Conclusion

Scripting languages can be deployed in the area of reliable computing, but one has to be mindful in choosing the right modules for the job. BTW, I am very impressed by Perl's Bignum implementation which hides all the technicality from the user. The same perl script can run with or without Bignum module.

Labels: , , , ,