TIP: Click on subject to list as thread! ANSI
echo: c_echo
to: All
from: Kurt Kuzba
date: 2004-02-15 07:27:12
subject: [C] Stepped round up

From: "Kurt Kuzba" 


From: Bruce Partridge
BP>  I have to round up to a stepped list of values in an embedded
BP>  system.  It occurs a few times, and I looking for something
BP>  that will be small/fast.

    Try using the modulus operator!

/*_|_|  stepup.c  PUBLIC DOMAIN by Kurt Kuzba - (02/15/2004)
_|_|_|  C code module to round up to a stepped integer value. _|_|_|  Code
is supplied AS IS.  No warrantees or guarantees _|_|_|  are given or
implied as to usability or appropriateness. _|_|*/

#include 
float stepup( float num, int step)
{
    long rounded = (long)num;
    if((float)rounded < num)
        rounded += (step - (rounded % step));
    return (float) rounded;
}
int main(void)
{
    printf("70.0000 stepup 5 = %.2f\n", stepup(70.0000, 5));
    printf("70.0001 stepup 5 = %.2f\n", stepup(70.0001, 5));
    printf("74.9999 stepup 5 = %.2f\n", stepup(74.9999, 5));
    return 0;
}

/*_|_|  end stepup.c
_|_|*/

>  kkuzba#centurytel.net   http://home.centurytel.net/kkuzba
>  Then the Messenger of Mordor laughed no more.

--- 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™.