@@ -363,17 +363,19 @@ A _floating-point literal_ has one of two forms:
363
363
second decimal literal.
364
364
* A single _ decimal literal_ followed by an _ exponent_ .
365
365
366
- By default, a floating-point literal has a generic type, but will fall back to
367
- ` f64 ` . A floating-point literal may be followed (immediately, without any
368
- spaces) by a _ floating-point suffix_ , which changes the type of the literal.
369
- There are two floating-point suffixes: ` f32 ` , and ` f64 ` (the 32-bit and 64-bit
370
- floating point types).
366
+ By default, a floating-point literal is of type ` float ` . A
367
+ floating-point literal may be followed (immediately, without any
368
+ spaces) by a _ floating-point suffix_ , which changes the type of the
369
+ literal. There are three floating-point suffixes: ` f ` (for the base
370
+ ` float ` type), ` f32 ` , and ` f64 ` (the 32-bit and 64-bit floating point
371
+ types).
371
372
372
373
Examples of floating-point literals of various forms:
373
374
374
375
~~~~
375
- 123.0; // type f64
376
- 0.1; // type f64
376
+ 123.0; // type float
377
+ 0.1; // type float
378
+ 3f; // type float
377
379
0.1f32; // type f32
378
380
12E+99_f64; // type f64
379
381
~~~~
@@ -1177,8 +1179,8 @@ a = Cat;
1177
1179
Enumeration constructors can have either named or unnamed fields:
1178
1180
~~~~
1179
1181
enum Animal {
1180
- Dog (~str, f64 ),
1181
- Cat { name: ~str, weight: f64 }
1182
+ Dog (~str, float ),
1183
+ Cat { name: ~str, weight: float }
1182
1184
}
1183
1185
1184
1186
let mut a: Animal = Dog(~"Cocoa", 37.2);
@@ -1342,17 +1344,17 @@ For example:
1342
1344
trait Num {
1343
1345
fn from_int(n: int) -> Self;
1344
1346
}
1345
- impl Num for f64 {
1346
- fn from_int(n: int) -> f64 { n as f64 }
1347
+ impl Num for float {
1348
+ fn from_int(n: int) -> float { n as float }
1347
1349
}
1348
- let x: f64 = Num::from_int(42);
1350
+ let x: float = Num::from_int(42);
1349
1351
~~~~
1350
1352
1351
1353
Traits may inherit from other traits. For example, in
1352
1354
1353
1355
~~~~
1354
- trait Shape { fn area() -> f64 ; }
1355
- trait Circle : Shape { fn radius() -> f64 ; }
1356
+ trait Shape { fn area() -> float ; }
1357
+ trait Circle : Shape { fn radius() -> float ; }
1356
1358
~~~~
1357
1359
1358
1360
the syntax ` Circle : Shape ` means that types that implement ` Circle ` must also have an implementation for ` Shape ` .
@@ -1365,9 +1367,9 @@ methods of the supertrait may be called on values of subtrait-bound type paramet
1365
1367
Referring to the previous example of ` trait Circle : Shape ` :
1366
1368
1367
1369
~~~
1368
- # trait Shape { fn area(&self) -> f64 ; }
1369
- # trait Circle : Shape { fn radius(&self) -> f64 ; }
1370
- fn radius_times_area<T: Circle>(c: T) -> f64 {
1370
+ # trait Shape { fn area(&self) -> float ; }
1371
+ # trait Circle : Shape { fn radius(&self) -> float ; }
1372
+ fn radius_times_area<T: Circle>(c: T) -> float {
1371
1373
// `c` is both a Circle and a Shape
1372
1374
c.radius() * c.area()
1373
1375
}
@@ -1376,10 +1378,10 @@ fn radius_times_area<T: Circle>(c: T) -> f64 {
1376
1378
Likewise, supertrait methods may also be called on trait objects.
1377
1379
1378
1380
~~~ {.xfail-test}
1379
- # trait Shape { fn area(&self) -> f64 ; }
1380
- # trait Circle : Shape { fn radius(&self) -> f64 ; }
1381
- # impl Shape for int { fn area(&self) -> f64 { 0.0 } }
1382
- # impl Circle for int { fn radius(&self) -> f64 { 0.0 } }
1381
+ # trait Shape { fn area(&self) -> float ; }
1382
+ # trait Circle : Shape { fn radius(&self) -> float ; }
1383
+ # impl Shape for int { fn area(&self) -> float { 0.0 } }
1384
+ # impl Circle for int { fn radius(&self) -> float { 0.0 } }
1383
1385
# let mycircle = 0;
1384
1386
1385
1387
let mycircle: Circle = @mycircle as @Circle;
@@ -1393,14 +1395,14 @@ An _implementation_ is an item that implements a [trait](#traits) for a specific
1393
1395
Implementations are defined with the keyword ` impl ` .
1394
1396
1395
1397
~~~~
1396
- # struct Point {x: f64 , y: f64 };
1398
+ # struct Point {x: float , y: float };
1397
1399
# type Surface = int;
1398
- # struct BoundingBox {x: f64 , y: f64 , width: f64 , height: f64 };
1400
+ # struct BoundingBox {x: float , y: float , width: float , height: float };
1399
1401
# trait Shape { fn draw(&self, Surface); fn bounding_box(&self) -> BoundingBox; }
1400
1402
# fn do_draw_circle(s: Surface, c: Circle) { }
1401
1403
1402
1404
struct Circle {
1403
- radius: f64 ,
1405
+ radius: float ,
1404
1406
center: Point,
1405
1407
}
1406
1408
@@ -1968,7 +1970,7 @@ values.
1968
1970
1969
1971
~~~~~~~~ {.tuple}
1970
1972
(0,);
1971
- (0.0 , 4.5 );
1973
+ (0f , 4.5f );
1972
1974
("a", 4u, true);
1973
1975
~~~~~~~~
1974
1976
@@ -2000,12 +2002,12 @@ A _unit-like structure expression_ consists only of the [path](#paths) of a [str
2000
2002
The following are examples of structure expressions:
2001
2003
2002
2004
~~~~
2003
- # struct Point { x: f64 , y: f64 }
2004
- # struct TuplePoint(f64, f64 );
2005
+ # struct Point { x: float , y: float }
2006
+ # struct TuplePoint(float, float );
2005
2007
# mod game { pub struct User<'self> { name: &'self str, age: uint, score: uint } }
2006
2008
# struct Cookie; fn some_fn<T>(t: T) {}
2007
- Point {x: 10.0 , y: 20.0 };
2008
- TuplePoint(10.0, 20.0 );
2009
+ Point {x: 10f , y: 20f };
2010
+ TuplePoint(10f, 20f );
2009
2011
let u = game::User {name: "Joe", age: 35, score: 100_000};
2010
2012
some_fn::<Cookie>(Cookie);
2011
2013
~~~~
@@ -2246,12 +2248,12 @@ Any other cast is unsupported and will fail to compile.
2246
2248
An example of an ` as ` expression:
2247
2249
2248
2250
~~~~
2249
- # fn sum(v: &[f64 ]) -> f64 { 0.0 }
2250
- # fn len(v: &[f64 ]) -> int { 0 }
2251
+ # fn sum(v: &[float ]) -> float { 0.0 }
2252
+ # fn len(v: &[float ]) -> int { 0 }
2251
2253
2252
- fn avg(v: &[f64 ]) -> f64 {
2253
- let sum: f64 = sum(v);
2254
- let sz: f64 = len(v) as f64 ;
2254
+ fn avg(v: &[float ]) -> float {
2255
+ let sum: float = sum(v);
2256
+ let sz: float = len(v) as float ;
2255
2257
return sum / sz;
2256
2258
}
2257
2259
~~~~
@@ -2765,6 +2767,19 @@ size, in bits, is equal to the size of the rust type `uint` on the same target
2765
2767
machine.
2766
2768
2767
2769
2770
+ #### Machine-dependent floating point type
2771
+
2772
+ The Rust type ` float ` is a machine-specific type equal to one of the supported
2773
+ Rust floating-point machine types (` f32 ` or ` f64 ` ). It is the largest
2774
+ floating-point type that is directly supported by hardware on the target
2775
+ machine, or if the target machine has no floating-point hardware support, the
2776
+ largest floating-point type supported by the software floating-point library
2777
+ used to support the other floating-point machine types.
2778
+
2779
+ Note that due to the preference for hardware-supported floating-point, the
2780
+ type ` float ` may not be equal to the largest * supported* floating-point type.
2781
+
2782
+
2768
2783
### Textual types
2769
2784
2770
2785
The types ` char ` and ` str ` hold textual data.
0 commit comments