Hard Light Productions Forums

Off-Topic Discussion => General Discussion => Topic started by: redsniper on September 27, 2007, 11:42:45 pm

Title: Speaking of programming, I need some help...
Post by: redsniper on September 27, 2007, 11:42:45 pm
I'm taking this n00b-level programming class where we're all learning the basics of PASCAL and I'm stumped on something.

Let's say I've got a situation where I need things to round up. That is, I can't have fractional values. Like on this one problem calculating the postage for a letter based on it's weight, it cost 30 cents for the first ounce and then 8 cents for every half ounce after that, but if you had 1.1 oz. it would cost 38 cents because you don't need a full half ounce for it to count. I couldn't find an elegant way to get this working on the problem that needed it and I managed a crude workaround instead, but now I'm further along on the assignment, and I need to know the proper way to do this.

Gracias.
Title: Re: Speaking of programming, I need some help...
Post by: Hippo on September 28, 2007, 12:45:41 am
i don't know PASCAL, but the c++ pseudocode could be:

Code: [Select]
double amount;
int tmp;
tmp = amount; //here you lose the decimals

if(tmp>amount){amount++;}

amount then becomes the base whole number + 1, or stays the same if there's no decimals


if PASCAL has issues with a double to int conversion like that, there's a slightly longer but also useful way

Code: [Select]
double amount;
for(int i = 0; i < amount; ){i++;}

i is now your rounded up number, but this becomes a much more time consuming calculation on larger numbers (ok, so barely but still, its hardly optimal)
Title: Re: Speaking of programming, I need some help...
Post by: Bobboau on September 28, 2007, 12:51:53 am
PASCAL?! :wtf:
Title: Re: Speaking of programming, I need some help...
Post by: Nuke on September 28, 2007, 08:50:02 am
when i took my newb programmer class back in like '97, they started us out on c, then we did c++, then we did vb last for some reason. sence i knew how to code graphics for vb, and i didnt for c/++, i did that for most of my time there. we never had to touch crap like pascal. but im sure if youre instructor was fond of pascal thats what youd end up doing.
Title: Re: Speaking of programming, I need some help...
Post by: Hippo on September 28, 2007, 12:42:50 pm
here's another more graceful one

tmp = amount
tmp mod 1
if tmp > 0
   amount++




or there's the useful roundup(); in cmath
Title: Re: Speaking of programming, I need some help...
Post by: Nuke on September 28, 2007, 02:28:52 pm
i usually just use floor() or ceil()
Title: Re: Speaking of programming, I need some help...
Post by: redsniper on September 28, 2007, 03:24:32 pm
I ended up doing this:
Code: [Select]
if wt > 1 then
    cost := trunc((wt - 1) * 2); {Finds the number of half ounces after
                                  the first ounce.}

 if cost < wt then     {Rounds up the the next half ounce.}
    cost := cost + 1;

trunc is truncate, which knocks the decimals off the end of any number.