| 1413 | /// Here "exact value" means the closest representable value by Real. |
| 1414 | template <typename Real> |
| 1415 | static Real ToRealPositive(const Decimal256& decimal, int32_t scale) { |
| 1416 | const auto parts_le = decimal.little_endian_array(); |
| 1417 | if (scale <= 0 || (parts_le[3] == 0 && parts_le[2] == 0 && parts_le[1] == 0 && |
| 1418 | parts_le[0] < RealTraits<Real>::kMaxPreciseInteger)) { |
| 1419 | // No need to split the decimal if it is already an integer (scale <= 0) or if it |
| 1420 | // can be precisely represented by Real |
| 1421 | return ToRealPositiveNoSplit<Real>(decimal, scale); |
| 1422 | } |
| 1423 | |
| 1424 | // Split the decimal into whole and fractional parts to avoid precision loss |
| 1425 | BasicDecimal256 whole_decimal, fraction_decimal; |
| 1426 | decimal.GetWholeAndFraction(scale, &whole_decimal, &fraction_decimal); |
| 1427 | |
| 1428 | Real whole = ToRealPositiveNoSplit<Real>(whole_decimal, 0); |
| 1429 | Real fraction = ToRealPositiveNoSplit<Real>(fraction_decimal, scale); |
| 1430 | return whole + fraction; |
| 1431 | } |
| 1432 | }; |
| 1433 | |
| 1434 | } // namespace |
nothing calls this directly
no test coverage detected