Skip to content

Commit 6702cd4

Browse files
committed
added doc section in the README,
added example code test executable
1 parent 20a7383 commit 6702cd4

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,33 @@ int main() {
357357
}
358358
```
359359

360+
## Multiplication of an integer by a power of 10
361+
An integer `W` can be multiplied by a power of ten `10^Q` and
362+
converted to `double` with correctly rounded value
363+
(in "round to nearest, tie to even" fashion) using
364+
`fast_float::integer_times_pow10()`, e.g.:
365+
```C++
366+
const uint64_t W = 12345678901234567;
367+
const int Q = 23;
368+
const double result = fast_float::integer_times_pow10(W, Q);
369+
std::cout.precision(17);
370+
std::cout << W << " * 10^" << Q << " = " << result << " ("
371+
<< (result == 12345678901234567e23 ? "==" : "!=") << "expected)\n";
372+
```
373+
outputs
374+
```
375+
12345678901234567 * 10^23 = 1.2345678901234567e+39 (==expected)
376+
```
377+
`fast_float::integer_times_pow10()` gives the same result as
378+
using `fast_float::from_chars()` when parsing the string `"WeQ"`
379+
(in this example `"12345678901234567e23"`),
380+
except `fast_float::integer_times_pow10()` does not report out-of-range errors, and
381+
underflows to zero or overflows to infinity when the resulting value is
382+
out of range.
383+
384+
Overloads of `fast_float::integer_times_pow10()` are provided for
385+
signed and unsigned integer types: `int64_t`, `uint64_t`, etc.
386+
360387

361388
## Users and Related Work
362389

tests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ fast_float_add_cpp_test(wide_char_test)
7171
fast_float_add_cpp_test(supported_chars_test)
7272
fast_float_add_cpp_test(example_test)
7373
fast_float_add_cpp_test(example_comma_test)
74+
fast_float_add_cpp_test(example_integer_times_pow10)
7475
fast_float_add_cpp_test(basictest)
7576
option(FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF)
7677
if (FASTFLOAT_CONSTEXPR_TESTS)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "fast_float/fast_float.h"
2+
3+
#include <iostream>
4+
5+
int main() {
6+
const uint64_t W = 12345678901234567;
7+
const int Q = 23;
8+
const double result = fast_float::integer_times_pow10(W, Q);
9+
std::cout.precision(17);
10+
std::cout << W << " * 10^" << Q << " = " << result << " ("
11+
<< (result == 12345678901234567e23 ? "==" : "!=") << "expected)\n";
12+
}

0 commit comments

Comments
 (0)