From 00d8d7bc04a882926829c65b9d6ff6448b22cfef Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Sat, 17 Oct 2015 16:27:31 -0700 Subject: Implement conversion traits for primitive float types --- src/libcore/num/mod.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'src/libcore') diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 8b005b2f8ba..38ad19c3cbe 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1473,14 +1473,14 @@ impl fmt::Display for ParseIntError { pub use num::dec2flt::ParseFloatError; -// Conversion traits for primitive integer types +// Conversion traits for primitive integer and float types // Conversions T -> T are covered by a blanket impl and therefore excluded // Some conversions from and to usize/isize are not implemented due to portability concerns macro_rules! impl_from { ($Small: ty, $Large: ty) => { - #[stable(feature = "lossless_int_conv", since = "1.5.0")] + #[stable(feature = "lossless_prim_conv", since = "1.5.0")] impl From<$Small> for $Large { - #[stable(feature = "lossless_int_conv", since = "1.5.0")] + #[stable(feature = "lossless_prim_conv", since = "1.5.0")] #[inline] fn from(small: $Small) -> $Large { small as $Large @@ -1514,3 +1514,20 @@ impl_from! { u8, i64 } impl_from! { u16, i32 } impl_from! { u16, i64 } impl_from! { u32, i64 } + +// Signed -> Float +impl_from! { i8, f32 } +impl_from! { i8, f64 } +impl_from! { i16, f32 } +impl_from! { i16, f64 } +impl_from! { i32, f64 } + +// Unsigned -> Float +impl_from! { u8, f32 } +impl_from! { u8, f64 } +impl_from! { u16, f32 } +impl_from! { u16, f64 } +impl_from! { u32, f64 } + +// Float -> Float +impl_from! { f32, f64 } -- cgit 1.4.1-3-g733a5 From 1a19f9877ad1d582ac80f8e765eff879e00222ea Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 28 Oct 2015 16:05:51 -0700 Subject: Comment how the significand limits lossless int->float conversion --- src/libcore/num/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/libcore') diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 38ad19c3cbe..a79bdd5961e 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -1515,6 +1515,10 @@ impl_from! { u16, i32 } impl_from! { u16, i64 } impl_from! { u32, i64 } +// Note: integers can only be represented with full precision in a float if +// they fit in the significand, which is 24 bits in f32 and 53 bits in f64. +// Lossy float conversions are not implemented at this time. + // Signed -> Float impl_from! { i8, f32 } impl_from! { i8, f64 } -- cgit 1.4.1-3-g733a5