Home / Docs / Specification / 29. Numerics

29.2 Literals

Release v0.4.0
ebnf
float_literal = digits "." digits [ "e" [ "-" ] exponent ] ;
digits        = digit { [ "_" ] digit } ;
exponent      = "0" | nonzero_digit { digit } ;
  1. Form. A float literal has digits on both sides of the point: 0.5, 1.0, 6.674_30e-11, 1.0e21. The integer part has no leading zero except 0 itself, and _ stands only between digits. The exponent is written with a lowercase e, an optional -, and no +.
  2. No ambiguity with methods and ranges. A point that a digit does not follow does not start a fraction, so 5.to_f64() is a method call on the integer 5, 1..5 is a range, and 1.0.sqrt() is a method call on 1.0. Each of 1., .5, 1e5, 1.e5, 1.0E5, 1.0e+5, 01.5 and 1.5e05 is a malformed literal, U0007, with an edit to the accepted form.
  3. No hex floats. A program that needs an exact bit pattern writes f64_from_bits(bits:) (§3 rule 9).
  4. Value. A literal denotes the binary64 value nearest to its exact decimal value, ties to even. 0.1 is 0x3FB999999999999A.
  5. Range. A literal that rounds to an infinity, as 1.0e309, or that is not zero and rounds to zero, as 1.0e-400, is U2031. A literal that rounds to a subnormal value is accepted.
  6. Typing. A float literal is always F64. An integer literal never takes the type F64: where F64 is expected, 1 is U2001, with an edit to 1.0.
  7. Negative numbers use unary -, which on 0.0 gives -0.0. There are no literals for NaN and the infinities: the prelude gives nan() and infinity(), and -infinity() is the negative one.
  8. The canonical form (spec/15) keeps a float literal as written.