Forth: Difference between revisions
Jump to navigation
Jump to search
m (→sample: g typo) |
m (→sample: nsqrt) |
||
| Line 67: | Line 67: | ||
t c <7> -1 128 65 34 20 16 16 -1 ok | t c <7> -1 128 65 34 20 16 16 -1 ok | ||
</pre> | </pre> | ||
'''integer square root''' | |||
<pre> | |||
: a + 2/ ; | |||
: sq dup * ; | |||
: sc 2dup sq < if 0 else 2dup 1+ sq < then ; | |||
: nsqrt 1 begin sc if swap drop exit then 2dup / a again ; | |||
100 nsqrt . | |||
</pre> | |||
result <code>10</code> | |||
== links == | == links == | ||
Revision as of 19:29, 14 October 2017
sample
2 + 2
2 2 + .
result 4
9 % 4
9 4 /mod .s
result 1 2
square word
: sq dup * ; 2 sq .s
result 4
hello world
: hw ." hello world" ; hw
result hello world
integer square root
-1 variable x 256 x ! variable g 1 g ! : r x @ g @ / ; : a g @ + 2/ g ! ; : t r a g @ .s ; : c over over = . ; t c ... t c
result
t c <2> -1 128 0 ok t c <3> -1 128 65 0 ok t c <4> -1 128 65 34 0 ok t c <5> -1 128 65 34 20 0 ok t c <6> -1 128 65 34 20 16 0 ok t c <7> -1 128 65 34 20 16 16 -1 ok
integer square root
: a + 2/ ; : sq dup * ; : sc 2dup sq < if 0 else 2dup 1+ sq < then ; : nsqrt 1 begin sc if swap drop exit then 2dup / a again ; 100 nsqrt .
result 10
links
http://amforth.sourceforge.net/
AmForth, Floating point and ATmega implementation:
Pygmy Forth: http://pygmy.utoh.org/riscy/