Advertisement

Power in VB

Started by February 24, 2001 10:15 PM
15 comments, last by Quantum 23 years, 11 months ago
Does VB have any way to do powers? pow() doesn''t work, stupidly enough.
N/M, rolled my own
Advertisement
Last time I checked, the BASIC language exponent operator was ''^'', as in

10 squared is written as 10^2

I would assume the operator still stands for Visual Basic.
I thought it was, but then I used it in replace of my function, and it started giving me really really strange results. MSDN says it is too, but if I use it in an expression it gives me weird results.
hmm i never thought of this!!!! all those times ive used vb and ive never run into this prob... oh well. Powers arent a prob. just create your own function. I'm not very good at writing VB code without the compiler fixing all my mistakes but here goes nothing!


Function pow( dblNumber As Double, intPower As Integer)as Double

Repeat:

If intPower <= 1 Then Exit Function

dblNumber = dblNumber * 2
intPower = intPower - 1

pow = dblNumber

Goto Repeat

End Function


that should do you for any positive powers. Im really sleepy right now so if you need negative powers... just optimize the code. I suggest you optimize the code anyways because its very cpu hungry.


-----------------------------
I'm almost finished my 3D Engine for the TI-83 calculator

Edited by - ninja770 on February 25, 2001 1:54:49 AM
-----------------------------;)I'm almost finished my 3D Engine for the TI-83 calculator ;)
ok im and idiot. I jsut checked "^" and it works fine. if you are using expressions maybe you should enclose them in parenthesis "10 ^ (index*index2+blah*blah2)" you get the idea? hmm but giving you wierd results??? maybe yu can post code... im good at debugging Visual Basic.. well ok maybe not good but good enough.

-----------------------------
I''m almost finished my 3D Engine for the TI-83 calculator
-----------------------------;)I'm almost finished my 3D Engine for the TI-83 calculator ;)
Advertisement
This is what I wrote:
Public Function Pow(Base As Integer, Exponent As Integer) As Integer    Dim ReturnVal As Integer        For i = 1 To Exponent        ReturnVal = ReturnVal + Base * Base    Next i        Pow = ReturnValEnd Function 

And I used it like so:
Value = Pow(Design.ListIndex, 2) * Pow(Colour.ListIndex, 4) * Pow(Shape.ListIndex, 64)
If I replace the Pow() with just .ListIndex ^ 2, it doesn''t work. And.. if I replace the main loop inside Pow() with just ReturnVal = Base ^ Exponent, it doesn''t work!
Strange..
Vary strange use use the ''^'' all the time and it alwas works for me
try this
x=10^2
y=pow(10,2)
if x<>y then msgbox "Dame MicroCrap"

hehe i dont see why it would not work




-VBLimits
Sorry about the Spelling..
www.VBLimits.com
-VBLimitsSorry about the Spelling..
Well i just tryed that
and then got my TI-84 Plus and well 10^2 by my Culc = 100
by VB x=10^2=100
but y=pow(10,2)=200
your Function is not right

hehe



-VBLimits
Sorry about the Spelling..
www.VBLimits.com
-VBLimitsSorry about the Spelling..
Here dude
try this

Public Function Pow(Base As Integer, Exponent As Integer) As Long
Dim ReturnVal As Long
Dim i As Long
ReturnVal = Base
For i = 1 To Exponent - 1
ReturnVal = ReturnVal * Base
Next i
Pow = ReturnVal
End Function

this one works

-VBLimits
Sorry about the Spelling..
www.VBLimits.com
-VBLimitsSorry about the Spelling..

This topic is closed to new replies.

Advertisement