This site is updated infrequently. For up-to-date information, please visit the new OCaml website at ocaml.org.

Float literals
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
 Date: 2007-06-26 (20:06) From: Edgar Friendly Subject: Float literals
```In writing a syntax highlighter for Ocaml, I've dug into the code for
float literals:

let float_literal =
['0'-'9'] ['0'-'9' '_']*
('.' ['0'-'9' '_']* )?
(['e' 'E'] ['+' '-']? ['0'-'9'] ['0'-'9' '_']*)?

This matches what the reference manual says about float literals:

float-literal	::=	[-] (0â€¦9) { 0â€¦9âˆ£ _ } [. { 0â€¦9âˆ£ _ }] [(eâˆ£ E) [+âˆ£ -]
(0â€¦9) { 0â€¦9âˆ£ _ }]

But it doesn't match some expectations I have about float literals.  The
following are all float literals:

3.14      (* no problem here *)
6.022E23  (* no problem here *)
9.109e-31 (* still no problem *)
2.        (* a float - blank decimal part *)
1e6       (* an integer?  no, a float without a .  Problem.*)
13        (* valid as a float, according to the above definition *)

Does anyone else find the last two cases kind of odd?  Maybe it's not a
big deal because the 1e6 is scientific notation, which is float-y, and
the last one is always parsed as an integer, I assume because of
precedence in the lexing functions.

E.

```