Skip to content

Commit af35a77

Browse files
Describe the effect of floating-point literal suffixes in literal-expr.md rather than tokens.md
1 parent 15204c0 commit af35a77

File tree

2 files changed

+35
-25
lines changed

2 files changed

+35
-25
lines changed

src/expressions/literal-expr.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,33 @@ let a: u64 = 123; // type u64
5959
0usize; // type usize
6060
```
6161

62+
## Floating-point literal expressions
63+
64+
A floating-point literal expression consists of a single [FLOAT_LITERAL] token.
65+
66+
If the token has a [suffix], the suffix will be the name of one of the [primitive floating-point types][floating-point types]: `f32` or `f64`, and the expression has that type.
67+
68+
If the token has no suffix, the expression's type is determined by type inference:
69+
70+
* If a floating-point type can be _uniquely_ determined from the surrounding program context, the expression has that type.
71+
72+
* If the program context under-constrains the type, it defaults to `f64`.
73+
74+
* If the program context over-constrains the type, it is considered a static type error.
75+
76+
Examples of floating-point literal expressions:
77+
78+
```rust
79+
123.0f64; // type f64
80+
0.1f64; // type f64
81+
0.1f32; // type f32
82+
12E+99_f64; // type f64
83+
5f32; // type f32
84+
let x: f64 = 2.; // type f64
85+
```
86+
6287
[constant expression]: ../const_eval.md#constant-expressions
88+
[floating-point types]: ../types/numeric.md#floating-point-types
6389
[literal tokens]: ../tokens.md#literals
6490
[numeric types]: ../types/numeric.md
6591
[suffix]: ../tokens.md#suffixes

src/tokens.md

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -465,40 +465,24 @@ A _floating-point literal_ has one of two forms:
465465

466466
Like integer literals, a floating-point literal may be followed by a
467467
suffix, so long as the pre-suffix part does not end with `U+002E` (`.`).
468-
The suffix forcibly sets the type of the literal. There are two valid
469-
_floating-point suffixes_, `f32` and `f64` (the 32-bit and 64-bit floating point
470-
types), which explicitly determine the type of the literal.
471-
472-
The type of an _unsuffixed_ floating-point literal is determined by
473-
type inference:
474-
475-
* If a floating-point type can be _uniquely_ determined from the
476-
surrounding program context, the unsuffixed floating-point literal
477-
has that type.
478-
479-
* If the program context under-constrains the type, it defaults to `f64`.
480-
481-
* If the program context over-constrains the type, it is considered a
482-
static type error.
468+
There are two valid _floating-point suffixes_: `f32` and `f64` (the names of the 32-bit and 64-bit [primitive floating-point types][floating-point types]).
469+
See [literal expressions] for the effect of these suffixes.
483470

484471
Examples of floating-point literals of various forms:
485472

486473
```rust
487-
123.0f64; // type f64
488-
0.1f64; // type f64
489-
0.1f32; // type f32
490-
12E+99_f64; // type f64
491-
5f32; // type f32
492-
let x: f64 = 2.; // type f64
474+
123.0f64;
475+
0.1f64;
476+
0.1f32;
477+
12E+99_f64;
478+
5f32;
479+
let x: f64 = 2.;
493480
```
494481

495482
This last example is different because it is not possible to use the suffix
496483
syntax with a floating point literal ending in a period. `2.f64` would attempt
497484
to call a method named `f64` on `2`.
498485

499-
The representation semantics of floating-point numbers are described in
500-
["Machine Types"][machine types].
501-
502486
### Boolean literals
503487

504488
> **<sup>Lexer</sup>**\
@@ -642,6 +626,7 @@ Similarly the `r`, `b`, and `br` prefixes used in raw string literals, byte lite
642626
[extern crates]: items/extern-crates.md
643627
[extern]: items/external-blocks.md
644628
[field]: expressions/field-expr.md
629+
[floating-point types]: types/numeric.md#floating-point-types
645630
[function pointer type]: types/function-pointer.md
646631
[functions]: items/functions.md
647632
[generics]: items/generics.md
@@ -651,7 +636,6 @@ Similarly the `r`, `b`, and `br` prefixes used in raw string literals, byte lite
651636
[lazy-bool]: expressions/operator-expr.md#lazy-boolean-operators
652637
[literal expressions]: expressions/literal-expr.md
653638
[loop labels]: expressions/loop-expr.md
654-
[machine types]: types/numeric.md
655639
[macros]: macros-by-example.md
656640
[match]: expressions/match-expr.md
657641
[negation]: expressions/operator-expr.md#negation-operators

0 commit comments

Comments
 (0)