Universal Numbers Library C++ API
sw::universal -- header-only custom arithmetic types (posit, cfloat, lns, fixpnt, dd/qd, and more)
Loading...
Searching...
No Matches
sw::universal::dragon Namespace Reference

Classes

struct  dragon_context
 Dragon context: configuration for decimal conversion. More...

Functions

void multiply_by_power_of_2 (support::decimal &d, int exp)
 multiply_by_power_of_2: multiply decimal by 2^exp using repeated doubling
void multiply_by_power_of_5 (support::decimal &d, int exp)
 multiply_by_power_of_5: multiply decimal by 5^exp
void divide_by_power_of_10 (support::decimal &d, int exp)
 divide_by_power_of_10: divide decimal by 10^exp
std::string dragon4 (const support::decimal &f, int e, int fbits, bool is_even, const dragon_context &ctx, int &decimal_exponent)
 Dragon4 algorithm: Generate digits using interval arithmetic.
int estimate_significant_bits (const support::decimal &d)
 significant_digits: estimate the number of significant bits in the mantissa This is a helper for the Dragon algorithm's initial k estimate
std::string format_decimal_string (bool sign, const std::string &digits, int decimal_exp, const dragon_context &ctx)
 format_decimal_string: format the digits and exponent according to ioflags
std::string to_decimal_string (bool sign, int scale, const support::decimal &mantissa, int fbits, std::ios_base::fmtflags flags=std::ios_base::dec, std::streamsize precision=6)
 to_decimal_string: main entry point for Dragon algorithm conversion

Function Documentation

◆ divide_by_power_of_10()

void sw::universal::dragon::divide_by_power_of_10 ( support::decimal & d,
int exp )
inline

divide_by_power_of_10: divide decimal by 10^exp

◆ dragon4()

std::string sw::universal::dragon::dragon4 ( const support::decimal & f,
int e,
int fbits,
bool is_even,
const dragon_context & ctx,
int & decimal_exponent )
inline

Dragon4 algorithm: Generate digits using interval arithmetic.

The algorithm maintains: r = numerator of the scaled value s = denominator of the scaled value mp = upper bound margin (m^+) mm = lower bound margin (m^-)

The value v = r/s, and valid decimal representations lie in the interval: (r - mm)/s to (r + mp)/s

We generate digits by computing d = floor(10r/s) and checking if we're still within the valid interval after each digit.

Parameters: f: mantissa as decimal integer e: binary exponent (value = f * 2^e) fbits: number of fraction bits in original binary representation is_even: IEEE round-to-even flag ctx: formatting context decimal_exponent: output decimal exponent

◆ estimate_significant_bits()

int sw::universal::dragon::estimate_significant_bits ( const support::decimal & d)
inline

significant_digits: estimate the number of significant bits in the mantissa This is a helper for the Dragon algorithm's initial k estimate

◆ format_decimal_string()

std::string sw::universal::dragon::format_decimal_string ( bool sign,
const std::string & digits,
int decimal_exp,
const dragon_context & ctx )
inline

format_decimal_string: format the digits and exponent according to ioflags

◆ multiply_by_power_of_2()

void sw::universal::dragon::multiply_by_power_of_2 ( support::decimal & d,
int exp )
inline

multiply_by_power_of_2: multiply decimal by 2^exp using repeated doubling

◆ multiply_by_power_of_5()

void sw::universal::dragon::multiply_by_power_of_5 ( support::decimal & d,
int exp )
inline

multiply_by_power_of_5: multiply decimal by 5^exp

◆ to_decimal_string()

std::string sw::universal::dragon::to_decimal_string ( bool sign,
int scale,
const support::decimal & mantissa,
int fbits,
std::ios_base::fmtflags flags = std::ios_base::dec,
std::streamsize precision = 6 )
inline

to_decimal_string: main entry point for Dragon algorithm conversion