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 / how to work out a percentage - why won't this work?

Author
Message
fatbiffa
23
Years of Service
User Offline
Joined: 15th Jan 2003
Location:
Posted: 16th May 2003 16:28
x=10
y=20
print "percentage=";(x/y)*100
heart of gold, nerves of steel, nob of butter
Beta1
23
Years of Service
User Offline
Joined: 28th Dec 2002
Location:
Posted: 16th May 2003 16:30
Its the old integer/float problem again.

x and y are both integers therefore the result of x/y will also be an integer. As 10/20=0.5 as an integer this = 0, hence you always get 0 as the result.

If however you change x to x# and y yo y# throughout to make them both floats then the answer should be correct (nb I havnt tested that cos I'm at work but I'm pretty sure thats the problem)

IanM
Retired Moderator
23
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 16th May 2003 16:54
That's one way to do it, but it's a bit drastic.

Here is another way - force the conversion to float yourself.

Two ways come to mind. First this :
( (x+0.0) / y ) * 100

And also this (a quick re-arrange of the numbers you have) :
( x * 100.0 ) / y
Andy Igoe
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: United Kingdom
Posted: 16th May 2003 16:54
numberBeingTested=remstart go figure this one out for yourself remend

numericalScale=100

maximumResult=remstart The highest value that numberBeingTested might reach remend


percentage=( numberBeingTested * numericalScale ) / maximumResult

Pneumatic Dryll
APEXnow
Retired Moderator
23
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 16th May 2003 17:15
I've been seeing this all over the place, using the old variable symbol assignments etc. It is possible to use methods similar to Visual Basic in DarkBASIC Professional when declaring variables.

Global variables can be declared using these conventions.

Global intVariable1 As Integer
Global fVariable2 As Float
Global strVariable3 As String


As you can see, variables used throughout the application do not need to use the symbols as DarkBASIC Professional knows how they were declared.

The same goes for inline variable declaration in functions.

FUNCTION MyFunc( intVar1 As Integer, strText As String)
intResult As Integer
strDescription As String

Rem function code here

ENDFUNCTION intResult


If you use these tactics, it will avoid potential math bugs like the one above.

"Man who looses key to woman's appartment...... He get no nookie" - A wise chinese man.
Van B
Moderator
23
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 16th May 2003 17:47
I always liked that about VB, the way it does'nt really care about variable format, I had no idea you could do that in pro too - nice one!


Van-B

Hate me clown because I'm not from your town y'all. Hate me clown.
fatbiffa
23
Years of Service
User Offline
Joined: 15th Jan 2003
Location:
Posted: 16th May 2003 18:33
ah, I am enlightened.... so I can ;-

x#=10
y#=50
print "percentage=";(x#/y#)*100

dat works!

ta very much.

heart of gold, nerves of steel, nob of butter
Beta1
23
Years of Service
User Offline
Joined: 28th Dec 2002
Location:
Posted: 16th May 2003 18:54
yup, but the other guys suggestions will also work.

remeber though float calculations are slower than integer so comverting x and y to floats all the way through your programs may not be the best solution.

Dryll's solution allows percentage calculations to be done using integers which will be faster but in its current form will only give the percentage without decimals.

Nilrem
23
Years of Service
User Offline
Joined: 27th Feb 2003
Location: United Kingdom
Posted: 16th May 2003 22:14
Beta1 they aren't that much slower. Not according to the beginners book.

I hear and I forget. I see and I remember. I do and I understand.
Andy Igoe
23
Years of Service
User Offline
Joined: 6th Oct 2002
Location: United Kingdom
Posted: 17th May 2003 02:57
Quote: "remeber though float calculations are slower than integer so comverting x and y to floats all the way through your programs may not be the best solution."


Actually I was suprised to find that when writting my standard range functions they performed faster with real numbers than integers, it seems spreading the load between the CPU and the FPU speeds the program up more than the hit of floating point mathematics slows things down.

Pneumatic Dryll

Login to post a reply

Server time is: 2026-07-11 03:38:20
Your offset time is: 2026-07-11 03:38:20