Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forth floating point calculation with local variables

Tags:

forth

gforth

In Forth, it's possible to use the separate floating point stack for division and multiplication. A typical example to calculate 3/4 is:

3.e 4.e f/ f.
0.75  ok

which is 0.75. But what will happen, if the calculation is done within a word, which is using local variables?

: calc { a b } 
  a b f/ f.
;
3.e 4.e calc 

:2: Stack underflow
3.e 4.e >>>calc<<< 
Backtrace:
$7FDF1C7C1220 >l 

It seems, that Forth has expected a value at the integer stack which is empty, because before the function call, the variables were put to the floating stack. The question is how to modify the calc-word in a way, that the local variables are taken from the float stack?

like image 298
Manuel Rodriguez Avatar asked Oct 17 '25 17:10

Manuel Rodriguez


1 Answers

You can specify that the variables be floating point by using the F: type specifier.

: calc { F: a F: b }
   a b f/ f.
;
3.e 4.e calc \ => 0.75  ok

For details, see section 5.21.1 in the gforth manual.

like image 191
lurker Avatar answered Oct 21 '25 10:10

lurker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!