redis prints doubles with up to 17 digits of precision, which captures the inaccuracy of many floating-point numbers (such as 0.1) By using the %g format and a precision of 5 significant digits, we avoid many awkward representations like RETURN 0.1 emitting "0.10000000000000001", though we're still subject to many of the typical issues with floating-point error
| 19 | // awkward representations like RETURN 0.1 emitting "0.10000000000000001", |
| 20 | // though we're still subject to many of the typical issues with floating-point error |
| 21 | static inline void _ReplyWithRoundedDouble |
| 22 | ( |
| 23 | RedisModuleCtx *ctx, |
| 24 | double d |
| 25 | ) { |
| 26 | // get length required to print number |
| 27 | int len = snprintf(NULL, 0, "%.5g", d); |
| 28 | char str[len + 1]; |
| 29 | sprintf(str, "%.5g", d); |
| 30 | // output string-formatted number |
| 31 | RedisModule_ReplyWithStringBuffer(ctx, str, len); |
| 32 | } |
| 33 | |
| 34 | static SlowLogItem *_SlowLogItem_New |
| 35 | ( |