Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
internal::float_writer< Char > Class Template Reference
Collaboration diagram for internal::float_writer< Char >:

Public Member Functions

 float_writer (const char *digits, int num_digits, int exp, float_specs specs, Char decimal_point)
size_t size () const
size_t width () const
template<typename It>
void operator() (It &&it)

Private Member Functions

template<typename It>
It prettify (It it) const

Private Attributes

const char * digits_
int num_digits_
int exp_
size_t size_
float_specs specs_
Char decimal_point_

Constructor & Destructor Documentation

◆ float_writer()

template<typename Char>
internal::float_writer< Char >::float_writer ( const char * digits,
int num_digits,
int exp,
float_specs specs,
Char decimal_point )
inline
17423 : digits_(digits),
17424 num_digits_(num_digits),
17425 exp_(exp),
17426 specs_(specs),
17427 decimal_point_(decimal_point) {
17428 int full_exp = num_digits + exp - 1;
17429 int precision = specs.precision > 0 ? specs.precision : 16;
17430 if (specs_.format == float_format::general &&
17431 !(full_exp >= -4 && full_exp < precision)) {
17432 specs_.format = float_format::exp;
17433 }
17434 size_ = prettify(counting_iterator()).count();
17435 size_ += specs.sign ? 1 : 0;
17436 }
Definition duckdb.cpp:17302

Member Function Documentation

◆ prettify()

template<typename Char>
template<typename It>
It internal::float_writer< Char >::prettify ( It it) const
inlineprivate
17312 {
17313 // pow(10, full_exp - 1) <= v <= pow(10, full_exp).
17314 int full_exp = num_digits_ + exp_;
17315 if (specs_.format == float_format::exp) {
17316 // Insert a decimal point after the first digit and add an exponent.
17317 *it++ = static_cast<Char>(*digits_);
17318 int num_zeros = specs_.precision - num_digits_;
17319 bool trailing_zeros = num_zeros > 0 && specs_.trailing_zeros;
17320 if (num_digits_ > 1 || trailing_zeros) *it++ = decimal_point_;
17321 it = copy_str<Char>(digits_ + 1, digits_ + num_digits_, it);
17322 if (trailing_zeros)
17323 it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
17324 *it++ = static_cast<Char>(specs_.upper ? 'E' : 'e');
17325 return write_exponent<Char>(full_exp - 1, it);
17326 }
17327 if (num_digits_ <= full_exp) {
17328 // 1234e7 -> 12340000000[.0+]
17329 if (specs_.thousand_sep != '\0' && full_exp > 3) {
17330 // thousand separator
17331 // we need to print the thousand separator every 3 digits
17332 // the loop is essentially write 3 digits, write the thousand separator
17333 // the exception is the first batch of digits which are 1-3 digits
17334 int digit_count = full_exp % 3 == 0 ? 3 : full_exp % 3;
17335 for(int i = 0; i < full_exp; i += digit_count, digit_count = 3) {
17336 if (i > 0) {
17337 *it++ = specs_.thousand_sep;
17338 }
17339 if (i < num_digits_) {
17340 // we still need to write digits
17341 int write_count = std::min<int>(num_digits_ - i, digit_count);
17342 it = copy_str<Char>(digits_ + i, digits_ + i + write_count, it);
17343 if (write_count < digit_count) {
17344 // write any trailing zeros that belong to this batch
17345 it = std::fill_n(it, digit_count - write_count, static_cast<Char>('0'));
17346 }
17347 } else {
17348 // we only need to write trailing zeros
17349 it = std::fill_n(it, digit_count, static_cast<Char>('0'));
17350 }
17351 }
17352 } else {
17353 it = copy_str<Char>(digits_, digits_ + num_digits_, it);
17354 it = std::fill_n(it, full_exp - num_digits_, static_cast<Char>('0'));
17355 }
17356 if (specs_.trailing_zeros) {
17357 *it++ = decimal_point_;
17358 int num_zeros = specs_.precision - full_exp;
17359 if (num_zeros <= 0) {
17360 if (specs_.format != float_format::fixed)
17361 *it++ = static_cast<Char>('0');
17362 return it;
17363 }
17364#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
17365 if (num_zeros > 1000)
17366 throw std::runtime_error("fuzz mode - avoiding excessive cpu use");
17367#endif
17368 it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
17369 }
17370 } else if (full_exp > 0) {
17371 // 1234e-2 -> 12.34[0+]
17372 if (specs_.thousand_sep != '\0' && full_exp > 3) {
17373 // thousand separator
17374 // we need to print the thousand separator every 3 digits
17375 // the loop is essentially write 3 digits, write the thousand separator
17376 // the exception is the first batch of digits which are 1-3 digits
17377 int digit_count = full_exp % 3 == 0 ? 3 : full_exp % 3;
17378 for(int i = 0; i < full_exp; i += digit_count, digit_count = 3) {
17379 if (i > 0) {
17380 *it++ = specs_.thousand_sep;
17381 }
17382 it = copy_str<Char>(digits_ + i, digits_ + i + digit_count, it);
17383 }
17384 } else {
17385 it = copy_str<Char>(digits_, digits_ + full_exp, it);
17386 }
17387 if (!specs_.trailing_zeros) {
17388 // Remove trailing zeros.
17389 int num_digits = num_digits_;
17390 while (num_digits > full_exp && digits_[num_digits - 1] == '0')
17391 --num_digits;
17392 if (num_digits != full_exp) *it++ = decimal_point_;
17393 return copy_str<Char>(digits_ + full_exp, digits_ + num_digits, it);
17394 }
17395 *it++ = decimal_point_;
17396 it = copy_str<Char>(digits_ + full_exp, digits_ + num_digits_, it);
17397 if (specs_.precision > num_digits_) {
17398 // Add trailing zeros.
17399 int num_zeros = specs_.precision - num_digits_;
17400 it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
17401 }
17402 } else {
17403 // 1234e-6 -> 0.001234
17404 *it++ = static_cast<Char>('0');
17405 int num_zeros = -full_exp;
17406 if (num_digits_ == 0 && specs_.precision >= 0 && specs_.precision < num_zeros)
17407 num_zeros = specs_.precision;
17408 int num_digits = num_digits_;
17409 if (!specs_.trailing_zeros)
17410 while (num_digits > 0 && digits_[num_digits - 1] == '0') --num_digits;
17411 if (num_zeros != 0 || num_digits != 0) {
17412 *it++ = decimal_point_;
17413 it = std::fill_n(it, num_zeros, static_cast<Char>('0'));
17414 it = copy_str<Char>(digits_, digits_ + num_digits, it);
17415 }
17416 }
17417 return it;
17418 }

◆ size()

template<typename Char>
size_t internal::float_writer< Char >::size ( ) const
inline
17438{ return size_; }

◆ width()

template<typename Char>
size_t internal::float_writer< Char >::width ( ) const
inline
17439{ return size(); }

◆ operator()()

template<typename Char>
template<typename It>
void internal::float_writer< Char >::operator() ( It && it)
inline
17441 {
17442 if (specs_.sign) *it++ = static_cast<Char>(data::signs[specs_.sign]);
17443 it = prettify(it);
17444 }

The documentation for this class was generated from the following file: