Literals/Floating point: Difference between revisions

Content added Content deleted
(add RPL)
(Added Quackery.)
Line 1,345: Line 1,345:
2.e34 # 1.9999999999999999e+34
2.e34 # 1.9999999999999999e+34
</syntaxhighlight>
</syntaxhighlight>

=={{header|Quackery}}==

Quackery does not support floating point, but it runs on top of Python, which does. So we can reach down into Python and pass floating point numbers back and forth. Here is a cheap but effective way of doing so. We will represent floating point numbers in Quackery as strings. The task requirements are fulfilled by [[Literals/Floating point#Python|the Python entry]].

<code>isfloat</code> conforms that a string is a float by asking Python to validate it.

<code>f</code> adds a sprinkle of syntactic sugar to the Quackery compiler so we can say <code>f 0.5</code> rather that <code>$ "0.5"</code>. (Builders – words defined with <code>builds</code> rather than <code>is</code> – are extensions to the Quackery compiler.)

<code>sin</code>returns the sine of an angle expressed in radians, so that we can demonstrate usage of <code>f</code> here.

<syntaxhighlight lang="Quackery"> [ $ \
try:
float(string_from_stack())
except:
to_stack(False)
else:
to_stack(True)
\ python ] is isfloat ( $ --> b )

[ nextword
dup isfloat not if
[ $ '"f" needs to be followed by a number.'
message put bail ]
' [ ' ] swap nested join
nested swap dip join ] builds f ( [ $ --> [ $ )

[ $ \
import math
a = string_from_stack()
a = str(math.sin(float(a)))
string_to_stack(a) \ python ] is sin ( $ --> $ )
</syntaxhighlight>

{{out}}

Demonstrating usage as a dialogue in the Quackery shell (REPL).

<pre>/O> f 0.5
... dup echo$ cr
... sin echo$ cr
...
0.5
0.479425538604203
</pre>


=={{header|Racket}}==
=={{header|Racket}}==