aboutsummaryrefslogtreecommitdiff
path: root/unit_label.h
blob: 286407b401f2811331e14d559dc7dde0d94e7c93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/* 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;

	/* label for the unit*/
	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);