Literals/Floating point: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(4 intermediate revisions by 4 users not shown)
Line 351:
=={{header|D}}==
D built-in floating point types include ''float'' (32-bit), ''double'' (64-bit) and ''real'' (machine hardware maximum precision floating point type, 80-bit on x86 machine) and respective complex number types. Here's information for [http://www.d-programming-language.org/lex.html#floatliteral Floating Literals].
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
procedure FloatLiterals(Memo: TMemo);
{Delphi has multiple floating number formats}
var R48: Real48; {48-bit real number}
var SI: Single; {32-bit real number}
var D: Double; {64-bit real number}
var E: Extended; {80-bit real number}
var Cmp: Comp; {}
var Cur: Currency; {Fixed point number for currency}
begin
{Various formats that can be used on input or
as constants assigned to various reals.
Pascal automaically converts integer to
reals as long as the real has enough precision
to hold the value. Consequently, all of the
following constants can be assigned to a real.}
D:=1234;
D:=1.234;
D:=1234E-4;
D:=$7F;
{Reals can also be output in various formats.}
D:=123456789.1234;
Memo.Lines.Add(FloatToStrF(D,ffGeneral,18,4));
Memo.Lines.Add(FloatToStrF(D,ffExponent,18,4));
Memo.Lines.Add(FloatToStrF(D,ffFixed,18,4));
Memo.Lines.Add(FloatToStrF(D,ffNumber,18,4));
Memo.Lines.Add(FloatToStrF(D,ffCurrency,18,4));
Memo.Lines.Add(Format('%10.4f',[D]));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
123456789.123400003
1.23456789123400003E+8
123456789.1234
123,456,789.1234
$123,456,789.1234
123456789.1234
 
</pre>
 
 
=={{header|Dyalect}}==
Line 398 ⟶ 447:
=={{header|Elena}}==
<syntaxhighlight lang="elena">real r := 1;
r := 23.2r2;
r := 1.2e+11r;</syntaxhighlight>
 
Line 1,296 ⟶ 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,360 ⟶ 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,633 ⟶ 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,476

edits