From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

scan from string %d double float - WHAT???

Solved!
Go to solution

I realise I'm asking for trouble using "scan from string" with %d as the format string and giving it a double as the default value type, but I certainly did not expect this result.  3013981218 does not equal -1280986078.

 

Looks like a bug to me (LV2011SP1-LV2015). Can anyone explain this?

U32 d DBL.pngU32 d DBL FP.png

 

When I say that the value that comes out of the typecast is expected, I mean I can expect something strange.

What I did not expect was that scanning an integer string to a double using %d would give me the same result as the weird typecast.

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 1 of 7
(4,225 Views)

Interestingly, it's got something to do with the 32-bit numbers. 2147483648 (2^31) is the first number where you don't get the expected result, but 2147483647 (2^31 - 1) works. I'm not sure if this is OS dependent or that scan from string converts it to a 32-bit number and then converts (or type casts?) to a DBL internally.


LabVIEW Champion, CLA, CLED, CTD
(blog)
0 Kudos
Message 2 of 7
(4,191 Views)

I am pretty sure the %d is parsed to an U32 internally and then casted into whatever value from there.  This was discussed somethere, I think in the Idea Exchange.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 3 of 7
(4,171 Views)

If %d is parsed internally to a U32 (as you would expect) then I should get the correct value out.

What comes out appears to be that U32 value type cast to an I32.

 

The actual byte data is the same obviously, it just seems to be assuming it is an I32 not a U32. It takes the most significant bit and assumes its a sign bit, not part of the value.

 

This behaviour is unexpected.

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 4 of 7
(4,124 Views)

Since a %d can be a negative number, I'm pretty sure it's converted to an I32 internally and then to a double. I agree that it is not expected behaviour though - perhaps raise a ticket with NI and get a CAR number?

 

Question - why don't you scan to a float (%f) and then round? That would give the correct results?


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 5 of 7
(4,085 Views)
Solution
Accepted by topic author TroyK

Ok, it is I32.  Here is the post I was thinking about: http://forums.ni.com/t5/LabVIEW-Idea-Exchange/Upgrade-format-string-s-quot-d-quot-behavior-to-serve-...


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 6 of 7
(4,063 Views)

Sam_Sharp wrote: why don't you scan to a float (%f) and then round? That would give the correct results?

Yes, that's what I've changed it to. It was an old bit of code that that hadn't looked at in years until I realized it misbehaved with large integers.

Troy - CLD "If a hammer is the only tool you have, everything starts to look like a nail." ~ Maslow/Kaplan - Law of the instrument
0 Kudos
Message 7 of 7
(4,022 Views)