Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You are missing dimensional analysis. For example (from the answer you linked to), in F# you can do this:</p> <pre><code>let g = 9.8&lt;m/s^2&gt; </code></pre> <p>and it will generate a new unit of acceleration, derived from meters and seconds (you can actually do the same thing in C++ using templates).</p> <p>In C#, it is possible to do dimensional analysis at runtime, but it adds overhead and doesn't give you the benefit of compile-time checking. As far as I know there's no way to do full compile-time units in C#.</p> <p>Whether it's worth doing depends on the application of course, but for many scientific applications, it's definitely a good idea. I don't know of any existing libraries for .NET, but they probably exist.</p> <p>If you are interested in how to do it at runtime, the idea is that each value has a scalar value and integers representing the power of each basic unit.</p> <pre><code>class Unit { double scalar; int kg; int m; int s; // ... for each basic unit public Unit(double scalar, int kg, int m, int s) { this.scalar = scalar; this.kg = kg; this.m = m; this.s = s; ... } // For addition/subtraction, exponents must match public static Unit operator +(Unit first, Unit second) { if (UnitsAreCompatible(first, second)) { return new Unit( first.scalar + second.scalar, first.kg, first.m, first.s, ... ); } else { throw new Exception("Units must match for addition"); } } // For multiplication/division, add/subtract the exponents public static Unit operator *(Unit first, Unit second) { return new Unit( first.scalar * second.scalar, first.kg + second.kg, first.m + second.m, first.s + second.s, ... ); } public static bool UnitsAreCompatible(Unit first, Unit second) { return first.kg == second.kg &amp;&amp; first.m == second.m &amp;&amp; first.s == second.s ...; } } </code></pre> <p>If you don't allow the user to change the value of the units (a good idea anyways), you could add subclasses for common units:</p> <pre><code>class Speed : Unit { public Speed(double x) : base(x, 0, 1, -1, ...); // m/s =&gt; m^1 * s^-1 { } } class Acceleration : Unit { public Acceleration(double x) : base(x, 0, 1, -2, ...); // m/s^2 =&gt; m^1 * s^-2 { } } </code></pre> <p>You could also define more specific operators on the derived types to avoid checking for compatible units on common types.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload