------------------------------------------------------------- Float to string or buffer. Makesure buffer size >= kMaxFloatStringSize -------------------------------------------------------------
| 252 | // Makesure buffer size >= kMaxFloatStringSize |
| 253 | // ------------------------------------------------------------- |
| 254 | char* WriteFloatToBuffer(float value, char* buffer) { |
| 255 | // FLT_DIG is 6 on almost all platforms. |
| 256 | // If it's too big, the buffer will overflow |
| 257 | // STATIC_ASSERT(FLT_DIG < 10, "FLT_DIG is too big"); |
| 258 | if (value >= numeric_limits<double>::infinity()) { |
| 259 | strcpy(buffer, "inf"); // NOLINT |
| 260 | return buffer + 3; |
| 261 | } else if (value <= -numeric_limits<double>::infinity()) { |
| 262 | strcpy(buffer, "-inf"); // NOLINT |
| 263 | return buffer + 4; |
| 264 | } else if (IsNaN(value)) { |
| 265 | strcpy(buffer, "nan"); // NOLINT |
| 266 | return buffer + 3; |
| 267 | } |
| 268 | |
| 269 | return buffer + snprintf(buffer, kMaxFloatStringSize, "%.*g", FLT_DIG, value); |
| 270 | } |
| 271 | |
| 272 | char* DoubleToString(double n, char* buffer) { |
| 273 | WriteDoubleToBuffer(n, buffer); |
no test coverage detected