<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'>
<div>Folks might find this amusing or useful.</div><div> I actually think I'm going to use it, just for the numbers 0-64.</div><div>It implements addition and subtraction for a fixed range, here 0-9999</div><div>Global variables are only used for initialization.</div><div>Thereafter, just global constants (no enforcement of that, but I think it could be done by having initialization return them)</div><div>.</div><div><br></div><div>Approach is that adding 1 to a single digit can be done with a small map.</div><div>Adding 1 to multiple digits can be done using multiple variables and checking for "wraparound" to 0 (the table maps 9 to 0).</div><div>You count up to 9999, filling in an Add1 and Sub1 hash table as you go up.</div><div> I don't think quake has an appropriate looping mechanism, so counting up to 9999 is done recursively.</div><div>Then Add and Sub are implemented recursively like Add1{Add(a, Sub1(b)}.</div><div><br></div><div><br></div><div><br></div><div>local Add1 = { }</div><div>local Sub1 = { }</div><div><br></div><div><br></div><div>local counter0 = 0</div><div>local counter1 = 0</div><div>local counter2 = 0</div><div>local counter3 = 0</div><div><br></div><div><br></div><div>local proc Increment() is</div><div> local inc = { 0:1,1:2,2:3,3:4,4:5,</div><div> 5:6,6:7,7:8,8:9,9:0 }</div><div> counter0 = inc{counter0}</div><div> if equal(counter0, 0)</div><div> counter1 = inc{counter1}</div><div> if equal(counter1, 0)</div><div> counter2 = inc{counter2}</div><div> if equal(counter2, 0)</div><div> counter3 = inc{counter3}</div><div> end</div><div> end</div><div> end</div><div> local c = ""</div><div> foreach d in [counter3, counter2, counter1, counter0]</div><div> if not equal(d, 0) or not equal(c, "")</div><div> c = c & d</div><div> end</div><div> end</div><div> return c</div><div>end</div><div><br></div><div><br></div><div>proc InitMath_F1(a, b) is</div><div> if equal(a, "9999")</div><div> return</div><div> end</div><div> Add1{a} = b</div><div> Sub1{b} = a</div><div> InitMath_F1(b, Increment())</div><div>end</div><div> </div><div><br></div><div>proc InitMath() is</div><div> counter0 = 0</div><div> counter1 = 0</div><div> counter2 = 0</div><div> counter3 = 0</div><div> InitMath_F1(0, Increment())</div><div>end</div><div><br></div><div><br></div><div>proc Add(a, b) is</div><div> if equal(a, 0) return b end</div><div> if equal(b, 0) return a end</div><div> if equal(a, 1) return Add1{b} end</div><div> if equal(b, 1) return Add1{a} end</div><div> return Add1{Add(a, Sub1{b})}</div><div>end</div><div><br></div><div><br></div><div>proc Sub(a, b) is</div><div> if equal(a, 0) return b end</div><div> if equal(b, 0) return a end</div><div> if equal(a, 1) return Sub1{b} end</div><div> if equal(b, 1) return Sub1{a} end</div><div> return Sub1{Sub(a, Sub1{b})}</div><div>end</div><div><br></div><div><br></div><div>InitMath()</div><div>write("99 - 40 is " & Sub(99, 40), CR)</div><div>write("6 - 4 is " & Sub(6, 4), CR)</div><div>write("3 + 2 is " & Add(2, 3), CR)</div><br><br> </body>
</html>