ploot

plot images or real-time plain text from a .csv file
Log | Files | Refs | README

commit db65109a51ccebe406eb2a708d003fe68ad0af20
parent 5d691aee8b20c7148c284636e3e897fc1803a562
Author: Josuah Demangeon <mail@josuah.net>
Date:   Tue, 26 Jun 2018 21:01:18 +0200

find the scale

Diffstat:
Mlibploot/ploot_humanize.c | 18++++++++++++++----
1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/libploot/ploot_humanize.c b/libploot/ploot_humanize.c @@ -10,7 +10,7 @@ static char sys_int_suf(double *val) { - char *big = "KMGTPEZY", *small = "mun"; + char *big = " KMGTPEZY", *small = "munpfazy"; if (*val < 1.0) { for (; *val < 1.0 && small[1] != '\0'; *val *= 1000, small++); @@ -22,6 +22,19 @@ sys_int_suf(double *val) } /* + * Return the precision to use for val so that it takes 6 characters to print. + */ +int +precision(double *val) +{ + return (*val == 0) ? 0 : /* 0 */ + (*val < 1.0) ? 4 : /* 0.0001u */ + (*val < 1000) ? 2 : /* 1.00u - 10.00u - 100.00u */ + (*val < 1000000) ? 1 : /* 100000u */ + -1; /* a lot */ +} + +/* * Set 'str' to a human-readable form of 'num' with always a width of 8 (+ 1 * the '\0' terminator). Buffer overflow is ensured not to happen due to the * max size of a double. Return the exponent. @@ -31,9 +44,6 @@ ploot_humanize(char *str, double val) { int exp, precision; - precision = (ABS(val) < 10) ? 2 : (ABS(val) < 100) ? 1 : 0; - precision += (exp == 0); - snprintf(str, 9, "%+.*f %c", precision, val, label[exp]); str[8] = '\0'; if (val >= 0)