Advertisement

Power in VB

Started by February 24, 2001 10:15 PM
15 comments, last by Quantum 23 years, 11 months ago
Now that''s weird. Cause the one I wrote is giving me the answers I want. lol.
Using ^ WORKS

What were you writing so it did not work? The only thing you have to be careful about is that it has a higher precedence than *,/,\,MOD,+,-. (as it is normal in mathematics)

216 is written as 2 ^ 16. That''s all.

I''m interested in when it isn''t working as I''ve never had problems.

- JQ
Infiltration: Losing Ground
"You just don''t understand. Guys have to be immature and stupid. It''s some biological thing. It helps us hunt and gather and stuff." -Nazrix
~phil
Advertisement
Quantum, here is your answer...

#1 Please put Option Explicit in your General Declarations section, I noticed you used the variable i in your for loop but never declared it. Trust me, Option Explicit will save you a hell of a lot of time debugging large applications.

#2 Your function has a simple logic flaw. Try calling Pow(5,0), Pow(5,1), Pow(5,2), and Pow(5,3). You'll notice you get 0, 25, 50, and 75 respectively, but you should be getting 1, 5, 25, 125. If you really don't want to use ^, then your calculation should be as follows:

        Public Function Pow(Base As Integer, Exponent As Integer) As Integer  Dim ReturnVal As Integer  Dim Index As Integer    If Exponent >= 0 Then    ReturnVal = 1    For Index = 1 To Exponent      ReturnVal = ReturnVal * Base    Next Index  End If    Pow = ReturnValEnd Function      


* Keep in mind this function returns 0 for any exponent < 0

* Also keep in mind you may want to use a larger return type than integer, a return type of integer will overflow quite easily, try Pow(2,15)


EDITED TO OPTIMIZE THE CODE

Edited by - Xorcist on February 25, 2001 7:39:24 AM
P.S. Always test your functions before using them, especially with their base cases, sometimes it helps to work it out on paper first too (I''m talking from experience here). Just don''t write a function and expect it to work point blank, run it through a battery of tests, it''s the only way to validate it''s functionality.
By the way, VBlimits, your code doesn't handle for negative and zero value exponents. Pow(2,-1) and Pow(2,0) would both return a value of 2, even though they shouldn't. I don't know how you wanted to handle negative exponents, technically they should be fractions, but an exponent of zero should return a value of 1 no matter what. Just thought I'd let ya know in case your using that code somewhere.

Edited by - Xorcist on February 25, 2001 8:11:42 AM
Yeah, I got it to work, thanks everyone And thanks Xorcist, I''m still learning VB, so I don''t know all this stuff
Advertisement
HEHEHE
/Xorcist thanks
but i wont be useing it anywhere L)
The '^' works faster and it works with evrything |) but thanks
anyone in here looking for a Job working on a game in VB with DX8????

if so just e-mail me at javaman@telusplanet.net

-VBLimits
Sorry about the Spelling..


Edited by - VBlimits on February 25, 2001 4:44:42 PM

Edited by - VBlimits on February 25, 2001 4:48:43 PM
-VBLimitsSorry about the Spelling..

This topic is closed to new replies.

Advertisement