Skip to content

Commit a9025c5

Browse files
committed
Auto merge of #74621 - LukasKalbertodt:float-docs, r=GuillaumeGomez
Improve `f32` and `f64` primitive documentation I noticed that the docs for the primitive floats were fairly short. I first only wanted to add the IEEE specification information (compare [the reference](https://doc.rust-lang.org/reference/types/numeric.html)), but then also added some more beginner-friendly docs. Let me know what you think! Random doc team assign: r? @rylev
2 parents 7189ca6 + e4d27fa commit a9025c5

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

library/std/src/primitive_docs.rs

+41-3
Original file line numberDiff line numberDiff line change
@@ -764,19 +764,57 @@ mod prim_str {}
764764
mod prim_tuple {}
765765

766766
#[doc(primitive = "f32")]
767-
/// The 32-bit floating point type.
767+
/// A 32-bit floating point type (specifically, the "binary32" type defined in IEEE 754-2008).
768+
///
769+
/// This type can represent a wide range of decimal numbers, like `3.5`, `27`,
770+
/// `-113.75`, `0.0078125`, `34359738368`, `0`, `-1`. So unlike integer types
771+
/// (like `i32`), floating point types can represent non-integer numbers, too.
772+
///
773+
/// However, being able to represent this wide range of numbers comes at the
774+
/// cost of precision: floats can only represent some of the real numbers and
775+
/// calculation with floats round to a nearby representable number. For example,
776+
/// `5.0` and `1.0` can be exactly represented as `f32`, but `1.0 / 5.0` results
777+
/// in `0.20000000298023223876953125` since `0.2` cannot be exactly represented
778+
/// as `f32`. Note however, that printing floats with `println` and friends will
779+
/// often discard insignificant digits: `println!("{}", 1.0f32 / 5.0f32)` will
780+
/// print `0.2`.
781+
///
782+
/// The precision is better for numbers near 0 and worse for large numbers. For
783+
/// example, above 2<sup>24</sup>, not even all integers are representable.
784+
///
785+
/// Additionally, `f32` can represent a couple of special values:
786+
///
787+
/// - `-0`: this is just due to how floats are encoded. It is semantically
788+
/// equivalent to `0` and `-0.0 == 0.0` results in `true`.
789+
/// - [∞](#associatedconstant.INFINITY) and
790+
/// [-∞](#associatedconstant.NEG_INFINITY): these result from calculations
791+
/// like `1.0 / 0.0`.
792+
/// - [NaN (not a number)](#associatedconstant.NAN): this value results from
793+
/// calculations like `(-1.0).sqrt()`. NaN has some potentially unexpected
794+
/// behavior: it is unequal to any float, including itself! It is also neither
795+
/// smaller nor greater than any float, making it impossible to sort. Lastly,
796+
/// it is considered infectious as almost all calculations where one of the
797+
/// operands is NaN will also result in NaN.
798+
///
799+
/// For more information on floating point numbers, see [Wikipedia][wikipedia].
768800
///
769801
/// *[See also the `std::f32::consts` module](f32/consts/index.html).*
770802
///
803+
/// [wikipedia]: https://en.wikipedia.org/wiki/Single-precision_floating-point_format
771804
#[stable(feature = "rust1", since = "1.0.0")]
772805
mod prim_f32 {}
773806

774807
#[doc(primitive = "f64")]
775-
//
776-
/// The 64-bit floating point type.
808+
/// A 64-bit floating point type (specifically, the "binary64" type defined in IEEE 754-2008).
809+
///
810+
/// This type is very similar to [`f32`](primitive.f32.html), but has increased
811+
/// precision by using twice as many bits. Please see [the documentation for
812+
/// `f32`](primitive.f32.html) or [Wikipedia on double precision
813+
/// values][wikipedia] for more information.
777814
///
778815
/// *[See also the `std::f64::consts` module](f64/consts/index.html).*
779816
///
817+
/// [wikipedia]: https://en.wikipedia.org/wiki/Double-precision_floating-point_format
780818
#[stable(feature = "rust1", since = "1.0.0")]
781819
mod prim_f64 {}
782820

0 commit comments

Comments
 (0)