Literals/Floating point: Difference between revisions

Content deleted Content added
Steenslag (talk | contribs)
→‎{{header|C}}: Added Common Lisp
Line 96:
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|Common Lisp}}==
The grammar for floating point literals in EBNF (ISO/IEC 14977):
<pre>
float = [ sign ], { decimal-digit }, decimal-point, decimal-digit, { decimal-digit }, [exponent]
| [ sign ], decimal-digit, { decimal-digit }, [ decimal-point, { decimal-digit } ], exponent ;
exponent = exponent-marker, [ sign ], decimal-digit, { decimal-digit } ;
sign = "+" | "-" ;
decimal-point = "." ;
decimal-digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
exponent-marker = "e" | "E" | "s" | "S" | "d" | "D" | "f" | "F" | "l" | "L" ;</pre>
 
Common Lisp implementations can provide up to 4 different float subtypes: short-float, single-float, double-float and long-float. The exponent marker specifies the type of the literal. "e"/"E" denotes the default floating point subtype (it is initially single-float but you can set it with the global variable <code>*READ-DEFAULT-FLOAT-FORMAT*</code> to any of the other subtypes).
The standard only recommends a minimum precision and exponent size for each subtype and an implementation doesn't have to provide all of them:
<pre>Format | Minimum Precision | Minimum Exponent Size
--------------------------------------------------
Short (s/S) | 13 bits | 5 bits
Single (f/F) | 24 bits | 8 bits
Double (d/D) | 50 bits | 8 bits
Long (l/L) | 50 bits | 8 bits</pre>
 
Some examples:
<pre>> 1.0
1.0
> -.1
-0.1
> -1e-4
-1.0E-4
> 1d2
100.0d0
> .1f3
100.0
> .001l-300
1.0L-303
</pre>
 
Note that <code>123.</code> is not a floating point number but an integer:
<pre>> (floatp 123.)
NIL
> (integerp 123.)
T</pre>
 
=={{header|D}}==