diff options
Diffstat (limited to 'unit_label.h')
-rw-r--r-- | unit_label.h | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/unit_label.h b/unit_label.h index 8f6220c..286407b 100644 --- a/unit_label.h +++ b/unit_label.h @@ -1,3 +1,4 @@ +/* struct representing a single row in the unit table mentioned above */ struct unit_label { /* number of times this unit fits into the immediate larger one */ double count; @@ -6,4 +7,24 @@ struct unit_label { char *label; }; +/* Assign *unit a unit string from table unit_label that best fits value. + * returns the value reduced by the chosen unit's magnitude + * + * Tables are worked through in order, stopping at an entry with a falsey + * label. While the value is larger than the current unit's maximum count, + * the value is divided by this count and the next unit in the table is + * examined. Example: + * {100, "flooby" }, + * {500, "glargle"}, + * { 30, "lafplop"}, // Can be any non-zero count + * { 0, NULL } // Can be any count + * + * Given this table is in `labels`, + * ret1 = make_unit_whatsit(labels, &unit1, 150); + * ret2 = make_unit_whatsit(labels, &unit2, 50); + * ret3 = make_unit_whatsit(labels, &unit3, 55000); + * leaves ret1 as 1.5 and unit1 as "gargle", + * leaves ret2 as 50 and unit2 as "flooby", + * leaves ret3 as 1.1 and unit3 as "lafplop", + */ double make_unit_whatsit(const struct unit_label l[], char **unit, double value); |