MCPcopy Create free account
hub / github.com/OpenAtomFoundation/pikiwidb / d2string

Function d2string

src/pstd/src/pstd_string.cc:479–516  ·  view source on GitHub ↗

Convert a double to a string representation. Returns the number of bytes * required. The representation should always be parsable by strtod(3). */

Source from the content-addressed store, hash-verified

477/* Convert a double to a string representation. Returns the number of bytes
478 * required. The representation should always be parsable by strtod(3). */
479int d2string(char* buf, size_t len, double value) {
480 if (std::isnan(value)) {
481 len = snprintf(buf, len, "nan");
482 } else if (std::isinf(value)) {
483 if (value < 0) {
484 len = snprintf(buf, len, "-inf");
485 } else {
486 len = snprintf(buf, len, "inf");
487 }
488 } else if (value == 0) {
489 /* See: http://en.wikipedia.org/wiki/Signed_zero, "Comparisons". */
490 if (1.0 / value < 0) {
491 len = snprintf(buf, len, "-0");
492 } else {
493 len = snprintf(buf, len, "0");
494 }
495 } else {
496#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
497 /* Check if the float is in a safe range to be casted into a
498 * long long. We are assuming that long long is 64 bit here.
499 * Also we are assuming that there are no implementations around where
500 * double has precision < 52 bit.
501 *
502 * Under this assumptions we test if a double is inside an interval
503 * where casting to long long is safe. Then using two castings we
504 * make sure the decimal part is zero. If all this is true we use
505 * integer printing function that is much faster. */
506 double min = -4503599627370495; /* (2^52)-1 */
507 double max = 4503599627370496; /* -(2^52) */
508 if (value > min && value < max && value == (static_cast<double>(static_cast<long long>(value)))) {
509 len = ll2string(buf, len, static_cast<long long>(value));
510 } else // NOLINT
511#endif
512 len = snprintf(buf, len, "%.17g", value);
513 }
514
515 return static_cast<int32_t>(len);
516}
517
518int string2d(const char* s, size_t slen, double* dval) {
519 char* pEnd;

Callers 6

DoMethod · 0.50
ReadCacheMethod · 0.50
DoBinlogMethod · 0.50
GetReplyInfoMethod · 0.50
DoMethod · 0.50
GetAllNeighborsFunction · 0.50

Calls 1

ll2stringFunction · 0.70

Tested by

no test coverage detected