Skip to content

Commit

Permalink
formats: fallback to scientific notation when out of range
Browse files Browse the repository at this point in the history
Signed-off-by: He Xian <[email protected]>
  • Loading branch information
hexian000 committed Nov 24, 2024
1 parent 41a1ee0 commit 5ef83d4
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions contrib/csnippets/utils/formats.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ int format_si_prefix(char *buf, const size_t bufsize, const double value)
if (!isnormal(value)) {
return snprintf(buf, bufsize, "%.0f", value);
}
if (!(1e-30 < value && value < 1e+31)) {
return snprintf(buf, bufsize, "%.2e", value);
}
const int e = (int)floor(log10(fabs(value)) / 3.0);
if (e == 0) {
return snprintf(buf, bufsize, "%.3g", value);
Expand Down

0 comments on commit 5ef83d4

Please sign in to comment.