Temperature conversion/REXX: Difference between revisions

m
→‎{{header|REXX}}: unpacked some functions/subroutines, added blocked characters, added/changed whitespace and comments.
m (→‎{{header|REXX}}: elided DOS' interjected section symbol from MORE command (from a cut-n-paste).)
m (→‎{{header|REXX}}: unpacked some functions/subroutines, added blocked characters, added/changed whitespace and comments.)
Line 2:
 
=={{header|REXX}}==
<lang rexx>/*REXX program converts temperatures for fifty─seven different temperature scales. */
 
 
/*────────────────────────────────────────────────────────────────────────────────────────────────
 
tt tt
tt tt
tttttt tttttt
tttttt eeeee mmmm mmm ppppp eeeee rr rrr aaaa tttttt uu uu rr rrr eeee
tt eeeeeee mmmmmmmmmm pppppp eeeeeee rrrrrrr aaaaa tt uu uu rrrrrrr eeeeeee
tt ee ee mm mm mm pp pp ee ee rrr rr aa tt uu uu rrr rr ee ee
tt eeeeeee mm mm mm pp pp eeeeeee rr aaaaaa tt uu uu rr eeeeeee
tt eeeeeee mm mm mm pppppp eeeeeee rr aaaaaaa tt uu uu rr eeeeeee
tt tt ee mm mm mm ppppp ee rr aa aa tt tt uu uu rr ee
ttttt eeeeee mm mm mm pp eeeeee rr aaaaaaa ttttt uuuuuuu rr eeeeee
ttt eeee mm mm mm pp eeee rr aaaaaaa ttt uuuu uu rr eeee
pp
 
────────────────────────────────────────────────────────────────────────────────────────────────*/
 
 
 
call e /*let's see the precision we can use. */
Line 12 ⟶ 32:
do until tList='' /*process the list of temperatures. */
parse var tList x ',' tList /*temperatures are separated by commas.*/
x= translate(x, '((', "[{") /*support other grouping symbols. */
x= space(x) /*elide any and all superfluous blanks.*/
parse var x z '(' /*handle any comments (if there're any)*/
Line 55 ⟶ 75:
say right(' ' x, 79, "─") /*show original value & scale (for sep)*/
 
call convert2specific /*convert Fahrenheit ──► specific temp.,*/
end /*until tlist*/ /*this is a biggish DO loop. */
 
Line 63 ⟶ 83:
 
/*──────────────────────────────────────────────────────────────────────────────────────*/
$: procedure; showDig=8 8 /*only show 8 significant decimal digs.*/
_= commas( format( arg(1), , showDig ) / 1 ) /*format# 8 digits past . and add comma*/
p= pos(., _) /*find position of the decimal point. */
/* [↓] align integers with FP numbers.*/
if p==0 then _= _ || left('', 5 + showDig + 1) /*no decimal point.*/
else _= _ || left('', 5 + showDig - length(_) + p) /*has " " */
 
return right(_, max(60, length(_) ) ) /*return the re─formatted argument (#).*/
 
 
Line 161 ⟶ 181:
if ?('DALENCE') then say $( ( F - 59 ) / 2.7 ) "Dalence"
 
if ?('DALTON') then if kK>a then say $(100*ln(k/273.15)/ln(373.15/273.15) ) "Dalton"
else say $right("-infinity ", 60) "Dalton"
 
if ?('DANIELL') then say $( ( F - 55.9994 ) / 7.27194 ) "Daniell"
Line 215 ⟶ 235:
 
/*──────────────────────────────────────────────────────────────────────────────────────────────────*/
-- More --
scaleName: parse arg y /*abbreviations ──► temp. short name.*/
yU= translate(y, '-eE', "_éÉ") /*translate some accented characters. */
Line 404 ⟶ 425:
 
 
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*────────────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg y
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); m.=9; numeric form; h=d+6
if not\=='' then do
numeric digits; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ % 2
do j=0 while h>9; if m.jnoS\=h;="" then if left(y, hnoL)==noS h%2 +then 1;return end /*j*/0
do k=j+5 if tonoE\=='' 0 then byif -1;right(y, numeric digits m.k; gnoL)==noE (g+x/g)*.5; then return end /*k*/0
return g/1 end
 
if all | y==! then return 1
return 0
 
 
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: procedure; parse arg _ /*insert commas in a number. */
n= _'.9' /*added suffix for VERIFY BIF.*/
#= 123456789 /*a nifty handy-dandy literal.*/
b= verify(n, #, "M") /*find beginning of a number. */
e= verify(n, #'0', , verify(n, #"0.", 'M') ) - 4 /* " end " " " */
 
do j=e to b by -3 /*insert commas right─to─left.*/
_= insert(",", _, j) /*insert a comma every period.*/
end /*j*/
 
return _
 
 
/*──────────────────────────────────────────────────────────────────────────────────────*/
sqrt: procedure; parse arg x /*obtain the target of SQRT. */
if x=0 then return 0 /*Argument is zero? Return 0.*/
/*This function work for zero.*/
d= digits()
m.= 9 /*get number of decimal digits*/
h= d+6 /*add six for safety reasons. */
numeric form /*ensure correct form of #'s. */
numeric digits /*start with nine numeric digs*/
 
/*a way of getting the expon. */
/*No exponent? Then add one. */
numeric digits; parse value format(x, 2, 1, , 0) 'E0' with g "E" _ .; g=g *.5'e'_ % 2
/* [↑] half the exponent. */
g=g *.5'e'_ % 2 /*a first best guess for sqrt.*/
 
/* [↓] use min number of dec.*/
/* digs for early SQRTs. */
do j=0 while h>9
m.j= h /*calculate # of digits to use*/
h= h % 2 + 1
end /*j*/
 
do k=j+5 to 0 by -1 /*calculate higher precision. */
numeric digits m.k /*bump the decimal digits. */
g= (g + x / g) * .5 /*calculate SQRT approximation*/
end /*k*/
return g / 1 /*this normalizes the sqrt #. */
 
 
/*──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
?: parse arg y; if not\=='' then do; if noS\=="" then if left(y, noL)==noS then return 0; if noE\=='' then if right(y, noL)==noE then return 0; end; if all | y==! then return 1; return 0
e: e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932; return e /*112 useful decimal digits. */
commas: procedure; parse arg _; n=_'.9'; #=123456789; b=verify(n,#,"M"); e=verify(n,#'0',,verify(n,#"0.",'M'))-4; do j=e to b by -3; _=insert(",",_,j); end /*j*/; return _
e: e = 2.718281828459045235360287471352662497757247093699959574966967627724076630353547594571382178525166427427466391932; return e
isInt: return datatype(arg(1), 'W') /*is the argument a whole number (integer)?*/
exp: procedure; parse arg x; ix=x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x=x-ix; z=1; _=1; w=z; do j=1; _=_*x/j; z=(z+_)/1; if z==w then leave; w=z; end; if z\==0 then z= z * e()**ix; return z/1
ln: procedure; parse arg x; call e; ig=x>1.5; is=1-2*(ig\==1); ii=0; xx=x; return ln..()
ln..: do while ig & xx>1.5 | \ig & xx<.5;_=e;do k=-1;iz=xx*_**-is;if k>=0 & (ig & iz<1 | \ig & iz>.5) then leave;_=_*_;izz=iz;end;xx=izz; ii=ii+is*2**k; end; x=x*e**-ii-1; z=0;_=-1;p=z;do k=1; _=-_*x;z=z+_/k; if z=p then leave;p=z; end; return z+ii
Line 423 ⟶ 490:
pow.: if abs(y//1)=.5 then return sqrt(x)**sign(y)*x**(y%1); return exp(y*ln(x))
root: procedure; parse arg x 1 ox,y 1 oy; if x=0 | y=1 then return x; if isInt(y) then return rooti(x,y); _=sqrt(x); if y<0 then _=1/_; return _
rooti: x=abs(x); y=abs(y); a= digits() + 5; g=rootIg(); m= y-1; d=5; do until d==a; d=min(d+d, a); numeric digits d; o=0; do until o=g; o=g; g=format( (m*g**y+x) /y/g**m, , d-2); end; end; _= g * sign(ox); if oy<0 then _= 1/_; return _
rootIg: numeric form;parse value format(x,2,1,,0) 'E0' with ? 'E' _ .; return (? / y'E'_ % y) + (x>1)
s: if arg(1)==1 then return arg(3); return word(arg(2) 's',1) /*pluralizer.*/