2

I need to parse a text file with bunch of float numbers that were created via C++'s printf("%a") and looks like:

-0x1.68p+6 -0x1.68p+7

As I understand it, this is the mantissa and exponent in hex.

Is there any way to parse this with the standard library? I could not find a f64::from_str_radix, and even if it existed, would it parse the 0x and p parts correctly?

I suppose I missed something, because the lossless conversion of f64 <-> String should be popular enough functions to include them into standard library.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
user1244932
  • 7,352
  • 5
  • 46
  • 103
  • See also [How to specify a floating point number literal using hexadecimal notation In Rust?](https://stackoverflow.com/q/63488332/155423). – Shepmaster May 10 '21 at 20:45
  • 1
    Re: "the lossless conversion of `f64` <-> `String` should be popular enough functions to include them into standard library" - You're right! The default `Debug` formatter (what you get by formatting with `{:?}`) is lossless. So is `f64::to_bits`, come to think of it. Neither of those is the format you're trying to parse, though. – trent May 10 '21 at 21:06
  • "this is the mantissa and exponent in hex." --> not quite. The `-0x1.68` is the significand in hex and the exponent `+6` is the decimal power of 2. – chux - Reinstate Monica May 13 '21 at 19:27

0 Answers0