使用F#度量单位从m/s转换为km/h [英] Converting from m/s to km/h using F# Units of Measure

查看:145
本文介绍了使用F#度量单位从m/s转换为km/h的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习F#-目前正在研究计量单位.我有一个简单的计算方法,可以每秒返回米,我想引入一个将其转换为每小时公里的函数.

I'm in the process of learning F# - and is currently looking into Units of Measure. I have a simple calculation returning meters per second, and I want to introduce a function converting it to kilometres per hour.

我的代码如下:

[<Measure>] type kg
[<Measure>] type s
[<Measure>] type m
[<Measure>] type km
[<Measure>] type h            

let msToKmph(speed : float<m/s>) =
    (float speed) * 3.6<km/h>

let gravityOnEarth = 9.81<m/s^2>
let heightOfJump = 3.5<m>

let speedOfImpact = sqrt (2.0 * gravityOnEarth * heightOfJump)
let speedOfImpactKmh = msToKmph(speedOfImpact)

这有效-我的速度为8.28673639 m/s和29.832251 km/h.我不确定这是否是表达不同单位之间关系的最佳方法.可以更优雅地做到这一点吗?

This works - I get 8.28673639 m/s and 29.832251 km/h. What I am unsure of is if this is the best way to express the relationship between different units. Can this be done more elegantly?

例如,生产线(浮动速度)从速度参数中删除单位信息,以使msToKmph返回km/h.如果在计算之前没有删除单位信息,则返回的单位将是:km m/(h s)

For instance, the line doing (float speed) to remove the unit information from the speed parameter, to make the msToKmph return km/h. If I did not remove unit information before doing the calculation, the returned unit would be: km m/(h s)

推荐答案

首先,您的msToKmph是完全错误的.尽管它返回正确的返回值,但实际上是通过转换为普通的 measurement float然后 droping 原始<m/s>值,然后将 measurement 值乘以3.6<km/h>.

First, your msToKmph is totally incorrect. Although it returns a correct return value, what it is actually doing, is it just drops the original <m/s> value by converting to a plain, measureless float and then multiplies the measureless value to a 3.6<km/h>.

为了更好地表达UoM之间的关系,请考虑以下问题:

To better express the relations between UoM's, consider this:

let kmToM = 1000.0<m/km>  // relation between kilometers and meters
let hrToSec = 3600.0<s/h> // relation between seconds and hours
let msToKmph(speed : float<m/s>) =
    speed / kmToM * hrToSec

请注意,所有魔数"都封装在UoM转换器中,因此您的公式保持简洁,例如它们只是操作值和常量,而UoM是由编译器计算的.

Note, all "magic numbers" are encapsulated within UoM converters, hence your formulas remain clean, e.g. they simply operate values and constants, but the UoM are calculated by the compiler.

更新:UoM转换的原理是转换公式应该是具有物理意义的东西.经验法则是您的转换价值是否出现在参考书中.用通俗的英语来说,从上方的3.6<km/h>是没有用的,但是1000.0<m/km>只是说:一公里内有1000 m",这是有道理的.

Update: The philosophy of UoM conversion is that the conversion formulas should be something that has physical sense. The rule of thumb is whether your conversion value presents in reference books. In plain English, 3.6<km/h> from above is useless, but 1000.0<m/km> just says, "there is 1000 m in 1 km", which makes sense.

您甚至可以像这样改善hrToSec:

You can even improve hrToSec like this:

let hrToSec2 = 60.0<s/minute> * 60.0<minute/h>

这将使每个值成为参考书中的知名值.

This will make every value a well-known value found in reference books.

这篇关于使用F#度量单位从m/s转换为km/h的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆