Jump to content
  • Advertisement
Sign in to follow this  
pyroExtreme

Percentage with only integers

This topic is 2783 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi
i'm making a game demo and its for a GP2x which can't handle floats it has to emulate them which is slow. I've managed to make the demo only using integers, but for one small feature i need to be able to calculate a percentage is there a way of doing this only using integers? for example 45 / 300 = 0.15 but i get 0 because of integers and i don't really want to make a fixed point library just for this.

thanks in advance

Share this post


Link to post
Share on other sites
Advertisement
Google for 'Fixed-Point arithmetic', that will probably answer your question. It's quite simple and does not require an entire library or anything.

Edit:
My initial replay might be a bit short. My point is that if you compute/use these percentages only once, it is quite easy to use some simple fixed point arithmetic for that particular task. If you are planning on using fixed point arithmetic a lot, it might still be worth to use/write a fixed point library, but for simple calculations fixed point is not that complicated or magical and certainly don' t require a large and complex fixed point library.

Share this post


Link to post
Share on other sites
(45 * 100) / 300 = 0.15 * 100 = 15. What do you need the percentage for?
If you need to multiply it with something just divide by 100 afterward. If you do many calculations with it, it's essentially the same thing as the fixed point library would do, but if you just need one calculation doing it yourself like this is simple and fast.

Share this post


Link to post
Share on other sites
multiply by 100 then divide by 300,
(45*100)/300 = 15
It probably won't work as well with all values but thats what per-cent is, how many in each hundred. You just need to bare it in mind for all your other calculation later (dividing by 100 afterwards). I think Dietepiet's solution would probably be easier but as you say you've already emulated floats and I guess you'd have the same speed problem with the fixed point.

Share this post


Link to post
Share on other sites
I haven't emulated floating point yet , it's what i was trying to avoid but the multiply by 100 then divide by the value works fine as i only need it to the nearest percent and it is only need for one line of code :)

thanks all for your help

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!