Re: float casting problem
Quote:
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
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
Re: float casting problem
Quote:
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?
Re: float casting problem
Quote:
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
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).
Re: float casting problem
Quote:
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
Re: float casting problem
Quote:
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…)
Re: float casting problem
Quote:
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