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