TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Improving our safety with a physical quantities and units library

67 点作者 limoce超过 1 年前

17 条评论

nicklecompte超过 1 年前
Surprised that I&#x27;m the only person so far mentioning F#, where units of measure have been built in to the language since 1.0: <a href="https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;fsharp&#x2F;language-reference&#x2F;units-of-measure" rel="nofollow noreferrer">https:&#x2F;&#x2F;learn.microsoft.com&#x2F;en-us&#x2F;dotnet&#x2F;fsharp&#x2F;language-ref...</a><p>I wrote a longer comment in reply to nraynaud elsewhere describing how I found it useful for certain things at a past job. It&#x27;s hardly a killer feature, but it&#x27;s very nice to have the compiler (partially) verify the semantic correctness of certain floating-point computations. It&#x27;s not just physics - finance has &quot;units&quot; as well, and the compiler can complain if you gave a function an array of dollars&#x2F;week when it wanted dollars&#x2F;month.<p>If you don&#x27;t mind writing some (admittedly tedious) overhead it can save a lot of headaches.
评论 #38763425 未加载
SloopJon超过 1 年前
Note that this is heavily dependent on C++20. I&#x27;m not even caught up on C++17, so I was thrown off by statements like this:<p><pre><code> quantity q1 = 42 * J; </code></pre> where quantity is a class template:<p><pre><code> template&lt;Reference auto R, RepresentationOf&lt;get_quantity_spec(R).character&gt; Rep = double&gt; class quantity; </code></pre> I didn&#x27;t know you could use auto in a template parameter. I gather that Reference is a concept, something else I&#x27;ve yet to explore. Just when I thought I&#x27;d gotten the hang of &quot;modern&quot; C++.
评论 #38756101 未加载
评论 #38759902 未加载
ahupp超过 1 年前
If you&#x27;re interested in this kind of thing, I wrote a python package called &quot;unit-syntax&quot;(<a href="https:&#x2F;&#x2F;github.com&#x2F;ahupp&#x2F;unit-syntax">https:&#x2F;&#x2F;github.com&#x2F;ahupp&#x2F;unit-syntax</a>) that adds physical unit syntax to python:<p><pre><code> &gt;&gt;&gt; speed = 5 meters&#x2F;second &gt;&gt;&gt; (2 seconds) * speed 10 meter </code></pre> I&#x27;d been using Jupyter notebooks as a calculator for engineering problems and was wishing for the clarity and type safety of real units, but with the succinctness of regular python.<p>It works in both Jupyter notebooks (with an input transform) and stock python (with an import hook). The actual unit checks and conversions are handled by the excellent `pint` package.
评论 #38760835 未加载
bombela超过 1 年前
All the motivating examples but one are about mixing units from different system. And of those, all but one are about errors in mixing US Customary units with the metric system. Maybe Americans should just stop using the US Customary Units as a start?
评论 #38753282 未加载
评论 #38769849 未加载
评论 #38754377 未加载
评论 #38752879 未加载
评论 #38755609 未加载
评论 #38754379 未加载
评论 #38752681 未加载
评论 #38753061 未加载
michaelt超过 1 年前
<p><pre><code> std::vector&lt;quantity&lt;si::milli&lt;si::seconds&gt;&gt;&gt; vec; </code></pre> I&#x27;ve seen efforts like this in the past, but in my experience there&#x27;s a substantial readability cost when you replace a single-line calculation with a multi-line one to accommodate giant type definitions like this.<p>And you just <i>know</i> anyone who&#x27;s adopting this library is also going to have long variable names and a strict line length limit, both of which they will describe as &quot;clean code&quot; :)
评论 #38752564 未加载
评论 #38752794 未加载
chriswarbo超过 1 年前
Scala&#x27;s squants library is a nice implementation of units-of-measure&#x2F;dimensional-analysis <a href="http:&#x2F;&#x2F;www.squants.com" rel="nofollow noreferrer">http:&#x2F;&#x2F;www.squants.com</a><p>In particular it uses types for <i>dimensions</i>; whilst units are just constructors. Hence `Meters(2)` and `Microns(7)` have the same type (`Length`).
评论 #38755568 未加载
majorexception超过 1 年前
I have a project for which I wrote a simple units library. I don&#x27;t think I&#x27;d be able to write any physics-related project now without using it (or a similar library). My Quantity class has a set of 8 parameters (7 SI base units and a hack that allows conversion between Hz and radians) + additional Scale and Offset parameters. Scale allows representing units other than SI (like Nautical Miles), Offset is for units like Celsius, for which 0°C == 273.15 K.<p>I can do things like:<p><pre><code> si::Length length = 15_m + 12_nm; &#x2F;&#x2F; _nm for Nautical Miles si::Area area = 1_m * 1_km; &#x2F;&#x2F; Equals to 1000_m2 si::Power power = 1_m &#x2F; 1_sec &#x2F; 1_sec; &#x2F;&#x2F; Compilation error, 1_m&#x2F;s² is not a si::Power </code></pre> I don&#x27;t have every possible User-Defined Literal, of course, so I end up doing this for less common units:<p><pre><code> using SomeLocalTypeName = decltype(1_rpm &#x2F; 1_V); </code></pre> Something to thing about, when designing such library:<p>* What is 0_degC + 1_degC? 1_degC or… 273.15_K + 274.15_K = 547.3_K = 274.15_degC? I forbid operations between units if any of them has Offset parameter different than 0. I&#x27;m not sure if this is the good solution, though.<p>* Nm (Newton-meters) is the same unit as Joules. ;-)
评论 #38770328 未加载
评论 #38755382 未加载
评论 #38755365 未加载
baq超过 1 年前
This is the one area I most sorely miss in Rust. Not that I don’t miss it anywhere else (I absolutely do, currently writing lots of TS and trying to make do with nominal types), but that it’s a very important safety mechanism in all of engineering and a unit&#x2F;system of measure built into the language would fit so well.
评论 #38753159 未加载
jeffreygoesto超过 1 年前
Mateusz delivers high quality C++ code since a long time. Having implemented a similar library with C++ 03 in 2008 I highly appreciate the amount of attention to detail he invests here. We had numerous bugs in formulas found by that library over the years.
aaauaucuggaa超过 1 年前
If working in python, Pint is an excellent choice: <a href="https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;Pint&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;pypi.org&#x2F;project&#x2F;Pint&#x2F;</a>
owlstuffing超过 1 年前
The manifold[1] project for Java lets you write unit expressions directly.<p>Force force = 5kg * 9.807 m&#x2F;s&#x2F;s;<p>1. <a href="https:&#x2F;&#x2F;github.com&#x2F;manifold-systems&#x2F;manifold&#x2F;tree&#x2F;master&#x2F;manifold-deps-parent&#x2F;manifold-ext#unit-expressions">https:&#x2F;&#x2F;github.com&#x2F;manifold-systems&#x2F;manifold&#x2F;tree&#x2F;master&#x2F;man...</a>
评论 #38755021 未加载
ajxs超过 1 年前
Adacore developed an interesting related system for the GNAT Ada compiler: <a href="https:&#x2F;&#x2F;www.adacore.com&#x2F;gems&#x2F;gem-136-how-tall-is-a-kilogram" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.adacore.com&#x2F;gems&#x2F;gem-136-how-tall-is-a-kilogram</a>
cxx超过 1 年前
Love this! I had to implement a half-assed version of this a long time ago at a job because there was a class of subtle bugs that popped up due to poor naming&#x2F;documentation&#x2F;spaghetti code that would&#x27;ve been eliminated by things like this.
nraynaud超过 1 年前
I am curious about people&#x27;s experience with this kind of system, I have been thinking about it for years, but never actually tried it.<p>I sometimes feel like finding the explicit unit of some sub-expressions in geometry might be complicated. I know &quot;auto&quot; avoids that particular problem, but I don&#x27;t have a good policy for when to put &quot;auto&quot; to avoid expressing a complex type and when to explicit the type to block the propagation of errors.<p>Also during debugging we might need to display some customary unit for sub expressions&#x2F;watchpoints because the SI unit means nothing to humans in some fields (pressure comes to mind, some people use mm of water, mm of mercury, bars, etc.)
评论 #38755192 未加载
tesdinger超过 1 年前
&gt; Although it makes physical sense to add heights of daily climbs, there is no sense in adding altitudes.<p>What if I want to compute the average altitude? This requires adding altitudes.
vitaminCPP超过 1 年前
Does anybody know about a unit library in C?
noobermin超过 1 年前
While this is a nice thing to have I dread the day that codes start to require people get with this. Yes, it&#x27;s a source of bugs, sometimes people need to accept that bugs naturally arise because of something called the undecideability and we need to stop shoehorning things into languages to make things safe. It makes sense in a host of features but the problem with these sorts of features is they make a lot of a assumptions, including the fact that codes with units will actually be better with units.
评论 #38754673 未加载
评论 #38754545 未加载