From 052513c7368a96b03b9a959bcb81ca7c59f47d19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 26 Dec 2018 12:44:26 +0100 Subject: [PATCH] particle/ramp: don't use floating point arithmetic --- particles/ramp.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/particles/ramp.c b/particles/ramp.c index c8c2fd5..31df037 100644 --- a/particles/ramp.c +++ b/particles/ramp.c @@ -33,13 +33,21 @@ instantiate(const struct particle *particle, const struct tag_set *tags) const struct tag *tag = tag_for_name(tags, ramp->tag); assert(tag != NULL); + assert(ramp->count > 0); + long value = tag->as_int(tag); long min = tag->min(tag); long max = tag->max(tag); - double progress = (double)value / (max - min); + assert(value >= min && value <= max); + assert(max >= min); - size_t idx = progress * ramp->count; + size_t idx = 0; + if (max - min > 0) + idx = ramp->count * value / (max - min); + + if (idx == ramp->count) + idx--; /* * printf("ramp: value: %lu, min: %lu, max: %lu, progress: %f, idx: %zu\n", * value, min, max, progress, idx);