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

From: "Kurt Kuzba" 


From: Kurt Kuzba
KK>  Try using the modulus operator!
KK>  float stepup( float num, int step)
KK>  {
KK>      long rounded = (long)num;
KK>      if((float)rounded < num)
KK>          rounded += (step - (rounded % step));
KK>      return (float) rounded;
KK>  }


    Right key, wrong door.  You will have problems if your number
 is an integer value but is not at a step value.  Try testing for
 the presence of a step value!

/*_|_|  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) || (0 != (rounded % step)))
  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("71.0000 stepup 5 = %.2f\n", stepup(71.0000, 5));
    printf("73.0000 stepup 5 = %.2f\n", stepup(71.0000, 5));
    printf("74.9999 stepup 5 = %.2f\n", stepup(74.9999, 5));
    return 0;
}

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

>  kkuzba{at}centurytel.net   http://home.centurytel.net/kkuzba
>  If you look to others for fulfillment,
>  You will never truly be fulfilled.
>  (Tao Te Ching - Verse 44 )

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