| 437 | |
| 438 | template <typename Real> |
| 439 | static Result<Decimal128> FromPositiveRealApprox(Real real, int32_t precision, |
| 440 | int32_t scale) { |
| 441 | // Approximate algorithm that operates in the FP domain (thus subject |
| 442 | // to precision loss). |
| 443 | const auto x = std::nearbyint(real * PowerOfTen<double>(scale)); |
| 444 | const auto max_abs = PowerOfTen<double>(precision); |
| 445 | if (x <= -max_abs || x >= max_abs) { |
| 446 | return OverflowError(real, precision, scale); |
| 447 | } |
| 448 | // Extract high and low bits |
| 449 | const auto high = std::floor(std::ldexp(x, -64)); |
| 450 | const auto low = x - std::ldexp(high, 64); |
| 451 | |
| 452 | DCHECK_GE(high, 0); |
| 453 | DCHECK_LT(high, 9.223372036854776e+18); // 2**63 |
| 454 | DCHECK_GE(low, 0); |
| 455 | DCHECK_LT(low, 1.8446744073709552e+19); // 2**64 |
| 456 | return Decimal128(static_cast<int64_t>(high), static_cast<uint64_t>(low)); |
| 457 | } |
| 458 | |
| 459 | template <typename Real> |
| 460 | static Real ToRealPositiveNoSplit(const Decimal128& decimal, int32_t scale) { |
nothing calls this directly
no test coverage detected