5 ms·
I’ve always wondered why units(1) isn’t a library. I’d love to have access to abstract “united quantities” and “united measurements” in my HLL of choice, rather
by derefr 6y ago
I’ve always wondered why units(1) isn’t a library. I’d love to have access to abstract “united quantities” and “united measurements” in my HLL of choice, rather than needing to switch to a special-purpose tool (units, Frink) or a statistical-analysis language like R just to get them.
Sometimes I need to bust out stoichiometry-like unit-balancing in the middle of my code, and I want type enforcement that I’m getting the right units in the output! Sometimes I want to do a series of floating-point calculations, and I need to put error-bars on the result, and it’d be great if those were tracked for me in the form of a measurement ADT that tracked sig-figs. Etc.
My only guess for why there’s no libunits, is that it’d be a obtrusive black-box of operational non-guarantees if it was implemented in C, given that what Units does — if naively implemented — would be heavy on arbitrary memory allocations, and basically necessitate garbage collection.
It’d make much more sense for a libunits to exist as a C++-ish “template library” that takes an allocator as an argument; but that doesn’t really help anyone who wants to use any language other than C++ (as the template parameters can’t be exposed in the C wrapper code that other languages FFI to.)
(Before someone asks, you could get a limited kind of united-quantities — ones that can only represent “realistic” units — with a static per-qty bitfield representing the compiled-in units. But this wouldn’t really let you represent arbitrary unexpected “unit polynomial” qtys, e.g. m^5s^-3. This might seem fine, but sometimes intermediate quantities in formulae look like that, even though they work out to something sensible when everything is multiplied out.)
Even if there couldn’t be a single units library implementation, it’d at least make sense for there to be 1. a canonical libunits-data release (sort of like tzdata), that individual library impls could pull in; and 2. a high-level API spec or reference-impl of what a libunits library for a given language-runtime would look like (sort of like how every HLL has copied JodaTime’s datetime design.)
- okl 6y agoYou means something like this? http://www.dmitry-kazakov.de/ada/units.htm http://www.dmitry-kazakov.de/ada/units.htm
- derefr 6y agoVery much so! But I don’t think I can link Ada libraries into my Ruby or Elixir codebase :)
- dragonwriter 6y ago> I don’t think I can link Ada libraries into my Ruby or Elixir codebase Ruby has (among others, I’m certain): https://github.com/olbrich/ruby-units https://github.com/olbrich/ruby-units
- cmrdporcupine 6y agoI've seen a few before, and a quick search yielded a bunch for me, right off the bat. By no means exhaustive: Julia: https://github.com/PainterQubits/Unitful.jl https://github.com/PainterQubits/Unitful.jl Python: https://pint.readthedocs.io/en/0.6/ https://pint.readthedocs.io/en/0.6/ C++: https://github.com/nholthaus/units https://github.com/nholthaus/units etc. And yes, it has always struck me as strange that date/time/calendar utilities are standard as canonical functionality shipped with language distributions, but units are not.
- scott_s 6y agoBoost also has a C++ library: https://www.boost.org/doc/libs/1_65_0/doc/html/boost_units/Examples.html#boost_units.Examples.DimensionExample https://www.boost.org/doc/libs/1_65_0/doc/html/boost_units/E...
- dktoao 6y agoThere are heaps of libraries for all languages because it is a fairly fun and straight forward project. I personally created this one because I was frustrated with how slow and clunky the existing javascript libraries were: https://github.com/GhostWrench/pqm https://github.com/GhostWrench/pqm Also, I open sourced just the unit database (in JSON format) for anyone interested in making their own version: https://github.com/GhostWrench/unitdb https://github.com/GhostWrench/unitdb
- maxnoe 6y ago> straight forward project. Assuming this is straightforward is as wrong as with calendars / time Systems.
- dktoao 6y agoWhat makes you say that? I suppose you could add as much complexity as you want with differentiating units from different standards and adding uncertainty in the unit definitions and on and on. But for 99% percent of users, just conversion factors and basic arithmetic operations are all that is needed. What application do you have in mind where this would fall short? In 15 years as an engineer I have not run into one.
- mmphosis 6y agoWhat day is it? Be conservative in what you send, be liberal in what you accept. leap years, leap seconds, julian dates, pre-gregorian dates, years beyond 9999, yyyy-mm-dd vs d/m/y vs month day year vs ..., zero padding, 12 hour vs 24 hour, 32 bit time stamps, DOS time accurate to every two seconds, why does excel need the date time in a specific format, I dual boot between Windows10 and Linux and every-time I boot up Windows10 the first thing I need to do is adjust the date time, pico-second granularity, time zones, daylight savings time, ... Today is Stability (4), Flint (Tijaax * Etznab) It's a good day to: Spend time in reflection and introspection.
- majewsky 6y ago
- SoSoRoCoCo 6y agoThe source isn't that big, I just downloaded 2.21 source and it is one directory with about ~45 files, and the majority of bytes are for /doc/dvi binaries, only 5 c files and a post Yacc'd tab file for parsing the units definitions file. Surprisingly simple now that I look at it. Heck... I was going ask you to write it, but now I'm thinking this could be a fun weekend project. I can see one function in that library: bool convert(const char *from, const char *to, double input, double *output); No need to make it more complex for starters. If you want to make an .hxx Boost-like class around it, that's outta my league! EDIT: markdown help EDIT: added a status return instead of val return to check for conversion correctness (bool is newer C)
- tom_mellior 6y ago> (...how the heck do I make a literal asterisk in HN markdown?) Spaces on both sides: const char * from But also you are supposed to mark up code by indenting with four spaces, and then everything will be treated as verbatim text: const char *from
- epilys 6y agoThere's also uom for Rust https://github.com/iliekturtles/uom https://github.com/iliekturtles/uom which also provides types for type level operations
- mhh__ 6y agoKeeping track of units was one of the first ever uses of C++ templates (one of the Bjarne books IIRC mentions it existing prior to the STL), but was apparently reinvented multiple times later.