GP32 Percentages And C


Pirotic

Certified Guru
Joined
Feb 16, 2004
Messages
593
Hiya, as u may already know im currently learning C/C++ properly and coding a few projects as a learn, but i've hit something which i cannot figure out.

frame_to_draw = (int)((special_fx_frames / fx.original_duration) * fx.duration);


basically i have three values here, the number of frames total in an animation, the original length of the animation, and the amount of time left in the animation.

what im trying to do, is work out what frame to draw based on what % thru the animation it is.

(i realise that code would the animation in reverse as duration counts down)

it always returns 0, i know im doing something wrong - just not sure what, maybe i need to chuck a few '*' in or maybe i need to define it as something which allows fractions, either way the value (frame_to_draw) has to be an int.

thanks in advance
 
Pirotic posted on Apr 13 2004 at 12:51 PM said:
frame_to_draw = (int)((special_fx_frames / fx.original_duration) * fx.duration);



Does special_fx_frames / fx.original_duration result in a 0.something decimal? of course it does so it is returning a cast of zero. So what you really need is a float to contain your percentage.

:p

try changing it to the following lines;

int frame_to_draw; //if this is declared int the casting is automatic
float percent; //dont cast off a perfectly lovely decimal
percent = special_fx_frames / fx.original_duration; //percentage as float
frame_to_draw = fx.duration * percent; //casting will be automatic

also, you may need to change that / to a \ as I believe that some compilers descriminate between integer and float division.
 
Last edited by a moderator:
hiya, thanks for your suggestion.

i already tried making a float equal the fraction, then using that float variable in the calculation but to no avail.

tried using % instead of / which kinda works, so long as the fraction is never over 1, which isnt always the case.. so i wonder why it works ok with % (in theory) yet doesn't work at all with / :p
 
Hi Pirotic,

I think the problem that you're having here is that calculations involving integers are done using integer maths, so
Code:
float percent; //dont cast off a perfectly lovely decimal
percent = special_fx_frames / fx[i].original_duration; //percentage as float
is doing the division as integer, which results in zero, and then casting that to float. So having that float doesn't achieve anything at all.

A good strategy is to do all of your multiplications first, followed by divides:
Code:
frame_to_draw = (int)((special_fx_frames * fx[i].duration ) / fx[i].original_duration);
This gets the same numerical result but avoids having a really small number that gets rounded down by the integer maths.
If you do want to do the calculation with floats, you have to make sure that the compiler knows that ALL of the operands are float, otherwise it will use int. Something like this should work:
Code:
float percent; //dont cast off a perfectly lovely decimal
percent = ((float)special_fx_frames) / ((float)fx[i].original_duration); //percentage as float
Also, using "\" for division isn't, and never has been, part of C. Maybe Loridan is getting confused with another language?
 
visual basic has two types of division (\ and /) so maybe thats it

thanks for your explaination, i'll adopt that method later, silly old me doing division first so it was int'ing it to zero all the time :D
 
Yeah, / \ discrimination is also part of QBasic. I started doing it by accident in C/C++ but the compiler errors will quickly let you know what's up :)

Beware that there also some errors in rounding floating point stuff up (I've seen some 1+1 = 3 examples around) ... as Rob said it's probably best to cast everything as float instead of picking which operations to cast, it might be slower, but it'll be more accurate :)
 
hehehe / \
too many languages fighting in my head :D

another reason to hate college...
VB
 
Well, implemented it into all my animation routines now - so its possible to allow the users to specify how many frames an animation is, how long it'll take and then the engine works everything else out for them.

thanks for your help (again) everybody!
 
Back
Top