Feel++ 0.111.0-preview.14
Finite Element Embedded Library in C++
nlohmann::detail::dtoa_impl Namespace Reference

implements the Grisu2 algorithm for binary to decimal floating-point conversion. More...

Classes

struct  boundaries
 
struct  cached_power
 
struct  diyfp
 

Functions

 __attribute__ ((__nonnull__(1))) inline void grisu2(char *buf
 v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. More...
 
template<typename FloatType >
 __attribute__ ((__nonnull__(1))) void grisu2(char *buf
 v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e. More...
 
return append_exponent (buf, n - 1)
 
const diyfp c_minus_k (cached.f, cached.e)
 
template<typename FloatType >
boundaries compute_boundaries (FloatType value)
 Compute the (normalized) diyfp representing the input number 'value' and its boundaries. More...
 
int find_largest_pow10 (const std::uint32_t n, std::uint32_t &pow10)
 For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k. More...
 
cached_power get_cached_power_for_binary_exponent (int e)
 For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c satisfies (Definition 3.2 from [1]) More...
 
 grisu2 (buf, len, decimal_exponent, w.minus, w.w, w.plus)
 
 grisu2_digit_gen (buf, len, decimal_exponent, M_minus, w, M_plus)
 
void grisu2_digit_gen (char *buffer, int &length, int &decimal_exponent, diyfp M_minus, diyfp w, diyfp M_plus)
 Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+. More...
 
void grisu2_round (char *buf, int len, std::uint64_t dist, std::uint64_t delta, std::uint64_t rest, std::uint64_t ten_k)
 
static_cast< bool >(e< 1000) ? void(0) if (e< 0)
 
 if (k<=n &&n<=max_exp)
 
 if (k==1)
 
template<typename Target , typename Source >
Target reinterpret_bits (const Source source)
 
 static_cast< bool > (static_cast< bool >(value > 0) ? void(0) :__assert_fail("value > 0", __builtin_FILE(), __builtin_LINE(), __extension__ __PRETTY_FUNCTION__))
 

Variables

* buf = static_cast<char>('0' + k / 10)
 
static_cast< bool >(m_plus.e==v.e) ? void(0) const cached_power cached = get_cached_power_for_binary_exponent(m_plus.e)
 
int int & decimal_exponent = -cached.k
 
int e
 
 else
 
auto k = static_cast<std::uint32_t>(e)
 
constexpr int kAlpha = -60
 
constexpr int kGamma = -32
 
int & len
 
int int diyfp m_minus
 
const diyfp M_minus (w_minus.f+1, w_minus.e)
 
int int diyfp diyfp diyfp m_plus
 
const diyfp M_plus (w_plus.f - 1, w_plus.e)
 
int int int int max_exp
 
int int int min_exp
 
const int n = len + decimal_exponent
 
int int diyfp diyfp v
 
int int FloatType value
 
const diyfp w = diyfp::mul(v, c_minus_k)
 
const diyfp w_minus = diyfp::mul(m_minus, c_minus_k)
 
const diyfp w_plus = diyfp::mul(m_plus, c_minus_k)
 

Detailed Description

implements the Grisu2 algorithm for binary to decimal floating-point conversion.

This implementation is a slightly modified version of the reference implementation which may be obtained from http://florian.loitsch.com/publications (bench.tar.gz).

The code is distributed under the MIT license, Copyright (c) 2009 Florian Loitsch.

For a detailed description of the algorithm see:

[1] Loitsch, "Printing Floating-Point Numbers Quickly and Accurately with Integers", Proceedings of the ACM SIGPLAN 2010 Conference on Programming Language Design and Implementation, PLDI 2010 [2] Burger, Dybvig, "Printing Floating-Point Numbers Quickly and Accurately", Proceedings of the ACM SIGPLAN 1996 Conference on Programming Language Design and Implementation, PLDI 1996

Function Documentation

◆ __attribute__() [1/2]

nlohmann::detail::dtoa_impl::__attribute__ ( (__nonnull__(1))  )

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e.

prettify v = buf * 10^decimal_exponent

appends a decimal representation of e to buf

>= max_digits10.

Returns
a pointer to the element following the exponent.
Precondition
-1000 < e < 1000

If v is in the range [10^min_exp, 10^max_exp) it will be printed in fixed-point notation. Otherwise it will be printed in exponential notation.

Precondition
min_exp < 0
max_exp > 0

◆ __attribute__() [2/2]

template<typename FloatType >
nlohmann::detail::dtoa_impl::__attribute__ ( (__nonnull__(1))  )

v = buf * 10^decimal_exponent len is the length of the buffer (number of decimal digits) The buffer must be large enough, i.e.

>= max_digits10.

◆ compute_boundaries()

template<typename FloatType >
boundaries nlohmann::detail::dtoa_impl::compute_boundaries ( FloatType  value)

Compute the (normalized) diyfp representing the input number 'value' and its boundaries.

Precondition
value must be finite and positive

◆ find_largest_pow10()

int nlohmann::detail::dtoa_impl::find_largest_pow10 ( const std::uint32_t  n,
std::uint32_t &  pow10 
)

For n != 0, returns k, such that pow10 := 10^(k-1) <= n < 10^k.

For n == 0, returns 1 and sets pow10 := 1.

◆ get_cached_power_for_binary_exponent()

cached_power nlohmann::detail::dtoa_impl::get_cached_power_for_binary_exponent ( int  e)

For a normalized diyfp w = f * 2^e, this function returns a (normalized) cached power-of-ten c = f_c * 2^e_c, such that the exponent of the product w * c satisfies (Definition 3.2 from [1])

 alpha <= e_c + e + q <= gamma.

◆ grisu2_digit_gen()

void nlohmann::detail::dtoa_impl::grisu2_digit_gen ( char *  buffer,
int &  length,
int &  decimal_exponent,
diyfp  M_minus,
diyfp  w,
diyfp  M_plus 
)

Generates V = buffer * 10^decimal_exponent, such that M- <= V <= M+.

M- and M+ must be normalized and share the same exponent -60 <= e <= -32.

References nlohmann::detail::array.

Variable Documentation

◆ e

int nlohmann::detail::dtoa_impl::e
Initial value:
{
(static_cast <bool> (e > -1000) ? void (0) : __assert_fail ("e > -1000", __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))

◆ else

nlohmann::detail::dtoa_impl::else
Initial value:
{
*buf++ = '+'

◆ m_plus

int int diyfp diyfp diyfp nlohmann::detail::dtoa_impl::m_plus
Initial value:
{
(static_cast <bool> (m_plus.e == m_minus.e) ? void (0) : __assert_fail ("m_plus.e == m_minus.e", __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))

◆ max_exp

int int int int nlohmann::detail::dtoa_impl::max_exp
Initial value:
{
(static_cast <bool> (min_exp < 0) ? void (0) : __assert_fail ("min_exp < 0", __builtin_FILE (), __builtin_LINE (), __extension__ __PRETTY_FUNCTION__))

◆ value

int int FloatType nlohmann::detail::dtoa_impl::value
Initial value:
{
static_assert(diyfp::kPrecision >= std::numeric_limits<FloatType>::digits + 3,
"internal error: not enough precision")