| TIP: Click on subject to list as thread! | ANSI |
| echo: | |
|---|---|
| to: | |
| from: | |
| date: | |
| subject: | Re: [C] Stepped round up |
From: Jerry Coffin
At 10:56 AM 1/22/2004, you wrote:
>I have to round up to a stepped list of values in an embedded system. It
>occurs a few times, and I looking for something that will be small/fast.
>
>The steps are multiples of 3, and the subject number is a float and the
>result is the nearest step that is greater than the subject_number.
>
>What I'm doing now is next_step = ((int)(subject_number / 3) * step_size) +
>step size
I'm not really happy with my solution, but it may be at least a minor improvement:
int step(float in) {
static int adds[] = {0, 2, 1};
int i=(int)in;
return i+adds[i%3];
}
At least if I understand your description correctly, this gives correct
results, and it shouldn't normally do anything in floating point (though
converting a float to an int may produce a distressing amount of code all
by itself). Of course, this will fail utterly if your input is outside the
range that can be represented in an int.
Later,
Jerry.
--- BBBS/LiI v4.01 Flag-5
* Origin: Prism's_Point (1:261/38.1)SEEN-BY: 633/267 270 @PATH: 261/38 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™.