Literals/Floating point: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Tcl}}: Added Vim Script)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(79 intermediate revisions by 51 users not shown)
Line 1:
{{task|Basic language learning}}
 
Programming languages have different ways of expressing floating-point literals. Show how floating-point literals can be expressed in your language: decimal or other bases, exponential notation, and any other special features.
Programming languages have different ways of expressing floating-point literals.
 
 
;Task:
Show how floating-point literals can be expressed in your language: decimal or other bases, exponential notation, and any other special features.
 
You may want to include a regular expression or BNF/ABNF/EBNF defining allowable formats for your language.
 
See also [[Literals/Integer]].
 
;Related tasks:
Cf. [[Extreme floating point values]]
*   [[Literals/Integer]]
*   [[Extreme floating point values]]
<br><br>
 
=={{header|11l}}==
<syntaxhighlight lang="11l">// 64-bit floating point literals:
2.3
0.3e+34
 
// single precision (32-bit) floating point literals:
2.3s
0.3e+34s</syntaxhighlight>
 
=={{header|360 Assembly}}==
[[wp:IBM_hexadecimal_floating_point|IBM hexadecimal floating point]]
<syntaxhighlight lang="360asm">XS4 DC E'1.23456E-4' short floating-point
XDPI DC D'3.141592653589793' long floating-point
XD1 DC D'0' long floating-point
XD2 DC D'1' long floating-point
XD3 DC D'-1' long floating-point
XD4 DC D'1.2345E-4' long floating-point
 
XQPI DC L'3.14159265358979323846264338327950' extended
 
* short floating-point - 32 bits - 4 bytes : 6 decimal digits
* long floating-point - 64 bits - 8 bytes : 16 decimal digits
* extended floating-point - 128 bits - 16 bytes : 33 decimal digits
 
* absolute approximate range: 5e-79 to 7e75 </syntaxhighlight>
 
=={{header|6502 Assembly}}==
You'll have to do it the hard way unfortunately. I used an IEEE-754 floating point calculator to figure this out.
<syntaxhighlight lang="6502asm">byte $DB,$0F,$49,$40 ;3.141592654</syntaxhighlight>
 
=={{header|68000 Assembly}}==
If you don't have a floating-point unit, floats aren't of very much use. The most portable way to declare float literals is also the most tedious: by storing their hexadecimal representation as data.
<syntaxhighlight lang="68000devpac">Pi:
DC.L $40490FDB</syntaxhighlight>
 
=={{header|Ada}}==
Real literals contain decimal point. The exponent part is optional. Underline may be used to separate groups of digits. A literal does not have sign, + or - are unary operations. Examples of real literals:
<syntaxhighlight lang="ada">
<lang Ada>
3.141_592_6
1.0E-12
0.13
</syntaxhighlight>
</lang>
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">3.14
5.0
8r # without the "r"(eal) suffix, "8" would be an integer
.125</langsyntaxhighlight>
 
=={{header|ALGOL 68}}==
<syntaxhighlight lang="algol68"># floating point literals are called REAL denotations in Algol 68 #
# They have the following forms: #
# 1: a digit sequence followed by "." followed by a digit sequence #
# 2: a "." followed by a digit sequence #
# 3: forms 1 or 2 followed by "e" followed by an optional sign #
# followed by a digit sequence #
# 4: a digit sequence follows by "e" followed by an optional sign #
# followed by a digit sequence #
# #
# The "e" indicates the following optionally-signed digit sequence is #
# the exponent of the literal. #
# If the implementation allows, a "times ten to the power symbol" #
# can be used to replace "e" - e.g. a subscript "10" character #
# #
# spaces can appear anywhere in the denotation #
# Examples: #
REAL r;
r := 1.234;
r := .987;
r := 4.2e-9;
r := .4e+23;
r := 1e10;
r := 3.142e-23;
r := 1 234 567 . 9 e - 4;
</syntaxhighlight>
 
=={{header|ALGOL W}}==
<syntaxhighlight lang="algolw">begin
real r; long real lr;
% floating point literals have the following forms: %
% 1 - a digit sequence followed by "." followed by a digit sequence %
% 2 - a digit sequence followed by "." %
% 3 - "." followed by a digit sequence %
% 4 - one of the above, followed by "'" followed by an optional sign %
% folloed by a digit sequence %
% the literal can be followed by "L", indicating it is long real %
% the literal can be followed by "I", indicating it is imaginary %
% the literal can be followed by "LI" or "IL" indicating it is a long %
% imaginary number %
% an integer literal ( digit sequence ) can also be used where a %
% floating point literal is required %
% non-imaginary examples: %
r := 1.23;
r := 1.;
r := .9;
r := 1.23'5;
r := 1.'+4;
r := .9'-12;
r := 7;
lr := 5.4321L;
end.</syntaxhighlight>
 
=={{header|Applesoft BASIC}}==
Line 36 ⟶ 132:
-6.66E-32
</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="rebol">pi: 3.14
print [pi "->" type pi]</syntaxhighlight>
 
{{out}}
 
<pre>3.14 -> :floating</pre>
 
=={{header|AWK}}==
With the One True Awk ([[nawk]]), all numbers are floating-point. A numeric literal consists of one or more digits '0-9', with an optional decimal point '.', followed by an optional exponent. The exponent is a letter 'E' or 'e', then an optional '+' or '-' sign, then one or more digits '0-9'.
 
<langsyntaxhighlight lang="awk">2
2.
.3
Line 46 ⟶ 150:
45e+6
78e-9
1.2E34</langsyntaxhighlight>
 
Other implementations of Awk can differ. They might not use floating-point numbers for integers.
Line 52 ⟶ 156:
This Awk program will detect whether each line of input contains a valid integer.
 
<langsyntaxhighlight lang="awk">/^([0-9]+(\.[0-9]*)?|\.[0-9]+)([Ee][-+]?[0-9]+)?$/ {
print $0 " is a literal number."
next
Line 59 ⟶ 163:
{
print $0 " is not valid."
}</langsyntaxhighlight>
 
A leading plus or minus sign (as in <tt>+23</tt> or <tt>-14</tt>) is not part of the literal; it is a unary operator. This is easy to check if you know that exponentiation has a higher precedence than unary minus; <tt>-14 ** 2</tt> acts like <tt>-(14 ** 2)</tt>, not like <tt>(-14) ** 2</tt>.
 
=={{header|Axe}}==
Axe does not support floating point literals. However, it does support converting floats to integers and vice versa.
<syntaxhighlight lang="axe">123→float{L₁}
float{L₁}→I</syntaxhighlight>
 
Axe does, however, support fixed-point literals.
<syntaxhighlight lang="axe">12.25→A</syntaxhighlight>
 
There are some mathematical operators in Axe that operate specifically on fixed-point numbers.
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> REM Floating-point literal syntax:
REM [-]{digit}[.]{digit}[E[-]{digit}]
Line 76 ⟶ 190:
PRINT 8.9E
PRINT .33E-
PRINT -.</langsyntaxhighlight>
'''Output:'''
<pre>
Line 100 ⟶ 214:
Why does 6.4.4.2 seem to disallow negative floats? Clearly this isn't the intent... maybe -1.2 is considered to be the unary negation of 1.2?
-->
 
=={{header|C sharp}}==
Floating point suffixes are not case-sensitive.
<syntaxhighlight lang="csharp">double d = 1;
d = 1d;
d = 1D;
d = 1.2; //double is the default if there's no suffix
d = 1.2d; //The suffix is redundant here
d = .2;
d = 12e-12;
d = 12E-12;
d = 1_234e-1_2; //digit separators are allowed since C# 7
float f = 1;
f = 1f;
f = 1F;
f = 1.2f;
f = .2f;
f = 12e-12f;
f = 12E-12f;
f = 1_234e-1_2f;
decimal m = 1;
m = 1m;
m = 1m;
m = 1.2m;
m = .2m;
m = 12e-12m;
m = 12E-12m;
m = 1_234e-1_2m;</syntaxhighlight>
 
=={{header|C++}}==
<syntaxhighlight lang="cpp">#include <iostream>
 
int main()
{
// a numeric literal with decimal point is a double
auto double1 = 2.5;
 
// an 'f' of 'F' suffix means the literal is a flaot
auto float1 = 2.5f;
 
// an 'l' or 'L' suffix means a long double
auto longdouble1 = 2.5l;
 
// a number after an 'e' or 'E' is the base 10 exponent
auto double2 = 2.5e-3;
auto float2 = 2.5e3f;
 
// a '0x' prefix means the literal is hexadecimal. the 'p' is base 2 the exponent
auto double3 = 0x1p4;
auto float3 = 0xbeefp-8f;
 
std::cout << "\ndouble1: " << double1;
std::cout << "\nfloat1: " << float1;
std::cout << "\nlongdouble1: " << longdouble1;
std::cout << "\ndouble2: " << double2;
std::cout << "\nfloat2: " << float2;
std::cout << "\ndouble3: " << double3;
std::cout << "\nfloat3: " << float3;
std::cout << "\n";
}</syntaxhighlight>
{{out}}
<pre>
double1: 2.5
float1: 2.5
longdouble1: 2.5
double2: 0.0025
float2: 2500
double3: 16
float3: 190.934
</pre>
 
=={{header|Clojure}}==
 
Clojure supports both standard and scientific notation.
 
<pre>user=> 1.
1.0
user=> 1.0
1.0
user=> 3.1415
3.1415
user=> 1.234E-10
1.234E-10
user=> 1e100
1.0E100
user=> (Float/valueOf "1.0f")
1.0</pre>
 
Clojure also supports returning ratios (fractions) if you divide integers. These are not subject to roundoff error. If you do specify a floating point in the division, it will return a floating point value.
<pre>user=> (/ 1 3)
1/3
user=> (/ 1.0 3)
0.3333333333333333</pre>
 
=={{header|Common Lisp}}==
Line 144 ⟶ 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}}==
 
Dyalect built-in types include only one floating point number of type ''Float'' (64-bit). Both regular and scientific notations are supported:
 
<syntaxhighlight lang="dyalect">var x = 42.02
var y = 0.174e-17</syntaxhighlight>
 
EBNF grammar for the floating point number is as follows:
 
<pre>float = "." digit { digit } [ ("e"|"E") [ "+" | "-" ] digit { digit } ]
| digit { digit }
(
"." digit { digit } [ ( "e" | "E" ) ["+" | "-" ] digit { digit } ]
| ( "e" | "E") ["+" | "-" ] digit { digit }
).</pre>
 
=={{header|EasyLang}}==
EasyLang's ability to use hexadecimal literals and floating-point numbers with "E" is undocumented.
<syntaxhighlight lang="easylang">
decimal = 57.1
decimalWithE = 5710E-2
hexadecimal = 0x39.1999999999
print decimal
print decimalWithE
print hexadecimal
</syntaxhighlight>
{{out}}
<pre>
57.10
57.10
57.10
</pre>
 
=={{header|Eiffel}}==
Floating point literals are of the form D.DeSD, where D represents a sequence of decimal digits, and S represents an optional sign. A leading "+" or "-" indicates a unary plus or minus feature and is not considered part of the literal.
 
'''Examples:'''<syntaxhighlight lang="eiffel">
1.
1.23
1e-5
.5
1.23E4
</syntaxhighlight>
 
=={{header|Elena}}==
<syntaxhighlight lang="elena">real r := 1;
r := 23.2;
r := 1.2e+11r;</syntaxhighlight>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">iex(180)> 0.123
0.123
iex(181)> -123.4
-123.4
iex(182)> 1.23e4
1.23e4
iex(183)> 1.2e-3
0.0012
iex(184)> 1.23E4
1.23e4
iex(185)> 10_000.0
1.0e4
iex(186)> .5
** (SyntaxError) iex:186: syntax error before: '.'
 
iex(186)> 2. + 3
** (CompileError) iex:186: invalid call 2.+(3)
 
iex(187)> 1e4
** (SyntaxError) iex:187: syntax error before: e4</syntaxhighlight>
 
=={{header|Erlang}}==
Floating point literal examples: 1.0 , -1.0 , 1.2e3 , 1.2e-3 and 1.2E3 , 1.2E-3 .
 
=={{header|Euphoria}}==
<syntaxhighlight lang="euphoria">
printf(1,"Exponential:\t%e, %e, %e, %e\n",{-10.1246,10.2356,16.123456789,64.12})
printf(1,"Floating Point\t%03.3f, %04.3f, %+3.3f, %3.3f\n",{-10.1246,10.2356,16.123456789,64.12})
printf(1,"Floating Point or Exponential: %g, %g, %g, %g\n",{10,16.123456789,64,123456789.123})
</syntaxhighlight>
{{out}}
<pre>
Exponential: -1.012460e+001, 1.023560e+001, 1.612346e+001, 6.412000e+001
Floating Point -10.125, 10.236, +16.123, 64.120
Floating Point or Exponential: 10, 16.1235, 64, 1.23457e+008
</pre>
 
=={{header|Factor}}==
<syntaxhighlight lang="factor">3.14 ! basic float
+3.14 ! Optional signs
-3.14
10e5 ! exponents signified by e or E
10E+5 ! with optional signs
+10e-5
1. ! equivalent to 1.0
.5 ! equivalent to 0.5
1/2. ! floating point approximation of a ratio (0.5)
1/3. ! 0.3333333333333333
1/0. ! positive infinity
-1/0. ! negative infinity
0/0. ! not-a-number
! hexadecimal, octal, and binary float literals are supported.
! they require a base 2 exponent expressed as a decimal
! preceded by p or P.
0x1.0p3 ! 8.0
-0x1.0P-3 ! -0.125
0b1.010001p3 ! 10.125
0o1.21p3 ! 10.125
! comma separators are allowed
1,234.123,456 ! 1234.123456
 
 
! normalized hex form ±0x1.MMMMMMMMMMMMMp±EEEE allows any floating-point
! number to be specified precisely according to IEEE 754 representation
+0x1.1234567891234p+0002 ! 4.28444444440952</syntaxhighlight>
 
=={{header|Fennel}}==
<syntaxhighlight lang="fennel">;;Numeric literals with a decimal component are treated as floating point.
3.14159 ;3.14159
 
;;An exponent can be specified via "e" or "E" and is always floating point.
2.3456e7 ;23456000.0
 
;;Hexadecimal literals are supported, including exponents via "p" or "P".
0x1234.abcd ;4660.6710968018
0x1234.56p3 ;37282.6875
 
;;Underscores can optionally be used to split numbers into readable chunks.
123_456.789 ;123456.789
0x1234_5678.9a ;305419896.60156</syntaxhighlight>
 
=={{header|Forth}}==
Line 162 ⟶ 546:
 
<pre> 1E 1.E 1.E0 +1.23E-1 -1.23E+1</pre>
 
=={{header|Fortran}}==
Floating-point literals involve a decimal point, otherwise they're integers. The rule is <sign><digits><.><digits><exponent> with each optional - except that there must be ''some'' digits! Spaces are irrelevant in source files, so 3 .141 159 would be acceptable, however when data are read, internal spaces are not allowed so "- 3.14" would be rejected - at least for free-format (or "list") style input. With formatted input, spaces are considered to be zeroes and a data field lacking a decimal point can have one assumed so that " 31" read by F4.1 would yield 3.1. There is no requirement that there be digits before the decimal point, nor digits after the decimal point (if there are digits before), so .5 and 5. are both acceptable.
 
The status of the sign is delicate, being a matter of context. In a DATA statement or in an assignment such as x = -5.5, the sign is a part of the number, but not in an arithmetic expression such as y = x*-5.5 which has two operators in a row and is rejected. x*(-5.5) is accepted and the sign is a part of the number.
 
The exponent part signifies a power of ten and if present, has the form <E or D><sign><integer>, the sign optional, where E signifies a single-precision number and D a double-precision number, irrespective of the number of digits offered in the number. Thus, a constant 3.14159265 will be double-precision only if there follows a D, presumably with a zero exponent. As a result, 1.0D0 is not the same as 1.0E0, even though they are equal, and a calculation such as 4*atan(1.0) will be in single precision unless it is 4*atan(1.0D0) or similar. Some compilers offer an option to regard all constants as being in double precision irrespective of E or D, but in the absence of that, 1.15 or 1.15E0 will ''not'' equal 1.15D0 because most decimal fractions are recurring sequences in binary, and if such constants were assigned to suitable variables and printed with one decimal digit, then on common computers, the double-precision value will come out as 10.2 because with 53-bit precision its value is (exactly) 10·1500000000000003552713678800500929355621337890625, which rounds up, while in single precision it is (exactly) 10·1499996185302734375, which rounds down.
 
There are also options for specifying constants as hexadecimal sequences, and if assigned to a floating-point variable, then Z"FFFFFFFFFFFFFFFF" will generate a (double precision) NaN value while Z"FFF0000000000000" will generate negative infinity, on cpus supporting such features. Similarly with octal and binary sequences. In these cases, the bit patterns are as they will be in the floating-point format, not as a number expressed in hexadecimal, etc. Thus, pi = Z"40490FDB" or 1000000010010010000111111011011, which is not 11.00100100001111110... at all.
 
Complex number constants are typically specified as (x,y) where x and y are floating-point literals; there is no provision for complex integers.
 
=={{header|FreeBASIC}}==
FreeBASIC has two floating point types : Single (4 bytes) and Double (8 bytes)
 
Numeric literals of these types can be specified by using the following suffixes:
 
Single !, f or F : Double #
 
However, this is not usually necessary as the compiler will automatically infer the type from the context and the two types are implicitly convertible to each other or explicitly convertible using the CSng or CDbl functions. However, conversions from Double to Single may lose precision.
 
All numeric literals which include a decimal point or exponent (i.e. scientific notation) are considered to be of floating point rather than integral type and are generally of the form:
 
number[.[fraction]][((D|E) [+|-] exponent)|(D|E)|][suffix]
 
or
 
.fraction[((D|E) [+|-] exponent)|(D|E)|][suffix]
 
Where scientific notation is used, D denotes Double precision and E denotes default precision , though these can be over-ridden by the suffix, if there is one. They can also be used on their own, without a following exponent.
 
The default precision is Double unless the 'QB' dialect of the language is used (for compatibility with QuickBasic code) where numbers of no more than 7 digits are considered to be Single precision.
 
Some examples, taken from the language documentation follow:
 
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 (default dialect)
 
Dim a As Double = 123.456
Dim b As Double = -123.0
Dim c As Double = -123.0d
Dim d As Double = -123e
Dim e As Double = 743.1e+13
Dim f As Double = 743.1D-13
Dim g As Double = 743.1E13
Dim h As Single = 743D! Rem ! overrides D
Dim i As Single = 3.1!
Dim j As Single = -123.456e-7f
Dim k As Double = 0#
Dim l As Double = 3.141592653589e3#</syntaxhighlight>
 
=={{header|FutureBasic}}==
FB can express floating point literals in several ways.
<syntaxhighlight lang="futurebasic">
local fn DoIt
print "Single:"
single s = 1.0 : print s
s = 123.456 : print s
s = -123.0 : print s
s = 43.1e+5 : print s
s = 43.1E5 : print s
s = 3.1 : print s
s = -123.456e-2 : print s
s = 3.141592653589e3 : print s
print
 
print "Double"
double d = 1.0 : print d
d = 123.456 : print d
d = -123.0 : print d
d = 43.1e+13 : print d
d = 43.1E13 : print d
d = 3.1 : print d
d = -123.456e-7 : print d
d = 3.141592653589e3 : print d
print
 
print "Float:"
float f = 1.0 : print f
f = 123.456 : print f
f = -123.0 : print f
f = 43.1e+5 : print f
f = 43.1E5 : print f
f = 3.1 : print f
f = -123.456e-2 : print f
f = 3.141592653589e3 : print f
print
 
print "CFNumberRef:"
CFNumberRef c = @1.0 : print c
c = @123.456 : print c
c = @-123.0 : print c
c = @43.1e+13 : print c
c = @743.1E13 : print c
c = @3.1 : print c
c = @-123.456e-7 : print c
c = @3.141592653589e37 : print c
end fn
 
fn DoIt
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Single:
1
123.456
-123
4310000
4310000
3.1
-1.23456
3141.593
 
Double
1
123.456
-123
431000000000000
431000000000000
3.1
-1.23456e-05
3141.592653589
 
Float:
1
123.456
-123
4310000
4310000
3.1
-1.23456
3141.593
 
CFNumberRef:
1
123.456
-123
431000000000000
7431000000000000
3.1
-1.23456e-05
3.141592653589e+37
</pre>
 
=={{header|GAP}}==
 
<langsyntaxhighlight lang="gap">-3.14
22.03e4
4.54e-5</langsyntaxhighlight>
 
=={{header|gecho}}==
<langsyntaxhighlight lang="gecho">
0.0
-1
Line 176 ⟶ 704:
-1.4324
3 4 /
</syntaxhighlight>
</lang>
 
=={{header|Go}}==
See [http://golang.org/doc/go_spec.html#Floating-point_literals relevant section] of language reference. Basically they are base 10, need either a decimal point or an exponent, and specify no precision or representation. The exponent can be signed, but the mantissa is not. One of the integer part or the fractional part may be elided; one of the decimal point or the exponent may be elided. A leading minus sign would be an operator and not part of the floating point literal. Examples,
<pre>0.
0.0
.0
1e3
1e-300
Line 187 ⟶ 717:
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">println 1.00f // float (IEEE-32)
println 1.00d // double (IEEE-64)
println 1.00 // BigDecimal (scaled BigInteger)
Line 197 ⟶ 727:
assert 1.00 instanceof BigDecimal
assert 1.00g instanceof BigDecimal
assert 1.00e0 instanceof BigDecimal</langsyntaxhighlight>
 
{{out}}
Line 209 ⟶ 739:
Haskell supports decimal representation of float literals, with or without an exponent. For more information, see the [http://www.haskell.org/onlinereport/lexemes.html#sect2.5 relevant portion] of the Haskell 98 Report.
 
<langsyntaxhighlight lang="haskell">main = print [0.1,23.3,35e-1,56E+2,14.67e1]
</syntaxhighlight>
</lang>
 
Output:
Line 219 ⟶ 749:
 
The program below shows a full range of valid real literals.
<langsyntaxhighlight Iconlang="icon">procedure main()
every write( ![ 1., .1, 0.1, 2e10, 2E10, 3e-1, .4e2, 1.41e2, 8.e+3, 3.141e43 ])
end</langsyntaxhighlight>
 
The function write will cause the real values to be coerced as string constants. Icon/Unicon will format these as it sees fit resorting to exponent forms only where needed.
{{libheader|Icon Programming Library}}
[http://www.cs.arizona.edu/icon/library/src/procs/printf The IPL library routine printf provides a broader range of formatting choices.]
 
=={{header|J}}==
Line 235 ⟶ 765:
Here is an informal bnf for J's numeric constant language. Note, however, that the implementation may disallow some unusual cases -- cases which are not treated as exceptional here (for example, the [http://jsoftware.com/help/dictionary/dcons.htm language specification] allows 1.2e3.4 but the current implementation does not support fractional powers of 10 in numeric constants):
 
<langsyntaxhighlight lang="bnf">numeric-constant ::= number-constant | number-constant whitespace numeric-constant
whitespace ::= whitespacecharacter | whitespacecharacter whitespace
whitespacecharacter ::= ' ' | TAB
Line 257 ⟶ 787:
signed-digits ::= digits | '_' digits
digits ::= digit | digit digits
digit ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'</langsyntaxhighlight>
 
e indicates exponential or scientific notation (number on left multiplied by 10 raised to power indicated by number on right)
Line 271 ⟶ 801:
Floating point examples:
 
<langsyntaxhighlight lang="j"> 0 1 _2 3.4 3e4 3p4 3x4
0 1 _2 3.4 30000 292.227 163.794
16bcafe.babe _16b_cafe.babe _10b11
51966.7 46818.7 _9</langsyntaxhighlight>
 
Note that all the values in an array are the same type, thus the 0, 1 and 2 in the above example are floating point because they do not appear by themselves. Note also that by default J displays no more than six significant digits of floating point values.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">1. //double equal to 1.0
1.0 //double
2432311.7567374 //double
Line 291 ⟶ 821:
1.0F //float
1 / 2. //double
1 / 2 //int equal to 0</syntaxhighlight>
</lang>
Values that are outside the bounds of a type will give compiler errors when trying to force them to that type.
 
=={{header|jq}}==
jq floating point literals are identical to JSON floating point literals. However, when jq parses a floating point or integer literal, conversion to IEEE 754 numbers takes place, which may result in a loss of accuracy and/or an apparent change of type, as illustrated by the following sequence of input => output pairs:
<syntaxhighlight lang="sh">1.0 => 1
1.2 => 1.2
1e10 => 10000000000
1e100 => 1e+100
1e1234 => 1.7976931348623157e+308
.1 => 0.1
.1e1 => 1</syntaxhighlight>
 
=={{header|Julia}}==
{{works with|Julia|0.6}}
 
<syntaxhighlight lang="julia">0.1
.1
1.
1e-1 # scientific notation
1e+10
1e-10
0x01p-1 # hex float</syntaxhighlight>
 
=={{header|Kotlin}}==
There are two floating point types: Double (64 bits) and Float (32 bits). The default floating point type is Double.
 
Double literals must end with a decimal point with at least one digit after it, and/or a scientific notation suffix made with <code>e</code> (or <code>E</code>) followed by an integer literal.
 
A Float literal is made by appending <code>f</code> (or <code>F</code>) to a Double or Int literal (see [[Literals/Integer#Kotlin|integer literals]]).
 
<syntaxhighlight lang="kotlin">
val d = 1.0 // Double
val d2 = 1.234e-10 // Double
val f = 728832f // Float
val f2 = 7.28832e5F // Float
</syntaxhighlight>
 
=={{header|Lasso}}==
<syntaxhighlight lang="lasso">0.0
<lang Lasso>0.0
0.1
-0.1
1.2e3
1.3e+3
1.2e-3</langsyntaxhighlight>
 
=={{header|Lingo}}==
<syntaxhighlight lang="lingo">put 0.23
-- 0.2300
 
-- activate higher printing precision
the floatPrecision = 8
 
put -.23
-- -0.23000000
 
put 9.00719925474099e15
-- 9.00719925474099e15
 
-- result is NOT a float
put 2/3
-- 0
 
-- casting integer to float
put float(2)/3
-- 0.66666667
 
-- casting string to float
put float("0.23")
-- 0.23000000</syntaxhighlight>
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">3.14159
314.159E-2</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
We can use Decimal using @ and Currency using # (no exponent part, both types)
 
<syntaxhighlight lang="m2000 interpreter">
Def ExpType$(x)=Type$(x)
Print ExpType$(-12)="Double", -12
Print ExpType$(12.)="Double", 12.
Print ExpType$(12.e-5)="Double", 12.e-5
Print ExpType$(.1)="Double", .1
Print ExpType$(-12~)="Single", -12~
Print ExpType$(12.~)="Single", 12.~
Print ExpType$(12.e-5~)="Single", 12.e-5~
Print ExpType$(.1~)="Single", .1~
</syntaxhighlight>
 
=={{header|Maple}}==
Maple distinguishes "software floats" (of arbitrary precision) and "hardware floats" (of machine precision). To get the latter, use the "HFloat" constructor.
<syntaxhighlight lang="maple">
<lang Maple>
> 123.456; # decimal notation
123.456
Line 357 ⟶ 962:
> Float(undefined); # "NaN", not-a-number
Float(undefined)
</syntaxhighlight>
</lang>
Whether a given float is a software or hardware float can be determined by using "type".
<syntaxhighlight lang="maple">
<lang Maple>
> type( 2.3, 'hfloat' );
false
Line 365 ⟶ 970:
> type( HFloat( 2.3 ), 'hfloat' );
true
</syntaxhighlight>
</lang>
(There is also a type "sfloat" for software floats, and the type "float", which covers both.)
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">These numbers are given in the default output format. Large numbers are given in scientific notation.
{6.7^-4,6.7^6,6.7^8}
{0.00049625,90458.4,4.06068*10^6}
Line 383 ⟶ 988:
In accounting form, negative numbers are given in parentheses, and scientific notation is never used.
AccountingForm[{5.6,-6.7,10.^7}]
{5.6,(6.7),10000000.}</langsyntaxhighlight>
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">/* Maxima has machine floating point (usually double precision IEEE 754), and
arbitrary length "big floats" */
 
Line 405 ⟶ 1,010:
 
bfloat(%pi);
3.141592653589793238462643383279502884197b0</langsyntaxhighlight>
=={{header|MIPS Assembly}}==
Ultimately it depends on the assembler, however, it's standard to allow simple expressions like "2.5". Since MIPS uses a floating-point coprocessor with separate registers, the assembler understands that you're trying to load a float into the register rather than an integer, and so the assembler will calculate the IEEE-754 hexadecimal representation for you.
 
<syntaxhighlight lang="mips">li.s f0,2.5 ;loads the single-precision float 2.5 (0x40200000) into register f0</syntaxhighlight>
 
Defining literal float data in your code also depends on the assembler. If all else fails, you can use a calculator to get the IEEE-754 hexadecimal representation of the desired float and store it as an "integer."
 
<syntaxhighlight lang="mips">la $t0,pi
lwc1 $f0,0($t0) ;load pi into $f0
 
li $v0,10 ;exit command
syscall ;return to linux
 
pi:
.word 0x40490FDB ;IEEE-754 representation of 3.1415927</syntaxhighlight>
 
=={{header|Nemerle}}==
Line 448 ⟶ 1,068:
it's built in ''Rexx'' object and any other Java object that supports floating point numbers.
 
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 496 ⟶ 1,116:
method normalize(fv) private constant
return fv + 0
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 526 ⟶ 1,146:
0.033E-9 == 3.3E-11
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="nim">var x: float
x = 2.3
x = 2.0
x = 0.3
x = 123_456_789.000_000_1
x = 2e10
x = 2.5e10
x = 2.523_123E10
x = 5.2e-10
 
var y = 2'f32 # Automatically a float32
var z = 2'f64 # Automatically a float64
</syntaxhighlight>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
3 + .14159
3.14159
314.159E-2
</syntaxhighlight>
</lang>
 
=={{header|OCaml}}==
Line 542 ⟶ 1,177:
Here are some examples:
 
<langsyntaxhighlight lang="ocaml">0.5
1.0
1. (* it is not possible to write only "1" because OCaml is strongly typed,
and this would be interpreted as an integer *)
1e-10
3.14159_26535_89793</langsyntaxhighlight>
 
=={{header|Oforth}}==
 
A literal floating point number is written with a . and with or without an exponential notation :
 
<syntaxhighlight lang="oforth">3.14
1.0e-12
0.13
1000.0
.22</syntaxhighlight>
 
=={{header|PARI/GP}}==
Line 565 ⟶ 1,210:
|}
where <math>\epsilon</math> is the machine epsilon at the selected precision. The minimum value is the opposite of the maximum value (reverse the sign bit).
<pre>0.0
0. \\ == 0.0
.0 \\ == 0.0
. \\ == 0.0
2e2
6.02e23
-2e48
1e-9
1e0</pre>
 
=={{header|Pascal}}==
Line 574 ⟶ 1,228:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl"># Standard notations:
.5;
0.5;
Line 581 ⟶ 1,235:
# The numbers can be grouped:
100_000_000; # equals to 100000000
</syntaxhighlight>
</lang>
 
=={{header|Perl 6Phix}}==
{{libheader|Phix/basics}}
Floating point numbers (the Num type) are written various forms of scientific notation:
Phix does not require any distinction between integers and floats: 5 and 5.0 are exactly the same.
<lang perl6>6.02e23 # standard E notation
A variable declared as atom can hold an integer or a floating point value.<br>
:10<6.02 * 10 ** 23> # radix notation
Division and other operators do what a sensible language should, eg 1/3 is 0.333333, not 0. [for the latter use floor(1/3)]<br>
:5<11.002 * 10 ** 23> # exponent is still decimal
Floats cannot be expressed in any base other than decimal. They may optinally include a sign for mantissa and/or exponent.<br>
:5<11.002*:5<20>**:5<43>> # all in base 5</lang>
It is not necessary for a digit to precede a decimal point, but one must follow it. Upper or lower e/g may be used.<br>
 
In the 32-bit version, integers outside -1,073,741,824 to +1,073,741,823 must be stored as atoms.
A number like <tt>3.1416</tt> is specifically not floating point, but rational (the Rat type), equivalent to <tt>3927/1250</tt>. On the other hand, <tt>Num(3.1416)</tt> would be considered a floating literal though by virtue of mandatory constant folding (not yet implemented).
In the 64-bit version the limits of integers are -4,611,686,018,427,387,904 to +4,611,686,018,427,387,903.<br>
On a 32-bit architecture floats can range from approximately -1e308 to +1e308 with 15 decimal digits,
and on a 64-bit architecture they can range from approximately -1e4932 to +1e4932 with 19 decimal digits.<br>
The included bigatom library allows working with extremely large integers and floats with arbitrary precision. In the following, '?x' is the Phix shorthand for 'print(1,x)', plus \n
<!--<syntaxhighlight lang="phix">-->
<span style="color: #0000FF;">?</span><span style="color: #000000;">1e+12</span> <span style="color: #000080;font-style:italic;">-- (same as 1e12)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">1e-12</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">5</span> <span style="color: #000080;font-style:italic;">-- (same as 5.0)
--?1. -- (illegal, use 1 or 1.0)</span>
<span style="color: #0000FF;">?.</span><span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (same as 0.1)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span> <span style="color: #000080;font-style:italic;">-- 0.333333</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%g %G\n"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1e-30</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
1e+12
1e-12
5
0.1
0.3333333333
1e-30 1E-30
</pre>
 
=={{header|PHP}}==
More [http://php.net/manual/en/language.types.float.php information] about floating point numbers in PHP.
<syntaxhighlight lang="php">.12
<lang PHP>.12
0.1234
1.2e3
7E-10
</syntaxhighlight>
</lang>
Formal representation:
<pre>
Line 605 ⟶ 1,281:
EXPONENT_DNUM [+-]?(({LNUM} | {DNUM}) [eE][+-]? {LNUM})
</pre>
 
=={{header|Picat}}==
<pre>2.0 % normal float. Must have a decimal after the decimal point
-2.1
2.01E3 % exponent can be E of e
2.01e3
2.01E-3
2.01e-3
1_000_123.123_456 % underscores can be used for clarity
</pre>
 
Precision for floats is 15. (Integers has arbitrary precision.)
 
 
=={{header|PicoLisp}}==
Line 613 ⟶ 1,302:
 
=={{header|PL/I}}==
<syntaxhighlight lang="pl/i">
<lang PL/I>
1.2345e-4 decimal floating-point
7e5 decimal floating-point
Line 624 ⟶ 1,313:
or 7.3125 * 2**7
1e5b binary floating-point equals 1 * 2**5
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
Line 635 ⟶ 1,324:
This is an excerpt of an ANTLR grammar for python obtained from [http://www.antlr.org/grammar/1200715779785/Python.g here].
 
<langsyntaxhighlight lang="ebnf">FLOAT
: '.' DIGITS (Exponent)?
| DIGITS '.' Exponent
Line 645 ⟶ 1,334:
Exponent
: ('e' | 'E') ( '+' | '-' )? DIGITS
;</langsyntaxhighlight>
 
Examples
<langsyntaxhighlight lang="python">
2.3 # 2.2999999999999998
.3 # 0.29999999999999999
Line 655 ⟶ 1,344:
.3e-34 # 2.9999999999999999e-35
2.e34 # 1.9999999999999999e+34
</syntaxhighlight>
</lang>
 
=={{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}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
.2
Line 669 ⟶ 1,403:
2.0f0 ; single float
1.0t0 ; extended 80-bit float (when available on platform)
</syntaxhighlight>
</lang>
 
Output:
Line 682 ⟶ 1,416:
1.0t0
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
Floating point numbers (the Num type) are written in the standard 'e' scientific notation:
<syntaxhighlight lang="raku" line>2e2 # same as 200e0, 2e2, 200.0e0 and 2.0e2
6.02e23
-2e48
1e-9
1e0</syntaxhighlight>
 
A number like <tt>3.1416</tt> is specifically not floating point, but rational (the Rat type), equivalent to <tt>3927/1250</tt>. On the other hand, <tt>Num(3.1416)</tt> would be considered a floating literal though by virtue of mandatory constant folding.
 
=={{header|REXX}}==
All values in REXX are character strings, &nbsp; so a value could hold such things as these (decimal) numbers:
<langsyntaxhighlight lang="rexx">something = 127
something = '127' /*exactly the same as the above. */
something = 1.27e2
something = 1.27E2
something = 1.27E+2
something = ' + 0001.27e+00000000000000002 '</langsyntaxhighlight>
To forcibly express a value in exponential notation, &nbsp; REXX has a built-in function &nbsp; '''format''' &nbsp; that can be used.
a built-in function (FORMAT) to do this.
 
Note that a value of &nbsp; '''0''' &nbsp; (zero) &nbsp; in any form is always converted to
<pre>
0
</pre>
by the &nbsp; '''FORMATformat''' subroutine&nbsp; BIF.
<langsyntaxhighlight lang="rexx">something = -.00478
say something
say format(something,,,,0)</langsyntaxhighlight>
'''output'''
<pre>
Line 707 ⟶ 1,451:
-4.78E-3
</pre>
The last invocation of &nbsp; '''FORMATformat''' &nbsp; (above, &nbsp; with the 5th parameter equal to zero) &nbsp; forces exponential notation, &nbsp; unless the exponent is &nbsp; '''0''' &nbsp; (zero), &nbsp; then exponential notation won't be used.
 
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}}==
There are other options for the '''FORMAT''' bif to force any number of digits before and/or after the decimal point, and/or specifying the number of digits in the exponent.
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 716 ⟶ 1,469:
Underscores can be used for clarity:
1_000_000_000.01
 
=={{header|Rust}}==
The fractional part may be elided (so 1. is valid) but the integer part may not (so .0 is not valid).
<syntaxhighlight lang="rust">2.3 // Normal floating point literal
3. // Equivalent to 3.0 (3 would be interpreted as an integer)
2f64 // The type (in this case f64, a 64-bit floating point number) may be appended to the value
1_000.2_f32 // Underscores may appear anywhere in the number for clarity.</syntaxhighlight>
 
=={{header|Scala}}==
{{libheader|Scala}}
As all values in Scala, values are boxed with wrapper classes. The compiler will unbox them to primitive types for run-time execution.
<langsyntaxhighlight Scalalang="scala">1. //Double equal to 1.0
1.0 //Double, a 64-bit IEEE-754 floating point number (equivalent to Java's double primitive type)
2432311.7567374 //Double
Line 744 ⟶ 1,504:
Double.PositiveInfinity
Double.NegativeInfinity
</syntaxhighlight>
</lang>
Values that are outside the bounds of a type will give compiler-time errors when trying to force them to that type.
 
=={{header|Scheme}}==
<langsyntaxhighlight lang="scheme">
.2 ; 0.2
2. ; 2.0
Line 758 ⟶ 1,518:
; #t
(inexact? 2)
; #f</langsyntaxhighlight>
 
=={{header|Seed7}}==
The type [http://seed7.sourceforge.net/libraries/float.htm float] consists of single precision floating point numbers. Float literals are base 10 and contain a decimal point. There must be at least one digit before and after the decimal point. An exponent part, which is introduced with E or e, is optional. The exponent can be signed, but the mantissa is not. A literal does not have a sign, + or - are unary operations. Examples of float literals are:
<langsyntaxhighlight lang="seed7">
3.14159265358979
1.0E-12
0.1234
</syntaxhighlight>
</lang>
The functions [http://seed7.sourceforge.net/libraries/float.htm#str%28ref_float%29 str] and the operators [http://seed7.sourceforge.net/libraries/float.htm#%28ref_float%29digits%28ref_integer%29 digits] and [http://seed7.sourceforge.net/libraries/float.htm#%28attr_float%29parse%28in_string%29 parse] create and accept float literals with sign.
 
Original source: [http://seed7.sourceforge.net/manual/types.htm#float]
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">say 1.234;
say .1234;
say 1234e-5;
say 12.34e5;</syntaxhighlight>
{{out}}
<pre>1.234
0.1234
0.01234
1234000</pre>
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">2.0
45e6
45e+6
78e-9
1.2E34</langsyntaxhighlight>
 
base 2 mantissa:
<langsyntaxhighlight lang="smalltalk">2r1010.0 -> 10.0
2r0.01 -> 0.25
2r1010e5 -> 320.0. "hint: = 10*(2ˆ5)"</langsyntaxhighlight>
 
base 2 mantissa and base 2 exponent:
<langsyntaxhighlight lang="smalltalk">2r1010e2r0101 -> 320.0 "hint: = 10*(2ˆ5)"</langsyntaxhighlight>
 
Complex numbers:
<syntaxhighlight lang="smalltalk">3.1i
2.0+4.5i</syntaxhighlight>
 
=={{header|Stata}}==
Only decimal floating-point are supported, computations are done in double precision (but storage can be made in integer or single floating-point as well).
 
Examples:
<syntaxhighlight lang="stata">.3
1.5
-1.5e10
3.15e-100</syntaxhighlight>
 
=={{header|Swift}}==
<syntaxhighlight lang="swift">let double = 1.0 as Double // Double precision
let float = 1.0 as Float // Single precision
let scientific = 1.0E-12
 
// Swift does not feature type coercion for explicit type declaration
let sum = double + float // Error
 
let div = 1.1 / 2 // Double
let div1 = 1 / 2 // 0</syntaxhighlight>
 
=={{header|Tcl}}==
Line 798 ⟶ 1,591:
:<code>[-+]?nan(\([0-9a-f]+\))?</code>
Note also that NaN values usually result in checked exceptions; they are supported mainly so that they can be handled when parsing and generating external binary data. All other floating-point literals are fully legal values. (Also note that this excludes the definition of integer literals; for details see [http://www.tcl.tk/cgi-bin/tct/tip/249.html this TIP document], which explains the whole state machine.)
 
=={{header|Ursa}}==
Cygnus/X Ursa (the standard Ursa interpreter) is written in Java and supports Java style floating-point literals.
<syntaxhighlight lang="ursa">1.
1.0
2432311.7567374
1.234E-10
1.234e-10
758832d
728832f
1.0f
758832D
728832F
1.0F</syntaxhighlight>
 
=={{header|Verbexx}}==
<syntaxhighlight lang="verbexx">// Floating-point Literals:
//
// If present,the exponent must be of the form:
//
// eNNN...N
// ENNN...N
// e-NNN...N
// E-NNN...N
// e+NNN...N
// E+NNN...N
//
// If present, length suffix must be:
//
// f F (FLOAT64_T)
// f32 F32 (FLOAT32_T)
// f64 F64 (FLOAT64_T)
// fd Fd fD FD (FLOATD_T) -- boost::multiprecision::
// cpp_dec_float<100, int64_t>
//
// The presence of "." "E" "e" "F" or "f" indicates a floating point literal.
//
// A literal can start with "-" "." or a decimal digit, but not "+" or "_".
// There must be at least one digit, so forms like ".F" ".e+11_f32" or just "."
// are not recognized as floating point literals.
//
// Floating-point literal examples:
@SAY 0. .0 0.0 1. .1 123.123 ;// FLOAT64_T
@SAY -0. -.0 -0.0 -1. -.1 -123.123 ;// FLOAT64_T
@SAY -0.E1 .0e0 0.0E6 -1.e6 -.1E8 12.12e44 -0E0 1e20 ;// FLOAT64_T
@SAY -0.e+1 .0E+0 0.0e+6 -1.E+6 -.1e+8 12.12E+44 -0e+0 1E+20 ;// FLOAT64_T
@SAY -0.E-1 .0e-0 0.0E-6 -1.e-6 -.1E-8 12.12e-44 -0E-0 1e-20 ;// FLOAT64_T
@SAY -0E9999999999999999 .0e+9999999999 0.E-999999999999999999 ;// FLOAT64_T
@SAY -8e0000000000000299 .6E+0000000299 5.e-000000000000000299 ;// FLOAT64_T
@SAY 0f -0f 0F -0f .0F -1234f 12.F 12.34f ;// FLOAT64_T
@SAY 0F32 -0f32 0f32 -0f32 .0f32 -1234F32 12.f32 12.34F32 ;// FLOAT32_T
@SAY 0f64 -0f64 0F64 -0f64 .0F64 -1234f64 12.F64 12.34f64 ;// FLOAT64_T
@SAY 0fD -0fd 0FD -0fd .0Fd -1234fD 12.FD 12.34fD ;// FLOATD_T
 
@SAY -0.E1f .0e0F 0.0E6f64 -1.e6F64 -.1E8f 12.12e44f64 -0E0F 1e20f64 ;// FLOAT64_T
@SAY -0.e+1f32 .0E+0F32 0.0e+6f32 -1.E+6F32 -.1e+8f32 12.12E+34F32 -0e+0f32 1E+20F32;// FLOAT32_T
@SAY -0.E-1fd .0e-0fD 0.0E-6Fd -1.e-6FD -.1E-8fd 12.12e-44fD -0E-0Fd 1e-20FD ;// FLOATD_T
@SAY -0E9999999999999999f32 .0e+9999999999F32 0.E-999999999999999999f32 ;// FLOAT32_T
@SAY -8e0000000000000299f .6E+0000000299f64 5.e-00000000000000000000299F64 ;// FLOAT64_T
@SAY -8e9999999999999999fD .6E+99999999999fD 5.e-12345678987654321FD ;// FLOATD_T
 
// note: _ (underscores) can appear in the main numeric part of the literal
// after the first digit and before any length suffix:
 
@SAY -10_000__f 1__0._55__ -1__.__ .0___44 1_._2__E-23F32 debug:;
 
// Underscores can also appear in the exponent, after the first digit:
 
@SAY -1_E-0__2_f32 1.e+0___5_5____ -1.0_E123_456_789_987_654_321__fD debug:;</syntaxhighlight>
 
=={{header|Vim Script}}==
Line 807 ⟶ 1,672:
 
Note that there must always be at least one digit before and after the period (and for the exponent).
 
=={{header|Visual Basic}}==
{{works with|Visual Basic|5}}
{{works with|Visual Basic|6}}
{{works with|VBA|Access 97}}
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<syntaxhighlight lang="vb">Sub Main()
Dim d As Double ' 8 Bytes, type specifier = #
Dim s As Single ' 4 Bytes, type specifier = !
d = -12.3456
d = 1000#
d = 0.00001
d = 67#
d = 8.9
d = 0.33
d = 0#
d = 2# * 10 ^ 3
d = 2E+50
d = 2E-50
s = -12.3456!
s = 1000!
s = 0.00001!
s = 67!
s = 8.9!
s = 0.33!
s = 0!
s = 2! * 10 ^ 3
End Sub</syntaxhighlight>
There is no built-in support for not-a-number, but here's a way to handle that anyway:
<syntaxhighlight lang="vb">Option Explicit
Public Declare Function RtlCompareMemory Lib "ntdll.dll" _
(ByRef Source1 As Any, ByRef Source2 As Any, ByVal Length As Long) As Long
 
Public Function IsNAN(ByRef d As Double) As Boolean
Dim d1 As Double
d1 = NaN()
IsNAN = (RtlCompareMemory(d, d1, 8) = 8)
End Function
 
Public Function NaN() As Double
On Error Resume Next ' ignore the error
NaN = 0 / 0
End Function
 
Sub Main()
Dim d1 As Double
Dim d2 As Double
d1 = NaN()
d2 = d1
Debug.Assert IsNAN(d2)
Debug.Print CStr(d2)
End Sub</syntaxhighlight>
{{out}}
<pre>-1,#IND</pre>
 
=={{header|Wren}}==
Wren has just one kind of floating point literal which must have digits both before and after the decimal point. Forms such as ''123.'' and ''.123'' are not allowed.
 
Negative zero can be represented directly but infinities or NaN cannot.
 
It is also possible to represent floating point numbers using scientific notation.
 
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="wren">var f = 123.45
var g = 0.12345 // .12345 not allowed
var h = 1.234e2
var i = -0.0
System.print([f, g, h, i])</syntaxhighlight>
 
{{out}}
<pre>
[123.45, 0.12345, 123.4, -0]
</pre>
 
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">0.
<lang XPL0>0.
.1
1e3
123.456E-300
-123_456_789e+123</langsyntaxhighlight>
 
=={{header|Z80 Assembly}}==
{{trans|6502 Assembly}}
You'll have to do it the hard way unfortunately. I used an IEEE-754 floating point calculator to figure this out.
The Z80 doesn't natively have any commands for floats, so you'll have to do it yourself.
<syntaxhighlight lang="z80">byte &DB,&0F,&49,&40 ;0x40490FDB or 3.141592654 (single-precision)</syntaxhighlight>
 
=={{header|zkl}}==
zkl requires something on both sides of the dot for a thing to be a float
<syntaxhighlight lang="zkl">1.0, 0.1, 3.1415, 1.e-100, 1.2e100, -1e10, -1e+10, 123.456E-300</syntaxhighlight>
 
 
{{omit from|Integer BASIC|You see the word "Integer" in the name, right? :-)}}
9,485

edits