Results 1 to 17 of 17

Thread: how to I do to the power in vb?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    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

  2. #2
    Lively Member
    Join Date
    May 1999
    Posts
    100
    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?"

  3. #3

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    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

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    we posted at the same time
    NXSupport - Your one-stop source for computer help

  6. #6

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    yea, but only by a few miliseconds
    NXSupport - Your one-stop source for computer help

  8. #8
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    Talking 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*

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    lol
    NXSupport - Your one-stop source for computer help

  10. #10
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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

  11. #11
    Guest
    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]

  12. #12
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    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


  13. #13

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    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

  14. #14
    Frenzied Member mlewis's Avatar
    Join Date
    Sep 2000
    Posts
    1,226
    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.
    M. Lewis
    Pi-Q Software
    How many mouse clicks does it take to cook breakfast?

    Blargh! I am dead!

  15. #15
    Guest
    hint:

    Code:
    Result = (X * X * X * X * X * X)
    will execute A LOT faster than:

    Code:
    Result = (X ^ 6)
    try it yourself in a loop of about 10,000,000 iterations.
    see which one finishes faster!

  16. #16
    Conquistador
    Join Date
    Dec 1999
    Location
    Australia
    Posts
    4,527
    that would probably be harder in the long run, though, won't it?

  17. #17

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2000
    Location
    East Providence, RI
    Posts
    1,715
    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
  •  



Click Here to Expand Forum to Full Width