Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Floating point number handling bug

16 views
Skip to first unread message

kazinoz

unread,
Aug 14, 2008, 10:10:07 PM8/14/08
to
I think there is a bug in the way that LabWindows/CVI handles floating point
numbers, both single and double precision. Double first; the few lines shown
below give a
result = 2.936788244924931 E +194 when the
actual decimal conversion is -48.9121589. (See this conversion page <a href="http://babbage.cs.qc.edu/IEEE-754/64bit.html" target="_blank">http://babbage.cs.qc.edu/IEEE-754/64bit.html</a>).
I have a binary stream coming from a Vector Network Analyser which I can output
in Single Precision Binary(32 bit) &nbsp;or Double precision Binary (64 bit)
(IEEE-754). The line below simplifies the problem from a longer array.

void
main (void)
{
char&nbsp;&nbsp;&nbsp; Input[16] = &quot;C04874C1A0000000&quot;;
double&nbsp;
Result;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Scan(Input, &quot;%s&gt;%f&quot;,
&amp;Result);
}
Following an example I tried the line below. This gave a result
= 2.1738120934444362 E -71 when the actual decimal conversion is
-48.9121589.&nbsp;
void main
(void)
{
char&nbsp;Input[16] =
&quot;C04874C1A0000000&quot;;
double&nbsp;Result;
&nbsp;
&nbsp;Scan(Input, &quot;%1f[z]&gt;%f&quot;,
&amp;Result);
}
&nbsp;
I then tried Single Precision which is actually my desired
format. This gives the result of 1.8364521 E -39 when the actual decimal
conversion is -48.9121589. (See the conversion page
<a href="http://babbage.cs.qc.edu/IEEE-754/32bit.html" target="_blank">http://babbage.cs.qc.edu/IEEE-754/32bit.html</a>)
&nbsp;
void main (void)
{
char&nbsp;Input[8] =
&quot;C243A60D&quot;;
float&nbsp;Result;
&nbsp;
&nbsp;Scan(Input, &quot;%s&gt;%f[b4]&quot;,
&amp;Result);
} &nbsp;If&nbsp; the error of my ways can be explained, I would be grateful.&nbsp;

jr_2005

unread,
Aug 15, 2008, 5:40:24 AM8/15/08
to
I think you may be expecting more of the Scan function than it is capable of. It would be happy with this conversion:&nbsp;char Input [16] = &quot;-48.9121589&quot;;
float Result;

Scan (Input, &quot;%s&gt;%f&quot;, &amp;Result);
&nbsp;but your ASCII representation of a binary code corresponding to a floating point number will need more manipulation.&nbsp;JR&nbsp;

ldp

unread,
Aug 15, 2008, 1:10:16 PM8/15/08
to
Hello,&nbsp;a&nbsp;string&nbsp;with&nbsp;hexadecimal&nbsp;digits&nbsp;is&nbsp;not&nbsp;a&nbsp;valid&nbsp;interpretation&nbsp;of&nbsp;a&nbsp;number&nbsp;in&nbsp;ANSI&nbsp;C.&nbsp;It's&nbsp;a&nbsp;mere&nbsp;string.&nbsp;If&nbsp;you&nbsp;have&nbsp;binary data coming in from the vector analyzer, then you can convert each 8-byte group into a double number directly. Example below:unsigned char buffer[]={0x00,0x00,0x00,0xA0,0xC1,0x74,0x48,0xC0}; &nbsp;&nbsp;&nbsp;// Binary representation of double -48.9121589
double *dptr=(double*)buffer; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;Direct&nbsp;conversion&nbsp;to&nbsp;double&nbsp;type&nbsp;&nbsp;The pointer dptr&nbsp;now&nbsp;points to a double value of -48.912158966064453.&nbsp;If your vector analyzer sends the data in text format, you would need to first convert the data to raw&nbsp;binary and&nbsp;then&nbsp;to&nbsp;double.&nbsp;Hope&nbsp;this&nbsp;helps.LDP&nbsp;
0 new messages