Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / Calculate a percentage

Author
Message
Ian T
23
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 10th Jul 2003 23:25
What is the fastest way to calculate a percentage of a int in DBP?

Thanks
--Mouse

Famous Fighting Furball
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 10th Jul 2003 23:30
The trick is to multiply before you divide - usually people are taught to divide first.

Ian T
23
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 10th Jul 2003 23:34
I'll use that then . I'm not sure if my question was clear-- I was actually wondering what the fastest way was, as in what would perform fastest with thousands of these going a second. But thanks

--Mouse

Famous Fighting Furball
Mentor
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: United Kingdom
Posted: 10th Jul 2003 23:42
just test it!, stick them each in their own loop and do the same calculation over and over a few hundred thousand times (save the timer 1st) and then subtract the saved timer value from the current timer and you have a winner, if the compiler is good at optimisation then you should get the same result whatever way you do it (as long as the math dont get too involved), cheers.

Mentor.

Hamish McHaggis
23
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 11th Jul 2003 01:13
On my computer, the "right way" is 0.10621% faster than the "wrong way". Not really much in it, both 76.2-76.4 secs for 100 million for-next loops.

cuRant PRogekt: a three-de map editer
Why the hell'd you ask me for crying out loud!?!
Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jul 2003 02:36
Hamish: Actually, the right/wrong way isn't about speed - it's about getting the right answer

50 divided by 90 gives 0.55 *if* you use floating point numbers. With integers, it gives you zero.

That's why you must multiply by 100 first, and that's why it's the 'right' way.

Mouse: If you want percentages, then this is the fast way to do it with integers. However, if you aren't worried about percentages, and could maybe use 64th's or 128th's instead (or any other power of two) then you can get slightly faster calculations.
ICERGB
23
Years of Service
User Offline
Joined: 8th Nov 2002
Location: Canada
Posted: 11th Jul 2003 10:41 Edited at: 11th Jul 2003 10:42
if you bracket it, it should not require that you use division first.

fy i
Technically, you always multiply and devide all of the equation first, then add and (it will) subtract..
works!
IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 11th Jul 2003 16:47
Erm, please read my last post again for an explanation of the why, bearing in mind that this is integer maths

You'll understand then why I'm confused about what you've posted.
Ian T
23
Years of Service
User Offline
Joined: 12th Sep 2002
Location: Around
Posted: 12th Jul 2003 01:35
Thanks Ian. Much appreciated ^_^.

--Mouse

Famous Fighting Furball

Login to post a reply

Server time is: 2026-07-18 07:32:10
Your offset time is: 2026-07-18 07:32:10