0

There are many answers on how to separate fractional part from integer part in float, but is there a way to efficiently assemble float from 2 integer numbers? For example, 123 and 012345 will produce 123.012345 (doesn't have to be exact)

This question was after reading "How to parse space-separated floats in c++ quickly" where the linked answer uses the following approach to assemble floats:

  1. using strtol extract number from left of the '.' (will be the integer), extract the number on the right of the '.' (will be the fractional part)
  2. using the length of second number, multiply the number by 0.1, 0.01, 0.001, 0.0001 depending on its length
  3. add left to right.

Is there a more efficient approach (in c++ 14 and above)? Currently it requires multiplication and addition, which can play role when assembling hundreds of millions of such numbers.

... Something that would set exponent and mantissa directly?

Edit:

regarding distinguishing 012345 from 12345 when it's in the int form, the author of the above link used strtol, so I can compute the length we travelled while parsing. So I would know that the number is 6 digits long (for example), even though I got 12345

Kari
  • 1,244
  • 1
  • 13
  • 27
  • Multiplication and addition are fast, very fast. By the way, don't forget about https://linux.die.net/man/3/strtof – erik258 Dec 30 '17 at 01:27
  • 1
    Following the link in my question its author found out that strod and strof is at least 2ce slower than the above approach – Kari Dec 30 '17 at 01:43
  • In what form do you have the second number? If it is an integer, you cannot distinguish 012345 from 12345. Those numerals represent the same number. So you would not know whether to make .012345 or .12345. If you have got a string, you could convert it to floating-point (as an integer value) and then divide it by ten to the power of the number of digits. Then just add the first number. – Eric Postpischil Dec 30 '17 at 01:54
  • 2
    You should be aware that it is impossible to produce 123.012345 in most C++ implementations. That is because most implementations use binary floating-point, and 123.012345 is not exact representable in binary floating-point. The closest you can get in IEEE-754 64-bit binary floating-point is 123.0123449999999962756191962398588657379150390625. Do you need to be exact? – Eric Postpischil Dec 30 '17 at 01:55
  • @EricPostpischil I don't require to be exact :) – Kari Dec 30 '17 at 01:57
  • What is the real problem you are trying to solve? Why do you have a need to assemble numbers this way? – Eric Postpischil Dec 30 '17 at 01:57
  • regarding distinguishing 012345 from 12345, the author of the link (in my question) used strtol, so I can compute the length we travelled while parsing. So I would know that the number is 6 digits long (for example), even though I got 12345 – Kari Dec 30 '17 at 01:59
  • Its an interesting question, sorry I missed your link. If you're going for repeatable speed, have you thought about whether your data could be in binary form rather than string/decimal? That would take out the need to convert all together. – erik258 Dec 30 '17 at 02:07
  • 2
    So the actual question is: Given integers *a*, *b*, and *c* (which is non-negative), what is an efficient way to compute *a* + *b* / 10\*\**c* (allowing reasonable floating-point rounding errors)? Note that C++ does not offer performance guarantees, so this cannot be answered definitively for C++. It may be possible to recommend general techniques that are good in typical implementations, or to target specific implementations. – Eric Postpischil Dec 30 '17 at 02:09
  • binary format would indeed be the fastest and the most compact (no need for formatting, etc), however I am interested if it's possible to still do it if formatting has to be present. – Kari Dec 30 '17 at 02:14
  • @EricPostpischil, correct - *a* could be negative, but *b*and *c* are positive – Kari Dec 30 '17 at 02:16
  • Here's somebody else thinking about modifying floats directly, so to speak https://stackoverflow.com/questions/12342926/casting-float-to-int-bitwise-in-c But I wonder how different that could possibly be from the C to do it? – erik258 Dec 30 '17 at 05:28

0 Answers0