Anonymous user
Tropical algebra overloading: Difference between revisions
→{{header|REXX}}: simplified the showing of expressions, aligned output
Thundergnat (talk | contribs) m (→{{header|Raku}}: add constraint) |
(→{{header|REXX}}: simplified the showing of expressions, aligned output) |
||
Line 631:
'''REXX''' doesn't support operator overloading, so functions are needed to be used instead.
<lang rexx>/*REXX pgm demonstrates max tropical semi─ring with overloading: topAdd, topMul, topExp.*/
call negInf; @x= '(x)'; @a= '(+)'; @h= '(^)'; @e= 'expression'; @c= 'comparison'
numeric digits 1000 /*be able to handle negative infinity. */
x= 0 ; y= nInf ; say is(@x) LS(x)
x= -0.5 ; y= 0 ; say is(@x) LS(x)
x= 5 ; y= $Add(8,7); say is(@e) LS(x @x) RS(@a"(8,7)")
x= 5 ;
say blanks
x= 5; y= 7▼
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
ABnInf: if b=='' then b=a; __= negInf(); _= nInf(); return a==__ | a==_ | b==__ | b==_
notNum: call sayErr "argument isn't numeric or minus infinity:", arg(1) /*tell error.*/▼
negInf: negInf= '-1e' || (digits()-1); call nInf; return negInf /*simulate a -∞ value.*/
nInf: nInf= '-∞'; return nInf /*return the "diagraph": -∞ */
▲notNum: call sayErr "argument isn't numeric or minus infinity:", arg(1) /*tell error.*/
is: return 'max tropical' center(arg(1), 10) "of" /*center what is to be shown*/
LS: return right( arg(1), 12) ' with ' /*pad left─side of equation*/
RS: return left( arg(1), 12) ' ───► ' /* " right─side " " */
sayErr: say; say '***error***' arg(1) arg(2); say; exit 13 /*issue error message──►term*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
{{out|output|text= when using the internal default input:}}
<pre>
max tropical (x) of 2 with -2
max tropical (+) of -0.001 with -∞
max tropical (x) of 0 with -∞
max tropical (+) of 1.5 with -1
max tropical (x) of -0.5 with 0
max tropical (^) of 5 with 7
max tropical expression of 5 (x) with
max tropical expression of
max tropical comparison of
</pre>
|