Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

^ Exponent Operator doesn't exists in VB?


  • Please log in to reply
6 replies to this topic

#1 Netduinerd

Netduinerd

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationVenice, Italy

Posted 03 December 2011 - 10:51 PM

Hi all, I'm looking for the Visual basic Exponent Operator "^" or its replacement without success....any suggestion? Thanx in advance Netduinerd

#2 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 04 December 2011 - 11:04 PM

Hi Netduinerd,
For standard integer exponentiation,
Public Function pow(ByVal a As Integer, ByVal b As Integer) As Integer
        'the bth power of a, where a is an integer, b a positive integer.
        Dim x, y, z As Integer
        x = a
        y = b
        z = 1
        Do While (y > 0)
            Do While (y Mod 2 = 0)
                y = y \ 2
                x = (x * x)
            Loop
            y = y - 1
            z = z * x
        Loop
        Return z
    End Function
See the following for floating point exponentiation,

http://stackoverflow...-power-function

and for the necessary square root, from Mario Vernari a contributer to the forum,

http://forums.netdui...h__1#entry11325

http://highfieldtale...he-square-root/

For other square root methods,
http://en.wikipedia....ng_square_roots

Baxter

#3 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 04 December 2011 - 11:27 PM

Make a call to Math.Pow( a, b )

#4 Novice Alex

Novice Alex

    Advanced Member

  • Members
  • PipPipPip
  • 59 posts

Posted 04 December 2011 - 11:36 PM

Make a call to Math.Pow( a, b )


Do note that latest version does not work for this function.

#5 baxter

baxter

    Advanced Member

  • Members
  • PipPipPip
  • 415 posts

Posted 05 December 2011 - 03:04 AM

I decided to translate the function that I referenced. I'm not quite sure how it works, but it gives the right answer.
Imports Microsoft.SPOT
Imports Microsoft.SPOT.Hardware
Imports SecretLabs.NETMF.Hardware
Imports SecretLabs.NETMF.Hardware.NetduinoPlus

Module Module1

    Sub Main()
        
        'Windows calculator  = 351.36323577745011426077534810366
        'sqrt1               = 351.36323577745009
        'exponentiation sqrt = 351.36323577745009
        Debug.Print("sqrt1 = " & sqrt1(123456.123456).ToString)
        Debug.Print("exponentiation sqrt = " & exponentiation(123456.123456, 0.5, 0.000000000000001).ToString)

        'Windows calculator = 43345080833350.462136968781988806
        'exponentiation     = 43345080833349.852
        Debug.Print(exponentiation(12345, 3.333, 0.000000000000001).ToString)

        'Windows calculator = 783029.35629994585528611534553745
        'exponentiation =     783029.35629995051
        Debug.Print(exponentiation(576, 2.1351, 0.000000000000001).ToString)

    End Sub
Public Function sqrt1(ByVal num As Double) As Double
        'http://openbookproject.net/thinkCSpy/ch06.html
        Dim approx As Double = num / 2.0
        Dim better As Double = (approx + num / approx) / 2.0
        While better <> approx
            approx = better
            better = (approx + num / approx) / 2.0
        End While
        Return approx
    End Function

    Public Function Abs(x As Double) As Double
        Return If(x <= 0, -x, x)
    End Function

    Public Function exponentiation(ByVal base As Double, ByVal exp As Double, ByVal eps As Double) As Double
        'Recursive function
        'http://stackoverflow.com/questions/3518973/floating-point-exponentiation-without-power-function
        If (exp >= 1) Then
            Dim temp As Double = exponentiation(base, exp / 2, eps)
            Return temp * temp
        Else
            Dim low As Double = 0
            Dim high As Double = 1.0
            Dim sqr As Double = sqrt1(base)
            Dim acc As Double = sqr
            Dim mid As Double = high / 2
            While (Abs(mid - exp) > eps)
                sqr = sqrt1(sqr)
                If (mid <= exp) Then
                    low = mid
                    acc *= sqr
                Else
                    high = mid
                    acc *= (1 / sqr)
                End If
                mid = (low + high) / 2
            End While
            Return acc
        End If
    End Function
Baxter

#6 Netduinerd

Netduinerd

    Member

  • Members
  • PipPip
  • 10 posts
  • LocationVenice, Italy

Posted 05 December 2011 - 07:48 AM

I confirm Math.pow function doesn't exists in the latest .NET MF implementation. Anyway I had wrote my own power function like the first Baxter's post, now I'll understand all of ur suggestions guys. Thanx a lot, Netduinerd

#7 Corey Kosak

Corey Kosak

    Advanced Member

  • Members
  • PipPipPip
  • 276 posts
  • LocationHoboken, NJ

Posted 05 December 2011 - 12:57 PM

I confirm Math.pow function doesn't exists in the latest .NET MF implementation.


Sorry for my bad advice. I should test these things before opening my mouth :(




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.