Anonymous user
Decimal floating point number to binary: Difference between revisions
Decimal floating point number to binary (view source)
Revision as of 17:39, 1 November 2018
, 5 years ago→{{header|REXX}}: simplified some code; optimized several DO loops, elided dead code, added/changed comments and whitespace.
(Add Factor example) |
(→{{header|REXX}}: simplified some code; optimized several DO loops, elided dead code, added/changed comments and whitespace.) |
||
Line 1,147:
<lang rexx>/*REXX pgm converts any number in a base to another base including fractions; bases≤242.*/
parse arg number toBase inBase digits . /*obtain optional arguments from the CL*/
if toBase=='' | toBase=="," then toBase=
if inBase=='' | inBase=="," then inBase=
if digits=='' | digits=="," then digits=
if number=='' | number=="," then call err "no number specified."
if \datatype(toBase, 'W') then call err "invalid toBase: " toBase
Line 1,155:
if \datatype(digits, 'W') then call err "invalid digits: " digits
numeric digits max(digits, length(number)) + 5 /*use a bigger numeric decimal digits. */
$= base(number, toBase, inBase)
numeric digits digits /*use a smaller numeric digs*/
if toBase==10 then
say number ' (in base' inBase") = " $ ' (in base' toBase")."
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
base: procedure; parse arg x 1 s 2 1 ox,tt,ii,,oS
@#= 0123456789; @abc= 'abcdefghijklmnopqrstuvwxyz'; @abcu= @abc; upper @abcu
dontUse= @#'.+-'@abc || @abcu"0708090a0b0c0d"x; OK= @# || @abcu || @abc
$= OK || space( translate( xrange('1'x, "fe"x), , dontUse), 0) /*max base string
m= length($) - 1
if tt=='' then tt=
if ii=='' then ii=
i= abs(ii); t= abs(tt)
if t>m then call err 'invalid range for ToBase:' t"; the range is: " 2 m
if i>m then call err 'invalid range for InBase:' i"; the range is: " 2 m
!= substr($, 1 + 10 * (tt<0), t)
if tt<0 then != 0 || !
if x=='' then return left(!, t) /*Is X null? Show base chars*/
@=substr($, 1 + 10 * (ii<0), i) /*@: legal chars for base X.*/
if s='-' | s="+" then do;
end
if (ii>10 & ii<37) | (ii<0 & ii>-27) then upper x /*should X be uppercased ?
parse var x w '.' g /*sep whole from fraction. */
if pos('-', x)>0 |
pos(., g)>0 then call err 'illegal number: ' ox /*too many decimal points ? */
if _\==0 then call err 'illegal char in number:' ox 'char=' substr(__, _, 1)
if i\==10 then do
_=0; p=0; do j=length(w) to 1 by -1 while
_=
end
w=_; p=1; _=0
do
end
else if g\=='' then g= "."g
if t\==10 then do /*────────────────────────────────convert # base ten─►base T.*/
do
n=n || substr(!, 1 + w%_, 1); w= w // _
w=
if g\=='' then do; n=
if n==0 then
if n\=='' then n= '.'n /*only a
▲ if n==0 then n=
end
return oS || word( strip( space(w), 'L', 0)strip( strip(g, , 0), "T", .) 0, 1)
/*──────────────────────────────────────────────────────────────────────────────────────*/
err: say; say '***error***: ' arg(1); say; exit 13</lang>
|