Skip to content

Instantly share code, notes, and snippets.

@neuro-sys
Last active November 27, 2022 10:09
Show Gist options
  • Save neuro-sys/228a910e7e11cf467fcd75ace2b27a13 to your computer and use it in GitHub Desktop.
Save neuro-sys/228a910e7e11cf467fcd75ace2b27a13 to your computer and use it in GitHub Desktop.
forth locals with vocabularies
vocabulary quadratic also quadratic definitions
vocabulary fdiscriminant also fdiscriminant definitions
fvariable a
fvariable b
fvariable c
previous definitions also fdiscriminant
\ calculate the discriminant (b^2 - 4ac)
: fdiscriminant ( a b c -- n )
c f! b f! a f!
b f@ fdup f* a f@ c f@ f* 4e f* f-
;
previous
: solutions? ( a b c -- )
fdiscriminant
fdup 0e f= if cr ." 1 solution" fdrop exit then
0e f> if cr ." 2 solutions" fdrop exit then
cr ." No real solutions"
;
\ Example usage:
\ 9e -12e 4e quadratic solutions?
\ 1 solution ok
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment