[swift-users] Dimensional analysis

Howard Lovatt howard.lovatt at gmail.com
Tue Nov 29 16:09:14 CST 2016


Great idea to have dimensions library. I use Quantity in Mathematica all the time and it is great. Even if some of the checks are at runtime it is still useful, better to throw an assert rather than nothing. All the rest of your list, in addition to compile time check, I also use.

-- Howard.

> On 29 Nov. 2016, at 9:37 am, Rick Mann via swift-users <swift-users at swift.org> wrote:
> 
> My earlier post about converting Decimal to Double was part of a larger effort by me to write a calculator app that supports dimensional analysis/unit conversions. I've explored some existing libraries on the topic, ranging from this one in Common Lisp (<http://www.cs.utexas.edu/users/novak/units95.html>) to this nice-looking C++ header-only implementation (<https://github.com/nholthaus/units>). Here's one done in Swift (<https://github.com/michalkonturek/MKUnits>), which has similarities to the C++ library.
> 
> My calculator allows expressions like this:
> 
>    1-3/4" x 2 + (4.3 mm + 0.1") -> <XX> mm
> 
> And produces values in both inch and mm, but I want to be able to expand that to more units.
> 
> I also want to support proper dimensional analysis:
> 
>    2 mm x 4" -> 203.2 mm^2
> 
> While my calculator generally only works with lengths, areas, and angles, I figure a general-purpose dimensional analysis library would be useful.
> 
> Dimensional analysis has come up before (<https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160111/006596.html> and <https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160229/011666.html>). It seems support for constant expressions is useful for dimensional analysis, and Swift doesn't currently support that.
> 
> Given that limitation, what approach would the gurus recommend? The library should support:
> 
> • Generating a result with derived units based on the input
> • Simplifying composite units to some canonical form
> • Validating conversion from one (derived) unit to another
> • Supporting leniency in conversions (e.g. conflating lb and lb•f).
> • Compile-time errors when mixing incompatible types.
> 
> In my case, the latter requirement is not so important, since all the expressions will be input by the user.
> 
> The existing Swift library might be sufficient for my purposes, but I was interested in the experts' opinions and suggestions.
> 
> Thanks!
> 
> -- 
> Rick Mann
> rmann at latencyzero.com
> 
> 
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users


More information about the swift-users mailing list