fixed_point128 1.0.1.0
128 bit integer, fixed point and floating point arithmetic for C++20
Loading...
Searching...
No Matches
fp128_decimal.h File Reference

Exact conversion between binary128 and decimal. More...

#include <cstdint>
#include "fp128_shared.h"

Go to the source code of this file.

Classes

class  fp128::detail::basic_big_uint< Capacity >
 Fixed capacity unsigned big integer, 32 bit limbs, least significant first. More...

Namespaces

namespace  fp128
 Everything the library provides.
namespace  fp128::detail

Typedefs

using fp128::detail::big_uint = basic_big_uint<560>
 The big integer every conversion uses, 560 limbs or 17920 bits.
using fp128::detail::wide_big_uint = basic_big_uint<3072>
 The big integer that holds MAX_EXACT_DIGITS digits scaled to the smallest subnormal, about 94600 bits. Twelve kilobytes, so it is only ever allocated on the heap.

Enumerations

enum class  fp128::detail::digit_rounding : uint8_t { fp128::detail::nearest_even , fp128::detail::toward_zero , fp128::detail::away_from_zero }
 How to_decimal_digits() rounds the last digit it keeps. More...

Functions

int32_t fp128::detail::to_decimal_digits (uint64_t mantissa_low, uint64_t mantissa_high, int32_t exponent, int32_t requested, char *digits, bool *exact=nullptr, digit_rounding direction=digit_rounding::nearest_even) noexcept
 Correctly rounded decimal digits of a finite, non zero binary128 value.
template<int32_t Capacity>
bool fp128::detail::decimal_to_binary (basic_big_uint< Capacity > &value, const char *digits, int32_t count, int32_t max_kept, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra) noexcept
 Reads a decimal mantissa and exponent back to the nearest binary128.
bool fp128::detail::from_decimal_digits (const char *digits, int32_t count, int32_t exponent10, uint64_t &mantissa_low, uint64_t &mantissa_high, int32_t &exponent, uint64_t *extra=nullptr) noexcept
 Reads a decimal mantissa and exponent back to the nearest binary128.

Variables

constexpr int32_t fp128::detail::MAX_SIGNIFICANT_DIGITS = 40
 Largest number of significant digits any conversion here produces.
constexpr int32_t fp128::detail::MAX_EXACT_DIGITS = 11700
 Significant digits that settle the rounding of any decimal input exactly.
constexpr int32_t fp128::detail::MAX_OUTPUT_DIGITS = 5000
 Largest number of digits to_decimal_digits() produces.
constexpr int32_t fp128::detail::FRACTION_LIMBS = 520
 Limbs of the fraction buffer. 16640 bits reaches below the smallest subnormal's last bit.

Detailed Description

Exact conversion between binary128 and decimal.

A binary128 value is a 113 bit integer times a power of two, so its decimal expansion is finite: at most 4933 digits before the point and 16494 after it. Everything here works with that expansion in full rather than with an approximation of it, which is what lets the digits be correctly rounded at any requested precision and lets a decimal string be read back to the nearest representable value.

The alternative - scaling by a power of ten held in the type itself - cannot do either. A negative power of ten is not representable in binary, so every scaling rounds, and the error compounds across the digits. That is what the previous conversions did, and it left the string form of a value about an ulp away from the value and unable to survive a round trip.

These are implementation details of float128, not part of the interface.

See also
float128.h

Definition in file fp128_decimal.h.