summaryrefslogtreecommitdiffstats
path: root/src/unit.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/unit.c')
-rw-r--r--src/unit.c31
1 files changed, 20 insertions, 11 deletions
diff --git a/src/unit.c b/src/unit.c
index 4e70fbe..4bbc195 100644
--- a/src/unit.c
+++ b/src/unit.c
@@ -34,15 +34,23 @@ static LIST_HEAD(units);
static struct list_head *get_flist(struct unit *unit)
{
- static int cached = 0, div = UNIT_DEFAULT;
+ static struct list_head *cached_list = NULL;
+ int div = UNIT_DEFAULT;
- if (!cached && cfg_getbool(cfg, "use_si"))
+ if (cached_list)
+ return cached_list;
+
+ if (cfg_getbool(cfg, "use_bit"))
+ div = UNIT_BIT;
+ else if (cfg_getbool(cfg, "use_si"))
div = UNIT_SI;
- if (!list_empty(&unit->u_div[UNIT_SI]) && div == UNIT_SI)
- return &unit->u_div[UNIT_SI];
+ if (!list_empty(&unit->u_div[div]))
+ cached_list = &unit->u_div[div];
else
- return &unit->u_div[UNIT_DEFAULT];
+ cached_list = &unit->u_div[UNIT_DEFAULT];
+
+ return cached_list;
}
struct unit *unit_lookup(const char *name)
@@ -72,8 +80,8 @@ struct unit *unit_lookup(const char *name)
* If prec points to a vaild integer, a number of precision digits
* is suggested to avoid n.00 to make pretty printing easier.
*/
-uint64_t unit_divisor(uint64_t hint, struct unit *unit, char **name,
- int *prec)
+double unit_divisor(uint64_t hint, struct unit *unit, char **name,
+ int *prec)
{
struct list_head *flist = get_flist(unit);
struct fraction *f;
@@ -98,7 +106,7 @@ uint64_t unit_divisor(uint64_t hint, struct unit *unit, char **name,
return 1;
found_it:
- if (f->f_divisor == 1 && prec)
+ if (f->f_divisor == 1.0f && prec)
*prec = 0;
*name = f->f_name;
@@ -108,12 +116,13 @@ found_it:
double unit_value2str(uint64_t value, struct unit *unit,
char **name, int *prec)
{
- uint64_t div = unit_divisor(value, unit, name, prec);
+ double div = unit_divisor(value, unit, name, prec);
+ double v = (double) value;
- if (value % div == 0 && prec)
+ if (fmod(v, div) == 0.0f && prec)
*prec = 0;
- return (double) value / (double) div;
+ return v / div;
}
void fraction_free(struct fraction *f)