<p>I'm currently designing a program that will involve some physics (nothing too fancy, a few balls crashing to each other)</p>

<p>What's the most exact datatype I can use to represent position (without a feeling of discrete jumps) in c#?</p>

<p>Also, what's the smallest ammount of time I can get between t and t+1? One tick?</p>

<p>EDIT: Clarifying: What is the smallest unit of time in C#? <code>[TimeSpan].Tick</code>?</p>

[{'answer_id': 28914, 'author': 'OysterD', 'author_id': 2638, 'author_profile': 'https://Stackoverflow.com/users/2638', 'pm_score': 0, 'selected': False, 'text': "<p>I'm not sure I understand your last question, could you please clarify?</p>\n\n<p>Edit:</p>\n\n<p>I might still not understand, but you can use any type you want (for example, doubles) to represent time (if what you actually want is to represent the discretization of time for your physics problem, in which case the tick is irrelevant). For most physics problems, doubles would be sufficient.</p>\n\n<p>The tick is the best precision you can achieve when measuring time with your machine.</p>\n"}, {'answer_id': 28917, 'author': 'tghw', 'author_id': 2363, 'author_profile': 'https://Stackoverflow.com/users/2363', 'pm_score': 4, 'selected': True, 'text': "<p>In .Net a <code>decimal</code> will be the most precise datatype that you could use for position.  I would just write a class for the position:</p>\n\n<pre><code>public class Position\n{\n    decimal x;\n    decimal y;\n    decimal z;\n}\n</code></pre>\n\n<p>As for time, your processor can't give you anything smaller than one tick.</p>\n\n<p>Sounds like an fun project!  Good luck!</p>\n"}, {'answer_id': 28918, 'author': 'Inisheer', 'author_id': 2982, 'author_profile': 'https://Stackoverflow.com/users/2982', 'pm_score': 0, 'selected': False, 'text': "<p>I think you should be able to get away with the Decimal data type with no problem. It has the most precision available. However, the double data type should be just fine.</p>\n\n<p>Yes, a tick is the smallest I'm aware of (using the System.Diagnostics.Stopwatch class).</p>\n"}, {'answer_id': 28931, 'author': 'dmo', 'author_id': 1807, 'author_profile': 'https://Stackoverflow.com/users/1807', 'pm_score': 0, 'selected': False, 'text': "<p>For a simulation you're probably better off using a decimal/double (same type as position) for a dimensionless time, then converting it from/to something meaningful on input/output.  Otherwise you'll be performing a ton of cast operations when you move things around.  You'll get arbitrary precision this way, too, because you can choose the timescale to be as large/small as you want.</p>\n"}, {'answer_id': 28981, 'author': 'Bjorn Reppen', 'author_id': 1324220, 'author_profile': 'https://Stackoverflow.com/users/1324220', 'pm_score': 3, 'selected': False, 'text': '<p>The Decimal data type although precise might not be the optimum choice depending on what you want to do.  Generally Direct3D and GPUs use 32-bit floats, and vectors of 3 (total 96 bits) to represent a position in x,y,z.</p>\n\n<p>This will usually give more than enough precision unless you need to mix both huge scale (planets) and microscopic level (basketballs) in the same "world".</p>\n\n<p>Reasons for not using Decimals could be size (4 x larger), speed (orders of magnitude slower) and no trigonometric functions available (AFAIK).</p>\n\n<p>On Windows, the <a href="http://msdn.microsoft.com/en-us/library/ms644904(VS.85).aspx" rel="nofollow noreferrer">QueryPerformanceCounter</a> API function will give you the highest resolution clock, and  <a href="http://msdn.microsoft.com/en-us/library/ms644905(VS.85).aspx" rel="nofollow noreferrer">QueryPerformanceFrequency</a> the frequency of the counter.  I believe the Stopwatch described in other comments wraps this in a .net class.</p>\n'}, {'answer_id': 29131, 'author': 'Dale Ragan', 'author_id': 1117, 'author_profile': 'https://Stackoverflow.com/users/1117', 'pm_score': 2, 'selected': False, 'text': '<p>I would use a Vector datatype.  Just like in Physics, when you want to model an objects movement, you use vectors.  Use a <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.aspx" rel="nofollow noreferrer">Vector2</a> or <a href="http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector3.aspx" rel="nofollow noreferrer">Vector3 class out of the XNA</a> framework or roll your <a href="http://www.codeproject.com/KB/recipes/VectorType.aspx" rel="nofollow noreferrer">own Vector3 struct</a> to represent the position.  Vector2 is for 2D and Vector3 is 3D.</p>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.timespan.aspx" rel="nofollow noreferrer">TimeSpan</a> struct or the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx" rel="nofollow noreferrer">Stopwatch</a> class will be your best options for calculating change in time.  If I had to recommend, I would use Stopwatch.</p>\n'}, {'answer_id': 85715, 'author': 'Adi', 'author_id': 9090, 'author_profile': 'https://Stackoverflow.com/users/9090', 'pm_score': 0, 'selected': False, 'text': "<p>Hey Juan, I'd recommend that you use the Vector3 class as suggested by several others since it's easy to use and above all - supports all operations you need (like addition, multiplication, matrix multiply etc...) without the need to implement it yourself.\nIf you have any doubt about how to proceed - inherit it and at later stage you can always change the inner implementation, or disconnect from vector3.</p>\n\n<p>Also, don't use anything less accurate than float - all processors these days runs fast enough to be more accurate than integers (unless it's meant for mobile, but even there...)\nUsing less than float you would lose precision very fast and end up with jumpy rotations and translations, especially if you plan to use more than a single matrix/quaternion multiplication.</p>\n"}, {'answer_id': 104173, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 3, 'selected': False, 'text': "<p>Unless you're doing rocket-science, a decimal is WAAAY overkill. And although it might give you more precise positions, it will not necessarily give you more precise (eg) velocities, since it is a fixed-point datatype and therefore is limited to a much smaller range than a float or double.</p>\n\n<p>Use floats, but leave the door open to move up to doubles in case precision turns out to be a problem.</p>\n"}]
['https://Stackoverflow.com/questions/28896', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1782/']