<i>New function in "expr": sgn()</i> status still Open with 4 other changes
by
.
D 2012-05-28T18:08:46
J icomment is\ssgn()\smeant\sto\sbe\s"sign()"\sas\sin\sDKF's\sexample,\sor\sis\sit\smeant\sto\sbe\s"round\sto\sa\snumber\sof\ssignificant\sdigits"\sas\sdiscussed\sbelow?\n\nIn\sExcel\sfor\sexample,\s=FLOOR(1.134,\s0.01)\srounds\s1.134\sdown\sto\sthe\snearest\smultiple\sof\s0.01,\sgiving\s1.13.\s\sSimilarly\s=FLOOR(1.5,\s0.4)\sgives\s1.2.\s\sThe\s=ROUND()\sfunction\srounds\sto\sa\snumber\sof\sdigits,\se.g.\s=ROUND(1.135,\s2)\sgives\s1.14.\n\nThe\sExcel\sfunctions\scombine\sa\smathematical\soperation\sand\san\soutput\sformatting\soperation.\s\sThe\sresult\sof\s=FLOOR(1.134,\s0.01)\smay\snot\sbe\saccurately\srepresented\sby\sa\sfloat\sor\sa\sdouble,\sso\sthe\sfloating\spoint\svalue\sis\struncated\sfor\sdisplay\spurposes.\n\nRounding\sto\sa\ssignificant\snumber\sof\sdigits\s(as\sin\s=ROUND)\sas\sopposed\sto\san\sexact\ssignificance\s(as\sin\s=FLOOR)\sis\squite\seasy\sin\sTcl:\n\nproc\sround\s{d\ssgn}\s{\sformat\s%0.${sgn}f\s$d\s}\n\nComputing\sand\sdisplaying\sfloor\sor\sceiling\sto\sa\sspecific\sprecision\sis\smore\scomplicated:\n\nproc\sfloor\s{d\ssgn}\s{\s\n\s\sset\sprec\s[expr\s{\sint(ceil(-log($sgn)/log(10)))\s}]\n\s\sformat\s%0.${prec}f\s[expr\s{\sfloor($d\s/\s$sgn)\s*\s$sgn\s}]\s\n}\n\nA\scompromise\smay\sbe\sto\sextend\sround,\sfloor\sand\sceil\sto\stake\san\soptional\ssecond\sparameter\s(significance),\sand\simplement\sequivalent\sto:\n\nproc\sround\s{d\s{sgn\s1}}\s{\sexpr\s{\sround($d\s/\s$sgn)\s*\s$sgn\s}\s}\nproc\sfloor\s{d\s{sgn\s1}}\s{\sexpr\s{\sfloor($d\s/\s$sgn)\s*\s$sgn\s}\s}\n\nThis\sgives\sa\smathematically\saccurate\sanswer\s(within\sthe\sconstraints\sof\sfloating\spoint\sarithmetic),\sbut\sdoes\snot\sattempt\sto\sformat/truncate\sthe\soutput.\s\sThis\swould\sbe\sappropriate\sbehaviour\sfor\s[expr]\sin\smy\sopinion\s(but\sincluding\sformatting\sbehaviour\swould\snot\sbe\sappropriate).\n\n\n\n\n\n
J login twylite
J mimetype text/plain
J username twylite
K 3530004fffffffffffffffffffffffffffffffff
U twylite
Z eb208876792609ec6977957ffdc422a0