Fix printing of zero floats and doubles (!)

This commit is contained in:
Tony Garnock-Jones 2022-11-08 20:47:50 +01:00
parent 7c4b9151ab
commit bf85504233
1 changed files with 8 additions and 8 deletions

View File

@ -208,22 +208,22 @@ impl<W: io::Write> Writer for TextWriter<W> {
}
fn write_f32(&mut self, v: f32) -> io::Result<()> {
if f32::is_normal(v) || f32::is_subnormal(v) {
if v.is_nan() || v.is_infinite() {
write!(self.w, "#xf\"{}\"",
HexFormatter::Packed.encode(&u32::to_be_bytes(f32::to_bits(v))))
} else {
dtoa::write(&mut self.w, v)?;
write!(self.w, "f")
} else {
let bs = v.to_be_bytes();
write!(self.w, "#xf\"{}\"", HexFormatter::Packed.encode(&bs))
}
}
fn write_f64(&mut self, v: f64) -> io::Result<()> {
if f64::is_normal(v) || f64::is_subnormal(v) {
if v.is_nan() || v.is_infinite() {
write!(self.w, "#xd\"{}\"",
HexFormatter::Packed.encode(&u64::to_be_bytes(f64::to_bits(v))))
} else {
dtoa::write(&mut self.w, v)?;
Ok(())
} else {
let bs = v.to_be_bytes();
write!(self.w, "#xd\"{}\"", HexFormatter::Packed.encode(&bs))
}
}