Hi all,
I'm looking for the Visual basic Exponent Operator "^" or its replacement without success....any suggestion?
Thanx in advance
Netduinerd
^ Exponent Operator doesn't exists in VB?
Started by Netduinerd, Dec 03 2011 10:51 PM
6 replies to this topic
#1
Posted 03 December 2011 - 10:51 PM
#2
Posted 04 December 2011 - 11:04 PM
Hi Netduinerd,
For standard integer 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
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 FunctionSee 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
Posted 04 December 2011 - 11:27 PM
Make a call to Math.Pow( a, b )
#4
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
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 FunctionBaxter
#6
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
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