Skip to content

Implement fmt::Binary for f32 and f64 #49066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug};
use fmt::{Formatter, Result, LowerExp, UpperExp, Display, Debug, Binary};
use mem;
use num::flt2dec;
use num::{flt2dec, Float};

// Don't inline this so callers don't use the stack space this function
// requires unless they have to.
Expand Down Expand Up @@ -150,6 +150,13 @@ macro_rules! floating {
float_to_exponential_common(fmt, self, true)
}
}

#[stable(feature = "floating_point_binary_fmt", since = "1.26.0")]
impl Binary for $ty {
fn fmt(&self, fmt: &mut Formatter) -> Result {
write!(fmt, "{:01$b}", self.to_bits(), 8 * mem::size_of::<$ty>())
}
}
)
}

Expand Down