Non-decimal radices/Convert: Difference between revisions

→‎{{header|REXX}}: added comments, added whitespace, changed some comments, added support for a leading sign. -- ~~~~
(→‎{{header|Perl 6}}: note that Int is a subtype of Real in Perl 6)
(→‎{{header|REXX}}: added comments, added whitespace, changed some comments, added support for a leading sign. -- ~~~~)
Line 1,244:
Instead of writing two seperate routines, only one was written to handle both tasks.
<br><br>This routine was ripped out from a bigger version of mine that allowed any number as input, including decimal (or whatever base) fractions.
<lang rexx>/*REXX program converts numbers from one base to another, from 2 ──>──► 90.*/
/*┌────────────────────────────────────────────────────────────────────┐
┌─┘ Input to this program (allbases must be positive whole numbers): └─┐
│ │
│ x (is required). (it may have a sign).
│ toBase the base to convert X to. │
│ inBase the base X is expressed in. │
│ │
toBase or inBase can be a comma (,) which causes the default
└─┐ of 10 to be used. The limits of bothbases are: 2 ──>──► 90. ┌─┘
└────────────────────────────────────────────────────────────────────┘*/
@abc='abcdefghijklmnopqrstuvwxyz' /*randomlowercase characters,Latin sortedalphabet. */
@abcU=@abc; upper @abcU /*go whole hog and extend 'em. */
@@@=0123456789||@abc||@abcU /*prefix 'em with numeric digits.*/
@@@=@@@'<>[]{}()?~!@#$%^&*_+-=|\/;:~¢¬≈' /*add some special chars as well.*/
/*spec. chars should be viewable.*/
numeric digits 1000 /*what the hey, support gihugeics*/
maxB=length(@@@) /*max base (radix) supported here*/
parse arg x toB inB . 1 ox . /*get a number, toBase, inBase */
if toB=='' | toB==',' then toB=10 /*if skipped, assume default (10)*/
if inB=='' | inB==',' then inB=10 /* " " " " " */
Line 1,268:
if toB<2 | tob>maxB then call erb 'toBase',toB /*bad boy toBase.*/
if x=='' then call erm /*bad boy number.*/
sigX=left(x,1); if pos(sigX,"-+")\==0 then x=substr(x,2) /*X has sign?*/
/*──────────────────────────────────convert X from base inB ──> base 10.*/
else sigX= /*no sign. */
#=0
#=0; do j=1 for length(x) /*convert X, base inB ──► base 10*/
_=substr(x,j,1) /*pick off a "digit" from X. */
v=pos(_,@@@) /*get the value of this "digits".*/
if v==0 |v>inB then call erd x,j,inB /*illegal "digit"? */
#=#*inB+v-1 /*construct new num, dig by dig. */
end /*j*/
 
/*──────────────────────────────────convert # from base 10 ──> base toB.*/
y=; do while #>=toB /*convert #, base 10 ──► base toB*/
y=''
do while y=substr(@@@,(#>=//toB )+1,1)y /*deconstructconstruct the newoutput number. (#).*/
y=substr(@@@,(#//toB)+1,1)y /*construnct the output number. */
#=#%toB /*... and whittle # down also. */
end /*while #>-toB*/
 
y=sigX || substr(@@@,#+1,1)y
say xox "(base" inB')' center('is',20) y "(base" toB')' /*show & tell.*/
exit /*stick a fork in it, we're done.*/
exit
/*──────────────────────────────────error subroutines───────────────────*/
ermerb: call ser; say 'noillegal' argumentarg(2) specified.';"base:" callarg(1) "must be in range: 2──►" erxmaxB
erd: call ser; say 'illegal "digit" in' x":" _; call erx
sererm: saycall ser; say '***no error!argument ***specified.'; say;call returnerx
erx: say; exit 13
erb: call ser; say 'illegal' arg(2) "base:" arg(1) "must be in range: 2──>" maxB
erxser: say; exitsay '*** error! ***'; say; 13return</lang>
'''output''' when input is (maximum positive integer in a signed 32-bit word): <tt> 7fffffff , 16 </tt>
<pre style="height:5ex;overflow:scroll">
7fffffff (base 16) is 2147483647 (base 10)
</pre>
'''output''' when input is: <tt> 4095 2 </tt>
<pre style="height:5ex;overflow:scroll">
4095 (base 10) is 111111111111 (base 2)
</pre>
'''output''' when input is: <tt> 100 3 2 </tt>
<pre style="height:5ex;overflow:scroll">
100 (base 102) is 1020111 (base 3)
</pre>
'''output''' when input is: <tt> zombieseatingdeadvegatables 10 36 </tt>
<pre style="height:5ex;overflow:scroll">
zombieseatingdeadvegatbles (base 36) is 28842619147225718605337632731806510179876 (base 10)
</pre>