8 ms·
Tracking units is a thing that I rarely have a use for (as a non-engineer), but when I do, I’m totally mystified by how bad almost every language is at it. F# i
by burke 5y ago
Tracking units is a thing that I rarely have a use for (as a non-engineer), but when I do, I’m totally mystified by how bad almost every language is at it. F# is the only actual programming language I found that even tries. It does a pretty decent job, but even it has some disappointing holes (e.g. not being able to understand g and kg as scaled versions of the same dimension).
I guess the overlap between physicists/engineers and type system/PLT people isn’t large enough.
- g_delgado14 5y agoIsn't the "newtype" pattern something that addresses this issue elegantly? Is there something that I'm missing here?
- burke 5y agoYep, it allows you to tag something a metres per second, but it doesn’t automatically make multiplying it by a duration yield a distance without writing any custom code. F# can do that, and it’s rather cool. There are dimensional analysis libraries for other languages, but none that I’m aware of baked in.
- heisenzombie 5y agoNot really, a units systems needs to be able to combine units in a useful way, like if I typed this: radius = 15 [mm] area = pi*radius^3 force = 100 [N] def will_i_blow_up(pressure [Pa]): if(pressure > 1 [Gpa]): return "Oh no it blew up" will_i_blow_up( force/area ) should raise an error like "UnitsException, [N/mm^3] is not compatible with [Pa]". I could then look over my code and realize I should have written: area = pi*radius^2
- cwhy 5y agoThis is rather a math-specific operation (type conversion on math operators), not the scope of general purpose language. A lot of dependent typed language can achieve this naturally and most languages with ADT can simulate this.
- dragonwriter 5y ago> This is rather a math-specific operation Math is a kind of a core use case for, really, almost any programming language, and is central to much of what computers are used for (as is hinted at in their name.) Though dealing with this aspect of units is less “math-specific” than central to the relation between math and real-world meaning, which is arguably even more important than abstract math in computing applications.
- yladiz 5y agoIf you have a language that supports tagged types and can overload operators, you can basically get there. I think a language like Idris and Julia (with the right packages) should be able to support this.
- jimmyhmiller 5y agoFrink[0] is a language specifically designed for dealing with this kind of stuff. It has tons of units and conversions. [0] - https://frinklang.org/#HowFrinkIsDifferent https://frinklang.org/#HowFrinkIsDifferent
- heisenzombie 5y agoI regularly use "pint" for Python: https://pint.readthedocs.io/en/stable/ https://pint.readthedocs.io/en/stable/ I'm sure it's not as good as a true type system could be, but it works pretty transparently due to Python's duck typing.
- techdragon 5y agoI’ll second the utility of “pint” for anything unit or quantity related in Python. I’m literally in the middle of writing a rate limited continuously run service wrapper around some existing one shot task code and while I initially thought about just parsing the rate definition out myself because it’s pretty simple to just split on “/“ or “per”, but after thinking through the usage scenarios I just ended up using pint to convert anything it can understand as a frequency. So anything of the form x / time unit or x per time unit, and converting it to the appropriate scale for use by the rest of my code. A whole bunch of string matching code replaced by a one line (two if I count the defensive unit type check to prevent false positive parsing problems) that looks as simple as this… default_sleep_interval = (1/ureg(default_rate)).to(“seconds”).magnitude
- sidpatil 5y agoJulia has the Unitful package [1], which does a decent job. It's quite ergonomic, and Julia's design helps with making existing code/libraries play nicely with Unitful-encoded units/values. [1] https://github.com/PainterQubits/Unitful.jl https://github.com/PainterQubits/Unitful.jl
- PascLeRasc 5y agoThis is something that Matlab really shines at with symbolic computation. Is there really not a numpy/scipy equivalent?
- techdragon 5y agoSympy[0] is the major Python package I go to for symbolic calculation, it’s been more than enough for what I’ve needed (except for a few shortcomings I found in higher dimensional symbolic geometry, but I’m not going to fault them for not having my own weird esoteric use cases covered) 0 - https://www.sympy.org/en/index.html https://www.sympy.org/en/index.html
- BalinKing 5y agoMathematica as well.
- publicola1990 5y agoMatlab I thought use Maple for symbolic functionality, is that not the case?
- dagw 5y agoSage/Sympy is the the python library for symbolic computation
- patal 5y agoHow so? Every MATLAB code base I've seen rolls their own unit computation. The symbolic toolbox costs money and I've seen it on university installations only. The unitConvert command is rather new. Neither mechanisms are native to MATLAB and have awkward call patterns.
- rurban 5y agoWhen I implemented unit support into my engine I used the units cmdline tool. Was by far the best and easiest to use. https://www.gnu.org/software/units/manual/html_node/Command-Line-Use.html https://www.gnu.org/software/units/manual/html_node/Command-...
- fsloth 5y agoUnit conversion is not the issue. It's strong typing the values themselves so that you don't actually plug in a value in feet that was thought to be in meters. This means each numeric value must have it's own type (or equivalent strong compiler or runtime backed constraints). So instead of float, we have (eg) float_meters, float_feet and so on.
- jamil7 5y agoSwift has a pretty nice measurements package built in https://developer.apple.com/documentation/foundation/measurement https://developer.apple.com/documentation/foundation/measure...
- klemola 5y agoThe Elm programming language ecosystem has the great "elm-units" library, which prevents one from mixing illogical units, and it produces logical unit combinations: https://package.elm-lang.org/packages/ianmackenzie/elm-units/latest/ https://package.elm-lang.org/packages/ianmackenzie/elm-units... ...which is used in other packages where unit type safety is important, such as elm-geometry: https://package.elm-lang.org/packages/ianmackenzie/elm-geometry/latest/ https://package.elm-lang.org/packages/ianmackenzie/elm-geome...
- papaf 5y agoI guess the overlap between physicists/engineers and type system/PLT people isn’t large enough. C++ has supported units, as a library, since the early 90s. The Barton Nackman book had the initial implementation [1] and it was picked up years later by Scott Meyers [2]. [1] https://www.thriftbooks.com/w/scientific-and-engineering-c-an-introduction-with-advanced-techniques-and-examples_john-j-barton/927108/#edition=4130871&idiq=25469380 https://www.thriftbooks.com/w/scientific-and-engineering-c-a... [2] https://stackoverflow.com/questions/28698558/c-dimensional-analysis-barnes-and-nackman-with-scale https://stackoverflow.com/questions/28698558/c-dimensional-a...
- neilv 5y agoThe Curl programming language for Web applications had units for purposes like its GUI objects, circa 2000. https://en.wikipedia.org/wiki/Curl_(programming_language) https://en.wikipedia.org/wiki/Curl_(programming_language)
- captainmuon 5y agoWhen I was a physics student, I would have loved a spreadsheet that tracks units, and automatically does conversions as needed. There is another huge thing that this enables: Tracking uncertainties. It would be awesome if you could not just write "10", but "10 cm ± 1 cm". Then the spreadsheet could do error propagation! Even the simplest case of non-correlated errors would be useful. Also you could have functions like "weighted mean" that take the errors into account. We used to do these calculations manually with extra columns and a few macros, but it was always error prone. Also, why is there no CAD program that allows me to enter errors? When I try to model real objects, say a room, the lengths never add up 100% and I always have gaps once I'm around the room. It would be great to say all these measurements are ± 1 cm, this one is ± 5 cm, and these angles are 90° ± 1°. And then it would do least-squares, and give me a really accurate model, or tell me where I have to measure again.
- arethuza 5y agoFor a while I toyed with writing an extensible spreadsheet engine/app that would allow extensions to do that kind of thing - maybe I should dust that idea off again! ;-)
- captainmuon 5y agoSeems like many people started on this but never finished it, me too. I got carried away trying to speed up my custom grid control, and borked it up when I tried to use threads :-P.
- arethuza 5y agoI wanted to write something that would be "spreadsheet" like that would work as a normal desktop app, as a component in a server application and on the command line as well. Combine that with all of the other grandiose plans (writing extension in WASM) then it's no wonder that I wasn't making too much progress... interesting though!
- AYBABTME 5y agoI built a Go library that would track units + errors when I was doing my undergrad... but never published it anywhere and lost the files, sadly. Anyways, I agree this would be neat.
- benhurmarcel 5y agoBlockpad ( https://blockpad.net/ https://blockpad.net/ ) and Mathematica do it.
- halfdan 5y agoJulia has beautiful support for adding units to your programs. The nice thing about it is that you can plug unitful numbers into any other package and do math on them. http://painterqubits.github.io/Unitful.jl/stable/ http://painterqubits.github.io/Unitful.jl/stable/
- jcelerier 5y agoI have yet to see unit handling be a problem in C++, making typesafe units is a basic exercise to give to students. Here is a good in-depth comparison of the existing libraries: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p1935r2.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p193...
- discordance 5y agoMathematica (and I guess Alpha now) has always had good support for units and dimensional analysis: https://reference.wolfram.com/language/guide/Units.html https://reference.wolfram.com/language/guide/Units.html
- V1ndaar 5y agoI've recently written a units library for Nim [0]. It's still WIP, but it's already proven extremely useful for me as a physicist. Thanks to Nim's strong type system and metaprogramming features, it allows for a fully compile time design, without any runtime overhead (in form of special unit objects or such things; everything is a `distinct float`). In addition Nim's unicode support, the code even looks nice! A more complex use case (I can link more if desired): [1] [0]: https://github.com/SciNim/Unchained/ https://github.com/SciNim/Unchained/ [1]: https://github.com/SciNim/Unchained/tree/master/examples https://github.com/SciNim/Unchained/tree/master/examples
- the-dude 5y agoI never had one, but didn't some of the later HP calculators do this? ( the 48? )
- sk5t 5y agoYou might find the Squants library for Scala interesting; it even seems like an apt usage of some oft-questioned language features. https://github.com/typelevel/squants https://github.com/typelevel/squants