MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / FloatToBuffer

Function FloatToBuffer

tensorflow/core/lib/strings/numbers.cc:361–384  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

359}
360
361size_t FloatToBuffer(float value, char* buffer) {
362 // FLT_DIG is 6 for IEEE-754 floats, which are used on almost all
363 // platforms these days. Just in case some system exists where FLT_DIG
364 // is significantly larger -- and risks overflowing our buffer -- we have
365 // this assert.
366 static_assert(FLT_DIG < 10, "FLT_DIG is too big");
367
368 int snprintf_result =
369 snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG, value);
370
371 // The snprintf should never overflow because the buffer is significantly
372 // larger than the precision we asked for.
373 DCHECK(snprintf_result > 0 && snprintf_result < kFastToBufferSize);
374
375 float parsed_value;
376 if (!safe_strtof(buffer, &parsed_value) || parsed_value != value) {
377 snprintf_result =
378 snprintf(buffer, kFastToBufferSize, "%.*g", FLT_DIG + 3, value);
379
380 // Should never overflow; see above.
381 DCHECK(snprintf_result > 0 && snprintf_result < kFastToBufferSize);
382 }
383 return snprintf_result;
384}
385
386string FpToString(Fprint fp) {
387 char buf[17];

Callers 2

RoundTripFpToStringFunction · 0.85
AlphaNumMethod · 0.85

Calls 1

safe_strtofFunction · 0.85

Tested by

no test coverage detected