Results 1 to 3 of 3

Thread: FLOAT to DWORD -- Accuracy?

  1. #1

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    FLOAT to DWORD -- Accuracy?

    inline DWORD FtoDW( FLOAT f ) { return *((DWORD*)&f); }

    DWORD dwNumber = FtoDW(1.5);

    What sort of accuracy is kept?
    How does the comp go from DWORD to float like this? I can write the function but I do not understand how any accuracy or anything behind the decimal can be kept. 1.5 converted to a DWORD should be 1 or 2....
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

  2. #2
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: FLOAT to DWORD -- Accuracy?

    I'm not sure if that function will give you any sort of reasonable answer. Floating point numbers are stored in the IEEE Floating Point standard format:

    one bit: sign
    eight bits: exponent
    23 bits: fractional part:

    and to calculate the actual value, you perform 1.fraction raised to the exponent minus 127.

    In other words, the bit pattern in a float is not a value that you can cast to a DWORD. You should instead be doing normal casting
    DWORD dw = (DWORD)(some_float /* + .5 to do conventional rounding */);
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  3. #3

    Thread Starter
    PowerPoster Halsafar's Avatar
    Join Date
    Jun 2004
    Location
    Saskatoon, SK
    Posts
    2,339

    Re: FLOAT to DWORD -- Accuracy?

    You are definetly correct with your explanations.

    However the function I posted works, and it works well enough for declaring scale values in float but the rendering pipeline wants DWORD. I am able to notice differences between 1.0 and 1.2 and 1.5 for example when converting to DWORD using that function for the pipeline
    "From what was there, and was meant to be, but not of that was faded away." - - Steve Damm

    "The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm

    "When you do things right, people won't be sure if you did anything at all." - - God from Futurama

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