Main Site Documentation

Parse to Float


#1

Hi guys,

I have data coming in through a UART port, the data is in comma delimited format, and contains floating point values. The data is commands coming through a radio link, and processing them quickly is of importance.

A typical command may look as such:

$DAVE,ADDWAYPOINT,30.34530,143.04054!

with $ and ! being string delimiting characters.

Right now I need to get the transmitted float co-ordinate values into floats. I’m really buffled on how to do this. I must be blind but I can’t find a function to do so, and doing it in managed code would be terribly inefficient, as I have a constant stream of different commands using float values coming through.

One idea I’ve had is to split the value by the dot, parse into ints, divide the second value by 10^of the values length, and add it onto the first value. That still seems hacky - could somebody advise me as to a built in method for doing this?

Thanks


#2

How about Parse method:

http://msdn.microsoft.com/en-us/library/ee435450.aspx


#3

Ahh. My bad. I kept on looking for float.parse… I didn’t think to check double.parse…

I feel stupid now. :slight_smile:

While I’m here, is it possible to create custom events in netmf and subscribe to them?

What I’m after is a way of letting one thread tell the other thread that it detected a certain condition without polling.


#4

Yes it is possible. Check OnMotion event here:

http://code.tinyclr.com/project/181/parallax-pir-sensor/

But in your case I would use one of the synchronization objects. AutoResetEvent is a good candidate

http://msdn.microsoft.com/en-us/library/ee425129.aspx


#5

Don’t feel stupid. Anyone familiar with regular .NET would absolutely expect there to be a float.Parse() method. I don’t understand it myself. It does exist in the full .NET and is about the only primitive type in NETMF w/o it. I usually resort to

var x = (float)double.Parse(y)

when it must be float.

As far as events, you can absolutely define your own. Here’s the simplest example that I think fits your situation…


using Microsoft.SPOT;

namespace CustomEvents
{
    class Program
    {
        static void Main()
        {
            var receiver = new DataReceiver();
            var consumer = new DataConsumer();
            receiver.DataReceivedEvent += consumer.OnDataReceived;
            receiver.GetData();
        }
    }

    public class DataReceivedEventArgs : EventArgs
    {
        public float MyCustomArgument;
    }

    public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs args);
    public class DataReceiver
    {
        public event DataReceivedEventHandler DataReceivedEvent;

        public void GetData()
        {
            var dataIGot = 1.2345F;
            DataReceivedEvent(this, new DataReceivedEventArgs { MyCustomArgument = dataIGot });
        }
    }

    public class DataConsumer
    {
        public void OnDataReceived(object sender, DataReceivedEventArgs args)
        {
            Debug.Print("Data received: " + args.MyCustomArgument);
        }
    }

}


#6

It is not there, because Double.Parse is enough. Think small. There is no need to waste resources with duplicate code.


#7

Thanks guys,
Where can I do more reading in regards to events? There are a million threads on the topic for the full .net, but it doesn’t seem like a lot of it is applicable.

And in regards to efficiency, what’s better, casting the parsed double to float and doing the math in float, or doing maths in double?

Is there a significant difference in the processing of floats and doubles? How timeconsuming is casting to a float?

And also, will:

wpts  = new double[255][];

discart any data that was previously contained within “wpts”?

If not, how do I do so? I just want to completely empty the array.


#8

Double requires twice the memory of float and from what I remember float is more native to the processor and yields better performance. So, to have to cast from a double every time seems like a real waste unless you really need 15 digits of precision. It would be interesting to see what really causes more waste - adding a small Parse() and eliminating all the casts or doing the casts.

I don’t remember any differences in the way events are handled either way. So, most everything you read should be useful. If you run into any specific examples that don’t translate, please post them and we’ll work through it.

Sorry, I don’t have the numbers off hand. But, as you work with more of other peoples NETMF code you will encounter many instances where code originally coded to doubles was later converted to floats for significant increases in speed. I use floats by default when I need high speed. I use doubles when I need high precision or don’t feel like dealing with the pains of coding to floats.

Yes. In C#, anytime you set a variable = a new value/object then the old value is discarded.


#9

Thanks for all your help Janlee,

I’m getting the hang of event programming now, and I have the following query:

Object nav has the event RequestWpt,
Object com has a handle for that event, with the signature

public void WptRequestHandle( object sender )

Is it now possible for me to run a public function of “nav” using the sender object?

To give you some context what I want to do is, the navigational system raises an event alerting communications port that it has reached its waypoint, communication port now reacts by getting the next waypoint from its array, and returning it to the navigational system.

What I would like to do in the event handler within the com class is something along the lines of:


        public void WptRequestHandle( object sender )
        {
            sender.SetNextWpt(waypoint);
        }

with nav containing the following function:


        public void SetNextWpt( double[] wpt )
        {

        }

So what I’m asking is, is it possible to run the SetNextWpt function like described? And if not, how would you go about doing so? I could just set up another event which returns the NextWpt to nav system, but that seems a bit clumsy.


#10

Sure can. Of course, before asking this type of question I always recommend you just try it first… You’ll learn more. But, try this…


using Microsoft.SPOT;

namespace CustomEvents
{
    class Program
    {
        static void Main()
        {
            var receiver = new DataReceiver();
            var consumer = new DataConsumer();
            receiver.DataReceivedEvent += consumer.OnDataReceived;
            receiver.GetData();
        }
    }

    public class DataReceivedEventArgs : EventArgs
    {
        public float MyCustomArgument;
    }

    public delegate void DataReceivedEventHandler(object sender, DataReceivedEventArgs args);
    public class DataReceiver
    {
        public event DataReceivedEventHandler DataReceivedEvent;

        public void GetData()
        {
            var dataIGot = 1.2345F;
            DataReceivedEvent(this, new DataReceivedEventArgs { MyCustomArgument = dataIGot });
        }

        public void SayHello(string myName)
        {
            Debug.Print("Hello, " + myName);
        }
    }

    public class DataConsumer
    {
        public void OnDataReceived(object sender, DataReceivedEventArgs args)
        {
            Debug.Print("Data received: " + args.MyCustomArgument);
            ((DataReceiver)sender).SayHello("Xarren");
        }
    }

}


#11

I did try it, the cast to (DataReceiver) is what I was missing - because of it it kept on getting underlined in red.

That’s exactly what I need, thank you.


#12

I think in this case it is a necessary trade off. The more space left for user applications the better.


#13

I’ve just realised that floats won’t do the job anyway - a latitude is min/max +/-90.00000, however longitude is +/- 180.00000, which is 8 digits - as far as I understand the limit on float accuracy is 7 digits right?


#14

The decimal precision is 7 digits. It looks like you only need 5. The actual range of a float is -3.4 × 10^38 to +3.4 × 10^38.

http://msdn.microsoft.com/en-us/library/b1e65aza.aspx


#15

Ahh decimal precision, thanks - I thought that meant 7 sig figs, rather than decimals. Back to using floats then :slight_smile:


#16

You might want to play around a little. That range sounded really large to me. Certainly much larger than I’ve ever tried to use. I think it may vary between the PC and NETMF.

If I change my previous sample app to use:


float dataIGot = 180.12345678F;

then it prints back “180.123459” This is probably sufficient for your needs. However, if you use a value of “123456789.123456789” then it prints a value of “123456792” which definitely proves that the above range is not accurate. Or there could just be a problem with the float.ToString() and this is a display problem only. Doubt it but I’m going to play a little more later. Got a lawn mower to go fix now… Do your own tests and see what you find.


#17

Thanks will do.