-
October 13th, 2009, 08:00 AM
#1
float casting problem
I recently encountered a weird problem that has to do with the VARIANT struct.
An external application has updated the VARIANT with a float value that conforms to the IEEE 754 standard (using Memory Watch, I verified that the bits are aligned correctly, representing a specific floating point value).
However when accessing the VARIANT using the fltVal member, the resulting float number is entirely different than expected.
The following code seems to have fixed the problem:
Code:
// 'v' is of VARIANT type
unsigned int uiVal = (unsigned int)v.fltVal;
float fVal = *((float*)&uiVal);
However, I'm not sure as to what has caused this problem and why the code above fixes it. I suspect this has something to do with the union type properties, but I'm not sure.
Any input is appreciated.
Regards,
Zachm
-
October 13th, 2009, 08:49 AM
#2
Re: float casting problem
Originally Posted by Zachm
I recently encountered a weird problem that has to do with the VARIANT struct.
An external application has updated the VARIANT with a float value that conforms to the IEEE 754 standard
What if you used double instead of float? Most compilers let the double type follow the IEEE standard, and not the float type.
Regards,
Paul McKenzie
-
October 13th, 2009, 08:53 AM
#3
Re: float casting problem
I tried casting it to double, getting the same wrong results.
I'm using the standard MS VS2005 compiler.
Regards,
Zachm
-
October 13th, 2009, 11:49 AM
#4
Re: float casting problem
Originally Posted by Zachm
I tried casting it to double, getting the same wrong results.
You shouldn't cast VARIANT, just use the correct member of that union. What is its VARTYPE vt?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
October 13th, 2009, 12:43 PM
#5
Re: float casting problem
Originally Posted by VladimirF
You shouldn't cast VARIANT, just use the correct member of that union. What is its VARTYPE vt?
It's VT_R4 (index 4 in the union) - 4-byte real.
Regards,
Zachm
-
October 13th, 2009, 01:09 PM
#6
Re: float casting problem
I would like to see a REAL number (a little pun intended).
What was passed in, what do you observe in a Watch window, and what you are getting as a result.
BTW, in your original post, the code makes no sense. You first type-convert your float to the int (it’s OK if you can tolerate truncation), then type-cast pointer-to-int to pointer-to-float (VERY rarely a good idea).
Last edited by VladimirF; October 13th, 2009 at 01:26 PM.
Reason: Restored the original post for consistency of this thread.
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
October 13th, 2009, 01:12 PM
#7
Re: float casting problem
Originally Posted by VladimirF
I would like to see a REAL number (a little pun intended).
What was passed in, what do you observe in a Watch window, and what you are getting as a result.
BTW, in your original post, the code makes no sense. You first type-convert your float to the int (it’s OK if you can tolerate truncation), then type-cast pointer-to-int to pointer-to-float (VERY rarely a good idea).
I am well aware that the code I posted doesn't make any sense - but nevertheless it was what it took to fix the float value.
I have a hunch that somehow due to some weird compiler runtime issue, the bits lying down in the memory address of that float are interpreted as an int (or unsigned int). Therefore, casting or assigning it to float just sets the float to the unsigned int value (as in regular int to float casting), yet when type-casting pointers - the float value is interpreted directly from the bits in that memory address. This is all just speculations. I don't have anything to back it up at the moment. I will post the exact data tomorrow - since I'm not near my workstation right now.
Regards,
Zachm
-
October 13th, 2009, 01:31 PM
#8
Re: float casting problem
Originally Posted by Zachm
I am well aware that the code I posted doesn't make any sense - but nevertheless it was what it took to fix the float value.
Hi Zachm,
After re-reading your first post, I figured that you must have known what you were doing. I even edited my post to remove the somewhat “mentoring” part (restored it now since you’ve already replied to it).
For your code to work, there must be some weird data massaging going on in that “external application”. Could they have bit-copied their float to int, saved it as int, but flagged it as float? (It hurts a little even to read my last sentence…)
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
October 13th, 2009, 02:44 PM
#9
Re: float casting problem
Originally Posted by VladimirF
For your code to work, there must be some weird data massaging going on in that “external application”. Could they have bit-copied their float to int, saved it as int, but flagged it as float?
Perhaps it is possible that this was the case, but I don't have access to this external application's code - this is a 3rd party application.
Oh, and don't worry about the mentoring bit. You can mentor me any day of the week - as long as your'e trying to be helpful, I wouldn't mind .
Thanks,
Zachm
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|