Public Group

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

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

##### Share on other sites
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 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 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 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

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5

• 26
• 11
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633701
• Total Posts
3013445
×