0

I have extracted mantissa (significand) and exponent from a string.

For example, with a string "123.45e6" I have mantissa: 12345 and exponent: 4.

Now the trouble comes with conversion of such information to float or double. I tried to move it back to the form of string ("12345e4" from the example) and then to use std::strtof() or std::strtod() functions.

Is there any better way to do so? I would love to handle cases where float or double overflows i.e. when it is not able to handle such a big mantissa or exponent. In case of mantissa it is relatively simple as the number of bits of both float and double mantissas is well known, but what about the 10-base exponent?

no one special
  • 1,608
  • 13
  • 32
  • 1
    One way — not necessarily the best, but a decent start — is to literally just do the math: `mantissa * pow(10, exponent)`. – Steve Summit Apr 10 '23 at 17:59
  • If you need really precise result this is quite tricky task and it is surprising it was properly addressed in standard libraries (for all languages) quite recently. See [this](https://youtu.be/kw-U6smcLzk) and use it to find papers for string to float conversion (link is about converting to string). – Marek R Apr 10 '23 at 18:01
  • BTW what is wrong with standard library that you are doing that yourself? For example: [std::from_chars](https://en.cppreference.com/w/cpp/utility/from_chars)? – Marek R Apr 10 '23 at 18:03
  • @SteveSummit with mantissa * pow(10, exponent) would I be able to tell if the number fits in float or double? I need to represent entire mantissa and cannot afford to lose any bits. – no one special Apr 10 '23 at 18:18
  • @MarekR I obtained this mantissa and exponent in a different way (not from the string), so I cannot use it in first place. The example above is for simplification. – no one special Apr 10 '23 at 18:26
  • @MarekR thank you for the link. I see it is not that simple and I need to decide to compromise something. – no one special Apr 10 '23 at 18:41
  • For a nice illustration of how tricky it can be, see [this recent question](https://stackoverflow.com/questions/75930596). – Steve Summit Apr 10 '23 at 20:06
  • *would I be able to tell if the number fits in float or double?* There are a couple ways to answer that. In terms of maximum range, if the (decimal) exponent is less than 38, it'll fit in a `float`, and if it's less than `308`, it'll fit in a `double`. But as far as "not being able to afford to lose any bits", if by that you mean precision, and depending on how you look at it, you're just about guaranteed to lose a few of those. – Steve Summit Apr 11 '23 at 00:35
  • 1
    Almost no decimal fraction can be represented exactly as a binary fraction. So you should decide how much precision you need based on the problem you're solving, and decide between `float` and `double` that way. Or, you can say that `float` gives you the equivalent of *approximately* 7 decimal digits' worth of precision, and `double` gives you *approximately* 16. – Steve Summit Apr 11 '23 at 00:35
  • So if your inputs have less than 7 significant digits, you can probably represent them acceptably accurately as type `float`, as long as a range of 10³⁷ is okay. (But then, it's good advice to just always use `double` anyway.) – Steve Summit Apr 11 '23 at 00:38
  • 1
    "would I be able to tell if the number fits in float or double? I need to represent entire mantissa and cannot afford to lose any bits." in that case floating point most probably is not suitable for you. you can look for special libraries that doing what you need like bignumbers etc. – Slava Apr 11 '23 at 02:14

0 Answers0