aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoseph Elliott Hand <jhand@hawaii.edu>2026-05-01 14:47:27 -1000
committerJoseph Elliott Hand <jhand@hawaii.edu>2026-05-01 14:47:27 -1000
commit5de6eb0fe2c09dcc7decebf027085a2efbc92dfd (patch)
treeac66bf4fb4b5947d261b0c3d74f0fc000c2d22f0
parent2ecb3168cef03de02ecb10849afabd8997872f8b (diff)
downloadunit.h-5de6eb0fe2c09dcc7decebf027085a2efbc92dfd.tar.gz
Fix units in denominator without exponents not being inverted
Signed-off-by: Joseph Elliott Hand <jhand@hawaii.edu>
-rw-r--r--unit.h8
1 files changed, 4 insertions, 4 deletions
diff --git a/unit.h b/unit.h
index fdb5a9a..a4c8b9d 100644
--- a/unit.h
+++ b/unit.h
@@ -169,7 +169,7 @@ get_unit_value(char const * string) {
for (; i < token_count;) {
if (tokens[i].type == ONE) {
- if (i == 0 && tokens[1].type == SLASH) {
+ if (i == 0 && token_count > 1 && tokens[1].type == SLASH) {
exp_sign = -1.0;
i += 2;
continue;
@@ -241,13 +241,13 @@ get_unit_value(char const * string) {
unit_found:
- if (tokens[i + 1].type != EXP) {
- value *= unit_value;
+ if (token_count <= i+1 || tokens[i + 1].type != EXP) {
+ value *= pow(unit_value, exp_sign);
i += 1;
continue;
}
- if (tokens[i + 2].type != NUMBER) goto error;
+ if (token_count <= i+2 || tokens[i + 2].type != NUMBER) goto error;
value *= pow(unit_value, exp_sign * tokens[i + 2].value.num);
i += 3;