Manuel Rodriguez
Manuel Rodriguez

Reputation: 782

Forth floating point calculation with local variables

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?

Upvotes: 4

Views: 576

Answers (1)

lurker
lurker

Reputation: 58284

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.

Upvotes: 3

Related Questions