Audacity  3.0.3
Namespaces | Functions
internal Namespace Reference

Namespaces

 dtoa_impl
 
 itoa_impl
 

Functions

template<typename T >
ToCharsResult float_to_chars (char *first, char *last, T value, int digitsAfterDecimalPoint)
 

Function Documentation

◆ float_to_chars()

template<typename T >
ToCharsResult internal::float_to_chars ( char *  first,
char *  last,
value,
int  digitsAfterDecimalPoint 
)

The format of the resulting decimal representation is similar to printf's g format. Returns an iterator pointing past-the-end of the decimal representation.

Note
The input number must be finite, i.e. NaN's and Inf's are not supported.
The buffer must be large enough.
The result is NOT null-terminated.

Definition at line 1178 of file ToChars.cpp.

1180 {
1181  if (first >= last || first == nullptr)
1182  return { last, std::errc::value_too_large };
1183 
1184  if (value == 0)
1185  {
1186  *first++ = '0';
1187 
1188  return { first, std::errc() };
1189  }
1190 
1191  if (std::signbit(value))
1192  {
1193  value = -value;
1194  *first++ = '-';
1195  }
1196  // Compute v = buffer * 10^decimal_exponent.
1197  // The decimal digits are stored in the buffer, which needs to be interpreted
1198  // as an unsigned decimal integer.
1199  // len is the length of the buffer, i.e. the number of decimal digits.
1200  int len = 0;
1201  int decimal_exponent = 0;
1202  if (!dtoa_impl::grisu2(first, last, len, decimal_exponent, value))
1203  return { last, std::errc::value_too_large };
1204  // Format the buffer like printf("%.*g", prec, value)
1205  const int kMinExp = digitsAfterDecimalPoint < 0 ? -4 : -digitsAfterDecimalPoint;
1206  constexpr int kMaxExp = std::numeric_limits<double>::digits10;
1207 
1208  // Audacity specific extension imitating Internat::ToDisplayString
1209  // for a consistent behavior
1210  if (digitsAfterDecimalPoint >= 0)
1211  {
1212  if (len + decimal_exponent > 0 && -decimal_exponent > digitsAfterDecimalPoint)
1213  {
1214  const int difference = digitsAfterDecimalPoint + decimal_exponent;
1215  decimal_exponent = -digitsAfterDecimalPoint;
1216  len += difference;
1217  }
1218  }
1219 
1220  return dtoa_impl::format_buffer(
1221  first, last, len, decimal_exponent, kMinExp, kMaxExp);
1222 }

References internal::dtoa_impl::format_buffer(), and internal::dtoa_impl::grisu2().

Referenced by ToChars().

Here is the call graph for this function:
Here is the caller graph for this function:
internal::dtoa_impl::grisu2
bool grisu2(char *buf, char *last, int &len, int &decimal_exponent, FloatType value)
Definition: ToChars.cpp:982
internal::dtoa_impl::format_buffer
ToCharsResult format_buffer(char *buf, char *last, int len, int decimal_exponent, int min_exp, int max_exp)
prettify v = buf * 10^decimal_exponent If v is in the range [10^min_exp, 10^max_exp) it will be print...
Definition: ToChars.cpp:1076