Single or Double for vector floats?

as i'm new to this. i was wondering if your only allowed single floats for vectors etc in directx. most of the code i've seen only has single float values. as i wanted to use the following unit scale 1.000 = 1 meter, 0.100 = 10cm, 0.001 = 1mm. with a max value of 99999.999 i think i'll need double floats for using this. so would i get away with this


Answer this question

Single or Double for vector floats?

  • RRamesh1234

    ahh thanks for that info. i was doing some testing in visual c# express 2005 to see how high a figure a float would take. reason about me asking about double usage was that when i entered a figure of 99999.999 it would print 10000 unless i made it a double. i don't need that high a figure just something close to 60000+.

    least i now know why that 10000 figure was turning up. :)

  • Ovidiu Platon [MSFT]

    Added to the Managed DirectX floating point FAQ

    http://www.thezbuffer.com/articles/400.aspx



  • Massy

    DirectX only supports floats.

    However, more importantly, the hardware only supports floats. This is because doubling the number of transistors (or quadrupling, for N-squared hardware) is not worth it, given that a float is quite capable of storing all the precision you'll need for anything the human eye can detect.

    If you want to support scenes with large coordinates, I would recommend storing the scene data (individual object positions, etc) as double precision, and then downcasting to single precision only when you turn the scene data into matrices (for transforms).

    Similarly, trying to store mesh data with double precision makes very little sense, because the dynamic range of a floating point number is still way bigger than what you can reasonably represent on a computer screen. If you need double precision data for, say, physical simulation, then you must down-convert to float when actually displaying.



  • Cliff hewett

    The problem with floating point isn't how high a number it will take (becuase its a very big number). However you have to understand that there are only so many digits of accuracy and as the numbers get bigger they get less and less accurate.

    e.g. in decimal if I use 3 decimal places for my mantissa I can accuratly represent any number from 0.000 to 0.999 (0.000 E 00, 0.999 E 00) but once the exponent starts to increase so does my accuracy. So while an exponent of 1 looks increases my range up to 9.99 (0.999 E 01) you will notice that the numbers I can accuratly represent are 9.97, 9.98, 9.99 - I can't represent 9.975 or 9.981. When exponent gets past 3 I can't represent any fractional values at all (0.999 E 03 = 999).

    Floating point is much the same only things under the covers are all binary so its harder to guess what the accuracy is - without that tool.

    For a bunch of interesting posts on floating point see Eric Lipperts series

    http://blogs.msdn.com/ericlippert/archive/2005/01/10/350108.aspx

    http://blogs.msdn.com/ericlippert/archive/2005/01/13/352284.aspx

    http://blogs.msdn.com/ericlippert/archive/2005/01/17/354658.aspx

    http://blogs.msdn.com/ericlippert/archive/2005/01/18/355351.aspx

    http://blogs.msdn.com/ericlippert/archive/2005/01/20/357407.aspx

    http://blogs.msdn.com/ericlippert/archive/2005/01/26/361041.aspx



  • noon1

    If you need a large range and continuous precision its worth considering storing the positions of your objects as Int32 or Int64. You would need to find a local point of reference (for each frame, or whatever time step) and convert your positions to floats relative to that point before using them in d3d transforms.

    Check this thread out for more information:

    http://sourceforge.net/mailarchive/forum.php thread_id=10283292&forum_id=6188

    For those who think they might not be able to get enough accuracy, consider this:

    (2^64) nanometers = 18,446,744.1 kilometers


  • Chris Bertenshaw

    Since all of the overloads and functions take singles you only have one choice to pass into there, but you can do all your intermediate cacluations in doubles and only round at the last minute.

    If you use the calculation engine here http://babbage.cs.qc.edu/courses/cs341/IEEE-754.html you can see that 99999.999 is right on the edge of accuracy for a single. Though barely by 1 decimal place.

    e.g 99999.984 then 99999.992 then 10000.000 and the only numbers that can be exactly stored.

    Of course depending on what calculations you do to get to those numbers you may lose accuracy with each calculation and end up a lot further away which is why you can consider using doubles.

    So its your call. If you want to use doubles don't forget to create your device correctly. Unless you create your device with the preserveFPU flag all of your doubles will really be single under the covers.



  • Single or Double for vector floats?