Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,

Does anyone know how to solve the following in vb.net 2013 ?
VB
Dim x1 As Double = -0.3
Dim x2 As Double = 0.7
Dim xx As Double = x1 ^ x2 '=-1.#IND
xx = Math.Pow(x1, x2)      '=-1.#IND
xx = -0.3 ^ 0.7            '= -0.43051162024993422
xx = Math.Pow(-0.3, 0.7)   '=-1.#IND

Thanks
Posted

If you look at the definition of Math.Pow[^] it says:
txt
x < 0 but not NegativeInfinity; y is not an integer, NegativeInfinity, or PositiveInfinity.
Returns NaN

So try:
VB
xx = Math.Sign(x1) * Math.Pow(Math.Abs(x1), x2)
 
Share this answer
 
Comments
vdw 19-Jun-15 10:54am    
Thanks, this solved it !
OriginalGriff 19-Jun-15 11:03am    
You're welcome!
There is no bug. The ^ operator is not for performing exponent operations. This operator is an exclusive-or operator. Please see the documentation:
^ Operator[^]
 
Share this answer
 
Comments
F-ES Sitecore 19-Jun-15 10:43am    
That's true for c# but not for VB.net :)

https://msdn.microsoft.com/en-us/library/zh100ckf.aspx
OriginalGriff 19-Jun-15 10:43am    
Reason fro my vote of one: ^ is XOR in C#.
In VB it's "To The Power Of":
https://msdn.microsoft.com/en-us/library/b6ex274z.aspx
Thomas Daniels 19-Jun-15 10:44am    
That's true for C# but not for VB.NET, and the OP's problem is in VB.NET.
https://msdn.microsoft.com/en-us/library/zh100ckf.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900