MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / d2string

Function d2string

src/util.c:543–578  ·  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). * This function does not support human-friendly formatting like ld2string * does. It is intended mainly to be used inside t_zset.c when writing scores * into a ziplist representing a sorted set. */

Source from the content-addressed store, hash-verified

541 * does. It is intended mainly to be used inside t_zset.c when writing scores
542 * into a ziplist representing a sorted set. */
543int d2string(char *buf, size_t len, double value) {
544 if (isnan(value)) {
545 len = snprintf(buf,len,"nan");
546 } else if (isinf(value)) {
547 if (value < 0)
548 len = snprintf(buf,len,"-inf");
549 else
550 len = snprintf(buf,len,"inf");
551 } else if (value == 0) {
552 /* See: http://en.wikipedia.org/wiki/Signed_zero, "Comparisons". */
553 if (1.0/value < 0)
554 len = snprintf(buf,len,"-0");
555 else
556 len = snprintf(buf,len,"0");
557 } else {
558#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
559 /* Check if the float is in a safe range to be casted into a
560 * long long. We are assuming that long long is 64 bit here.
561 * Also we are assuming that there are no implementations around where
562 * double has precision < 52 bit.
563 *
564 * Under this assumptions we test if a double is inside an interval
565 * where casting to long long is safe. Then using two castings we
566 * make sure the decimal part is zero. If all this is true we use
567 * integer printing function that is much faster. */
568 double min = -4503599627370495; /* (2^52)-1 */
569 double max = 4503599627370496; /* -(2^52) */
570 if (value > min && value < max && value == ((double)((long long)value)))
571 len = ll2string(buf,len,(long long)value);
572 else
573#endif
574 len = snprintf(buf,len,"%.17g",value);
575 }
576
577 return len;
578}
579
580/* Create a string object from a long double.
581 * If mode is humanfriendly it does not use exponential format and trims trailing

Callers 2

zzlInsertAtFunction · 0.85

Calls 1

ll2stringFunction · 0.85

Tested by

no test coverage detected