particle/ramp: don't use floating point arithmetic

This commit is contained in:
Daniel Eklöf 2018-12-26 12:44:26 +01:00
parent 1929099ca4
commit 052513c736

View file

@ -33,13 +33,21 @@ instantiate(const struct particle *particle, const struct tag_set *tags)
const struct tag *tag = tag_for_name(tags, ramp->tag); const struct tag *tag = tag_for_name(tags, ramp->tag);
assert(tag != NULL); assert(tag != NULL);
assert(ramp->count > 0);
long value = tag->as_int(tag); long value = tag->as_int(tag);
long min = tag->min(tag); long min = tag->min(tag);
long max = tag->max(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", * printf("ramp: value: %lu, min: %lu, max: %lu, progress: %f, idx: %zu\n",
* value, min, max, progress, idx); * value, min, max, progress, idx);