CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Oct 2006
    Posts
    616

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: float casting problem

    Quote Originally Posted by Zachm View Post
    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

  3. #3
    Join Date
    Oct 2006
    Posts
    616

    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

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: float casting problem

    Quote Originally Posted by Zachm View Post
    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...

  5. #5
    Join Date
    Oct 2006
    Posts
    616

    Re: float casting problem

    Quote Originally Posted by VladimirF View Post
    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

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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...

  7. #7
    Join Date
    Oct 2006
    Posts
    616

    Re: float casting problem

    Quote Originally Posted by VladimirF View Post
    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

  8. #8
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: float casting problem

    Quote Originally Posted by Zachm View Post
    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...

  9. #9
    Join Date
    Oct 2006
    Posts
    616

    Re: float casting problem

    Quote Originally Posted by VladimirF View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured