|
-
Sep 14th, 2000, 02:25 PM
#1
Thread Starter
Frenzied Member
how do I do like:
Code:
dim x as integer
label1.caption = x to the second power?
thanks in advance
NXSupport - Your one-stop source for computer help
-
Sep 14th, 2000, 02:30 PM
#2
Lively Member
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?"
-
Sep 14th, 2000, 02:32 PM
#3
Frenzied Member
x^
-
Sep 14th, 2000, 02:32 PM
#4
Thread Starter
Frenzied Member
no but I figured out what I needed:
Code:
Dim x as integer
x = 10
label1.caption = x ^ 5
NXSupport - Your one-stop source for computer help
-
Sep 14th, 2000, 02:33 PM
#5
Thread Starter
Frenzied Member
we posted at the same time
NXSupport - Your one-stop source for computer help
-
Sep 14th, 2000, 02:35 PM
#6
Frenzied Member
nope i beat your ass to it :)
i beat you by few seconds.. haha
-
Sep 14th, 2000, 02:37 PM
#7
Thread Starter
Frenzied Member
yea, but only by a few miliseconds
NXSupport - Your one-stop source for computer help
-
Sep 14th, 2000, 02:39 PM
#8
Frenzied Member
doesnt metta
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*
-
Sep 14th, 2000, 02:43 PM
#9
Thread Starter
Frenzied Member
NXSupport - Your one-stop source for computer help
-
Sep 15th, 2000, 02:34 AM
#10
Conquistador
a different function for power
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
harder than the other one though
-
Sep 15th, 2000, 01:40 PM
#11
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:
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
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.
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]
-
Sep 15th, 2000, 10:17 PM
#12
Conquistador
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
-
Sep 15th, 2000, 10:20 PM
#13
Thread Starter
Frenzied Member
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!!!!!!
NXSupport - Your one-stop source for computer help
-
Sep 16th, 2000, 03:17 AM
#14
Frenzied Member
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.
-
Sep 16th, 2000, 10:08 AM
#15
hint:
Code:
Result = (X * X * X * X * X * X)
will execute A LOT faster than:
try it yourself in a loop of about 10,000,000 iterations.
see which one finishes faster!
-
Sep 16th, 2000, 09:03 PM
#16
Conquistador
that would probably be harder in the long run, though, won't it?
-
Sep 16th, 2000, 09:08 PM
#17
Thread Starter
Frenzied Member
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
NXSupport - Your one-stop source for computer help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|