Hi Guys,
Now got stuck with this conversion from string to single.
In the netmf, there is the function CSng, but it does not work with string object.
Also try use Double.TryParse, still go the exception.
Any help?
I know in this forum that is a piece of code in C# but I would like to see something in VB.net and I support netmf 4.2 RC3 should have include something for this.
CSng function
Started by Novice Alex, Oct 24 2011 08:28 AM
3 replies to this topic
#1
Posted 24 October 2011 - 08:28 AM
#2
Posted 24 October 2011 - 10:37 AM
Hi Guys,
Instead I try to do my Str2Sng function.
below is the sample code (not completed yet, due to all the exception caused by simple function call)
From the sample code, I realise there many exception.
Take example, the Math.Pow function. Just a simple "dblTmp = Math.Pow(1, 10)" also cause exception?
What is wrong with the netmf? Did I make any mistake in my code?
Anyone tested these function in VB.NET?
Instead I try to do my Str2Sng function.
below is the sample code (not completed yet, due to all the exception caused by simple function call)
#Region "Str2Single" Public Shared Function Str2Single(sVal As String) As Single Dim dblRes As Double = 0 Dim dblTmp As Double = 0 Dim dblFactor As Double = 0 Dim dblFactor1 As Double = 0 Dim iDecmialPos As Integer = 0 Dim iPos As Integer = 0 Str2Single = 0 Try sVal = sVal.Trim 'Locate the decimal if any iDecmialPos = sVal.IndexOf(".") For iPos = sVal.Length To 0 Step -1 If iDecmialPos < 0 Then dblRes += CDbl(Integer.Parse(sVal.Substring(iPos - 1, 1))) * CDbl(10 ^ (iPos - sVal.Length)) Else If iPos < iDecmialPos Then dblRes += (CDbl(Integer.Parse(sVal.Substring(iPos - 1, 1))) * CDbl(10 ^ (iPos - sVal.Length))) ElseIf iPos > iDecmialPos Then dblTmp = Math.Pow(1, 10) dblTmp = Integer.Parse(sVal.Substring(iPos - 1, 1)) dblFactor1 = iDecmialPos - sVal.Length + 1 dblFactor = Math.Pow(10, dblFactor1) dblRes += dblTmp * dblFactor End If End If Next Return CSng(dblRes) Catch ex As Exception End Try End Function #End Region
From the sample code, I realise there many exception.
Take example, the Math.Pow function. Just a simple "dblTmp = Math.Pow(1, 10)" also cause exception?
What is wrong with the netmf? Did I make any mistake in my code?
Anyone tested these function in VB.NET?
#3
Posted 11 January 2012 - 09:49 PM
Have you tried Sng.Parse("123.456") ?
Also, note that conversion int to double conversion seems to be broken in 4.2 RC3 (http://forums.netdui...ble-to-integer/) so there's a chance Sng.Parse may be broken too.
#4
Posted 12 January 2012 - 12:03 AM
Microsoft.Spot.Math is very limited in what it offers; basically, just sine and cosine of an integer. If you want Pow and Sqrt, you need to write your own. However, Csng will convert your string (using MF 4.2 RC1),
I am using MF 4.2 RC1
Baxter
Imports Microsoft.SPOT Imports Microsoft.SPOT.Hardware Imports SecretLabs.NETMF.Hardware Imports SecretLabs.NETMF.Hardware.NetduinoPlus Imports System Imports System.IO Module Module1 Sub Main() Dim s As Single = CSng("123.456") Debug.Print("s = " & s.ToString) ' ---> s = 123.456001 End Sub End ModuleThere are postings in the forum regarding math libraries. Here are a couple of reasonably efficient functions for Pow and Sqrt,
Public Function Pow(ByVal base As Double, ByVal exp As Double, ByVal eps As Double) As Double 'REF: 'http://stackoverflow.com/questions/3518973/floating-point-exponentiation-without-power-function 'for: Pow(12345, 3.333, 0.000000000000001) 'Windows calculator = 43345080833350.462136968781988806 'Pow = 43345080833349.852 '--- 'For: Pow(576, 2.1351, 0.000000000000001) 'Windows calculator = 783029.35629994585528611534553745 'Pow = 783029.35629995051 ' If (exp >= 1) Then Dim temp As Double = Pow(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 = exMath.Sqrt(sqr) 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 Public Function sqrt1(ByVal num As Double) As Double 'http://openbookproject.net/thinkCSpy/ch06.html 'for sqrt1(123456.123456) 'Windows calculator = 351.36323577745011426077534810366 'sqrt1 = 351.36323577745009 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 Max(ByVal a As Double, ByVal b As Double) As Double Return If(a >= b, a, B) End Function
I am using MF 4.2 RC1
Baxter
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users