nth root function

I'm trying to code an equation that calculates the nth root of an expression. For example, I want to isolate the variable 'r' in the expression r ^ n = x. ( i.e. "r to the power of n = x"). If n=5 for example, the expression could be written as r = 5th root ( x ). Is there a specific function that does this?

[331 byte] By [cdalglish] at [2008-1-10]
# 1

I dont think theres anything specific in the system.math namespace so I did a quick search and found the following formula which when placed in a function may be what you looking for.

Public Function NthRoot(ByVal y As ValueType, ByVal RootIndex As Integer) As Single
Return System.Math.Exp(1 / RootIndex * (System.Math.Log(y)))
End Function

So if I call it

NthRoot(49,2) I get 7 as 7^2 is 49
NthRoot(625,4) I get 5 as 5^4 is 625

So I think this may be close to what you looking for.

spotty at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 2
Thank you! I'll run it and let you know.
cdalglish at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 3
Yes, it does work. Many thanks. I'd have been a long time brushing up on my calculus before I figured that one out.
cdalglish at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 4
Outstanding!
Pickleface at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 5

Hi,

What is 2.5 ^ 6.7 though ? Well it equals

463.65832088157585126331596260465

according to windows calculator.

You can stick DOUBLE number TYPEs into this version.

E.G.

Code Snippet

'Returns 2.5

MessageBox.Show(NthRoot(6.7, 463.65832088157583).ToString)

More about LOGarithms.

Log(2) + Log(3) =Log(6) 'Like 2 * 3 = 6

( just ADD the LOG values ).

Log(x) + Log(y) = Log(z)

Log(2) * 3 = LOG(8)

Log(x) * y = Log(z) ' Where x ^ y = z

EXP in VB.Net is the equivent of ALOG or the Arc-Log ( Inverse Log ) if you like.

Public Class Form1

'This should be one line of code in your code window.>>>>

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Returns 2. as 2 is the 3rd root of 8

MessageBox.Show(NthRoot(3, 8).ToString)

End Sub

Public Function NthRoot(ByVal nthRootToFind As Double, ByVal ofThisNumber As Double) As Double

Return Math.Exp(Math.Log(ofThisNumber) / nthRootToFind)

End Function

End Class

Regards,

S_DS

# 6

Hey Groovers,

Sorry to re-hash this old thread.

I need to be able to find out what x is in the formula below using vb.net. can anyone offer any ideas

2^x = 10 (ie. 2 to the power of x = 10. alternatively, 2 is the xth root of 10).

Any thoughts.

Greg

rustyoldboat at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 7

Hi,

2^x = 10

<=>

log(2^x) = log(10)

<=>

X log(2) = log(10)

->

X= Math.Log(10) / Math.Log(2)

Regards

Csaba

CsabaU at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...
# 8

Perfect. Thank you so much Csaba.

Greg

rustyoldboat at 2007-10-3 > top of Msdn Tech,Visual Basic,Visual Basic Language...