how do I do like:
Code:dim x as integer
label1.caption = x to the second power?
thanks in advance
Printable View
how do I do like:
Code:dim x as integer
label1.caption = x to the second power?
thanks in advance
what? explain better..
Do you wan't to write the value of x?
if so do
Code:Dim x as Integer
x = 1
Label1.caption = x & " to the second power?"
x^2
no but I figured out what I needed:
Code:
Dim x as integer
x = 10
label1.caption = x ^ 5
we posted at the same time
i beat you by few seconds.. haha :)
yea, but only by a few miliseconds
if its even a nano second..
i still beat ya
the point is i won, i won i tell ya
jk, as long as ya got what you want
doesnt matter who won
*ya right, i won damn it*
lol
a different function for power
harder than the other one though :(Code:Private Sub Form_Load()
MsgBox ToThePower(10, 3)
End Sub
Function ToThePower(Number As Integer, Power As Integer) As Integer
Dim Counter As Integer, Result As Integer
Counter = 0
Result = Number
Do
Counter = Counter + 1
If Counter = Power Then Exit Do
Result = Result * Number
Loop
ToThePower = Result
End Function
OK. Let me first say that I'm a bit-whipper. I "hate" big functions for doing something simple. Why not try 10^3 instead of that loooong function?
Sorry if you consider this rude...
Second of all I think your code could be "optimized" if you had to do it yourself in code:
I Dimensioned Power as byte because you'd get a overflow if you raise anything bigger as 1 to the power of 255. So I save a byte there.Code:Function ToThePower(Number As Integer, Power As Byte) As Long
Dim Counter As Byte, Result As Long
Result = Number
For Counter = 1 To Power - 1
Result = Result * Number
Next
ToThePower = Result
End Function
Second, I return a Long because raising a number to the power of X could return very large values, and Integer might be the wrong type then ;) Lost a few bytes though...
Then I used a For-loop so I don't have to use those "scary" Exit... functions. Those remind me too much of a bunch of goto's (which is a big no-no) and saves an if-statement. It also saved on Counter=Counter+1.
After all it also saved a bit of type-work... So......
I hope I helped...
[Edited by RobIII on 09-15-2000 at 02:56 PM]
yeah, the real way is better but i was just showing another way in which it could be done, didn't really think about the "for" loop
thanks ALL!!!!!!!!!
and I think that:
8 ^ 9 (eight to the 9th power) works best for me
its nice and short and not like 10 lines, but again, thanks to everyone who posted!!!!!!
Hey, make life easy on yourselves.
Function XtotheY (ByVal X As Single, Y As Byte) As Single
XtotheY = X^Y
End Function
this lets you do decimal numbers as well.
But for the best results, inline everything. Instead of:
MsgBox "X to the Y power is " & XtotheY(X,Y)
just do
MsgBox "X to the Y power is " & X^Y
No evil type coercion, nothing. Makes things so much nicer.
hint:
will execute A LOT faster than:Code:Result = (X * X * X * X * X * X)
try it yourself in a loop of about 10,000,000 iterations.Code:Result = (X ^ 6)
see which one finishes faster!
that would probably be harder in the long run, though, won't it?
um.... guys, read the forth post, in which I found the answer which I think is the fastest, and after that pretty much, everyone keeps saying the same thing