LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Single Precision Float (SGL) not working as per IEEE754

Solved!
Go to solution

I can correctly convert to the HEX value of the SGL input.

However it doesn't always work when converting it back to a SGL.

 

I tried with 12.961126 and it works fine on both sides.

But 0.2556 doesn't work on the way back. It gives 0.

 

I confirmed that the convert to bytes works according to http://www.h-schmidt.net/FloatConverter/IEEE754.html

 

If I change the least significant byte from 01 to 10, it does give a return of 0.2556000. However, I prefer not to manipulate data because this is for transport.

 

A workaround would be to use a C application for the conversion...

 

Image below is the code. It is also attached as LabVIEW 2011.

The Typecast is SGL constant (not a double). The input is a U32, but that doesn't matter. Makes no difference as U8. I will never have more than U8 data per element for this project.

 

Thanks

 

SGL Issues.png

0 Kudos
Message 1 of 7
(4,859 Views)

Try this. YOu need to change the width to 2.

 

Chhers,

mcduff

 

 

0 Kudos
Message 2 of 7
(4,838 Views)
Solution
Accepted by Foreshadow20

I should have attached a snippet, sorry.

 

Cheers,

mcduff

 

snip.png

Message 3 of 7
(4,816 Views)
Actually you don't need the loop. If you want to simply concatenate all the elements in a string array, all you need a one string concatenator. Pass in an array of strings and you will get a string out with all the elements strung together.

But more to the point, why are you going to all the trouble with a loop and three different reformats. Just do the reverse of the original conversion.

Actually you can make it even easier.

1. Cast the SGL directly as an array of U8s.
2. To convert back, cast the array as an SGL.
3. Stick a fork in it, and call it done...

Flatten to String will also do the conversion, and Unflatten from String will change it back.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 4 of 7
(4,790 Views)

Your snippet works just fine for me.

 

PS:  You don't need the SGL bullet in the upper picture.  Just wire a constant to the type input of the conversion and set its representation to single float.  Also the %s on the upper typecast is unnecessary as typecast by default sets things to strings.

0 Kudos
Message 5 of 7
(4,769 Views)

I agree with the others that you need to clean up your code, because it is just way too convoluted. Your "bytes Input" should probably be U8.

To demostrate the concept, you don't even use formatted strings, because (especially with SGL, you might lose significant information detouring over a decimally formatted intermediary representation of the binary values.)

 

Your use of a string containing %s for the upper typecast input is just silly. Since only the type is important and the value is irrelevant, you should probably wire an empty string (or leave it unwired, since string is the default). Typing a formatting statement in there does not change things, but might really confuse somebody looking at the code.

 

You can also avoide your "toSGL" if you wire a SGL constant to the default input.

 

 

 

All you probably need is a hexadecimally formatted string contro/indicator a SBL contr/indicator and a typecase operation. eliminate the middleman!

 

 

(Of course you can easiy go from SGL to string and back. Same difference)

Download All
Message 6 of 7
(4,763 Views)

Thanks for the help. It works with the first solution (also shown as solution for future visitors). I also applied the other solutions in order to optimize the code. Lots of valuable information in this thread.

 

Added note:

The code shown is a snippet of 2 different VIs that I merged into 1 vi for ease of forum discussion.

The strings are data from serial communication of the RAM content of a controller. It has various formats and sizes from U8 to float. I had issues with the float. The rest is largely done with bitwise operations.

 

Thanks again.

0 Kudos
Message 7 of 7
(4,676 Views)