Original Post
🔒 Locked
Ceil
Advertisement
This will round v to nearest multiple of q.
round() is a function that rounds to nearest integer (code taken from a MALTAB script of mine). Replace round with ceil/floor to round up/down to nearest multiple.
v = 7.1;
q = 1/16;
r = q*round(v/q);
round() is a function that rounds to nearest integer (code taken from a MALTAB script of mine). Replace round with ceil/floor to round up/down to nearest multiple.
If it''s that valuable to you, you should probably encapsulate it in a function:
double granularCeil(double input, double quantum) {
return std::ceil(input/quantum) * quantum;
}
Handling a quantum of 0 gracefully, dealing with floats (and possibly other numeric types? Do you maybe have a Rational number class you''d want to do this with?) etc. is left as an exercise for the reader.
double granularCeil(double input, double quantum) {
return std::ceil(input/quantum) * quantum;
}
Handling a quantum of 0 gracefully, dealing with floats (and possibly other numeric types? Do you maybe have a Rational number class you''d want to do this with?) etc. is left as an exercise for the reader.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement