Fractional power of units of measures in F#

心已入冬 提交于 2019-12-19 05:50:08

问题


Is it true to say that : there are no fractional power units in F#


回答1:


In addition to what has already been said, the best resource for information about (not just) F# units of measure is Andrew Kennedy's PhD thesis, who actually designed F# units. He mentions fractional units:

The most important decision is whether or not to allow fractional exponents of dimensions. The argument against them is philosophical: a quantity with a dimension such as M1/2 makes no sense physically, and if such a thing arose, it would suggest revision of the set of base dimensions rather than a re-evaluation of integral exponents. The argument in favour is pragmatic: sometimes it is easier to write program code which temporarily creates a value whose dimension has fractional exponents. In this dissertation the former view prevails, and fractional exponents are not considered. However, most of the theory would apply just the same; any potential differences are highlighted as they arise.

I think this is essentially the reason why F# does not have fractional units, because the F# design quite closely follows Andrew Kennedy's work to make sure it is sound.

Update: With F# 4.0, support for fractional exponents has been implemented




回答2:


Units with fractional exponents are quite common and there is nothing special about them. Probably everyone in technology has come across a voltage noise density, which is measured per sqrt(Hz). This makes a lot of sense physically, the noise power is proportional to the bandwidth, and the noise voltage is the sqrt of the power, no strange mathematics here.

To create a new base unit every time one comes across a fractional power exponent is not the right approach.

These units are not SI units and their use breaks library compatibility. If you define the sqrtHz as a new unit and I define the rootHz, our code can't work together. Anyway, I would need to introduce quite a big set of base units to have a complete set Hz^-2, Hz^3, Hz^-5,... Just to offer rational exponents seems to be the better choice, btw. Boost.units does so.




回答3:


Absence of literally fractional power units of measure does not anyhow discounts F# unit facility as it allows presenting seemingly fractional exponent unit relationships the other way around having smallest fraction as a base dimension:

let takeSqrt (x: float<_>) = sqrt(x)

has inferred signature of float<'u ^ 2> -> float<'u> this way avoiding introduction of imaginary "naturally fractional" float<'u> -> float<'u^1/2>.

let moreComplicated (x: float<_>) (y: float<_>) =
    sqrt(x*x + y*y*y)

has inferred signature of float<'u ^ 3> -> float<'u ^ 2> -> float<'u ^ 3>, where all unit measure conversions stay valid relative to some derived implicit base dimension float<'u>.

The fact that the piece of code below

[<Measure>]type m
let c = sqrt(1.0<m>)

does not even compile with diagnostics The unit of measure 'm' does not match the unit of measure ''u ^ 2' can be considered as a blame, or a blessing, but is a clear indication that the unit measure checks are in place.




回答4:


EDIT: After reading OP's comment and the except from Andrew Kennedy's paper, it appears @nicolas is correct -- F# doesn't support units of measure with fractional exponents.




回答5:


Shouldn't the answer be as easy as saying, yes, hertz are measured in s^-2 which is the same as s^(1/2)? There ya' go. Also, I like the philosophical idea of using , say m^(1/2) if it came up in calculations and perhaps one day understanding what that unit means in the literal sense.



来源:https://stackoverflow.com/questions/11333135/fractional-power-of-units-of-measures-in-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!