Literals/Floating point: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
No edit summary
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(3 intermediate revisions by 3 users not shown)
Line 447:
=={{header|Elena}}==
<syntaxhighlight lang="elena">real r := 1;
r := 23.2r2;
r := 1.2e+11r;</syntaxhighlight>
 
Line 1,345:
2.e34 # 1.9999999999999999e+34
</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}}==
Line 1,409 ⟶ 1,454:
 
There are other options for the &nbsp; '''format''' &nbsp; BIF to force any number of digits before and/or after the decimal point, &nbsp; and/or specifying the number of digits in the exponent. <br><br>
 
=={{header|RPL}}==
Floating point numbers exist only in decimal format. They can be entered using the scientific format: <code>3.1416E2</code> is equivalent to <code>314.16</code>.
Display can be forced to <code>SCI</code>entific or "<code>ENG</code>ineering" format (the exponent being a multiple of 3), with a defined number of digits after the decimal point of the mantissa:
1: { 123.45 0.012345 }
3 SCI
1: { 1.235E+02 1.245E-02 }
3 ENG
1: { 123.5E0 12.45E-3 }
 
=={{header|Ruby}}==
Line 1,682 ⟶ 1,736:
 
All numbers are instances of the built-in Num class which is always 8 bytes in size. Integers are really just floating point numbers which have no decimal part.
<syntaxhighlight lang="ecmascriptwren">var f = 123.45
var g = 0.12345 // .12345 not allowed
var h = 1.234e2
9,482

edits