added doc to README and examples

This commit is contained in:
Pavel Novikov 2025-09-29 13:00:28 +03:00
parent 01e505797b
commit 7abb574ffc
No known key found for this signature in database
GPG Key ID: F2C305CAE4167D14
2 changed files with 42 additions and 1 deletions

View File

@ -401,6 +401,23 @@ except `fast_float::integer_times_pow10()` does not report out-of-range errors,
underflows to zero or overflows to infinity when the resulting value is underflows to zero or overflows to infinity when the resulting value is
out of range. out of range.
You can use template overloads to get the result converted to different
supported floating-point types: `float`, `double`, etc.
For example, to get result as `float` use
`fast_float::integer_times_pow10<float>()` specialization:
```C++
const uint64_t W = 1234567;
const int Q = 23;
const double result = fast_float::integer_times_pow10<float>(W, Q);
std::cout.precision(7);
std::cout << "float: " << W << " * 10^" << Q << " = " << result << " ("
<< (result == 1234567e23f ? "==" : "!=") << "expected)\n";
```
outputs
```
float: 1234567 * 10^23 = 1.234567e+29 (==expected)
```
Overloads of `fast_float::integer_times_pow10()` are provided for Overloads of `fast_float::integer_times_pow10()` are provided for
signed and unsigned integer types: `int64_t`, `uint64_t`, etc. signed and unsigned integer types: `int64_t`, `uint64_t`, etc.

View File

@ -2,7 +2,7 @@
#include <iostream> #include <iostream>
int main() { void default_overload() {
const uint64_t W = 12345678901234567; const uint64_t W = 12345678901234567;
const int Q = 23; const int Q = 23;
const double result = fast_float::integer_times_pow10(W, Q); const double result = fast_float::integer_times_pow10(W, Q);
@ -10,3 +10,27 @@ int main() {
std::cout << W << " * 10^" << Q << " = " << result << " (" std::cout << W << " * 10^" << Q << " = " << result << " ("
<< (result == 12345678901234567e23 ? "==" : "!=") << "expected)\n"; << (result == 12345678901234567e23 ? "==" : "!=") << "expected)\n";
} }
void double_specialization() {
const uint64_t W = 12345678901234567;
const int Q = 23;
const double result = fast_float::integer_times_pow10<double>(W, Q);
std::cout.precision(17);
std::cout << "double: " << W << " * 10^" << Q << " = " << result << " ("
<< (result == 12345678901234567e23 ? "==" : "!=") << "expected)\n";
}
void float_specialization() {
const uint64_t W = 1234567;
const int Q = 23;
const double result = fast_float::integer_times_pow10<float>(W, Q);
std::cout.precision(7);
std::cout << "float: " << W << " * 10^" << Q << " = " << result << " ("
<< (result == 1234567e23f ? "==" : "!=") << "expected)\n";
}
int main() {
default_overload();
double_specialization();
float_specialization();
}