File tree Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Expand file tree Collapse file tree 3 files changed +40
-0
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -71,6 +71,7 @@ fast_float_add_cpp_test(wide_char_test)
7171fast_float_add_cpp_test(supported_chars_test)
7272fast_float_add_cpp_test(example_test)
7373fast_float_add_cpp_test(example_comma_test)
74+ fast_float_add_cpp_test(example_integer_times_pow10)
7475fast_float_add_cpp_test(basictest)
7576option (FASTFLOAT_CONSTEXPR_TESTS "Require constexpr tests (build will fail if the compiler won't support it)" OFF )
7677if (FASTFLOAT_CONSTEXPR_TESTS)
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments