| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | [C] Stepped round up |
Hi Bruce.
22-Jan-04 09:56:42, Bruce Partridge wrote to All
BP> From: Bruce Partridge
BP> I have to round up to a stepped list of values in an embedded system. It
BP>
BP> occurs a few times, and I looking for something that will be small/fast
BP> The steps are multiples of 3, and the subject number is a float and the
BP> result
BP> is the nearest step that is greater than the subject_number
BP> What I'm doing now is
next_step = ((int)(subject_number / 3) * step_size) + step_size
by 3 above do you mean step_size ?
BP> This code generates a bunch of assembler code (including a float
BP> division) for what seems to be a simple requirement
next_step = (((int)subject_number / 3) * step_size) + step size
should cure the float divide problem, now it should convert the float to an
int and do an integer divide
BP> This code will also falsely set the next_step one step too high if the
BP> subject_number happened to actually hit exactly on a step
so you want next_step to be 6 when subject_number is 6 ?
next_step = ((((int)subject_number -1) / 3) * step_size) + step_size
BP> I've checked the usual places like "Hackers Delight", but
haven't found
BP> an inspired solution yet
This assumes an int is big enough to hold the range of allowable subject
numbers.
assuming 3 above means step_size or 3 == step_size always
here's another way to do it.
next_step = subject_number +
(step_size-1 -(int)(subject_number-1)%step_size)
and another.
next_step = subject_number + step_size-1;
next_step -= next_step % step_size
remove the -1 in the bove examples if you want next_step tp be 9 when
subject_number is 6
by using the modulus operator (%) I avoid the need to multiply...
however when reduced to assembler modulus still does the equivalent of an
integer division.
-=> Bye <=-
---
* Origin: One less than the checksum of "Jasen Betts" (3:640/1042)SEEN-BY: 633/267 270 @PATH: 640/1042 531 954 774/605 123/500 106/2000 633/267 |
|
| SOURCE: echomail via fidonet.ozzmosis.com | |
Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.