![](/img/trans.png)
[英]convert Decimal to signed 2's compliment and vice versa in vanilla javascript
[英]How to convert windspeed between Beaufort Scale and M/S and vice versa in Javascript?
我正在尝试创建一个函数,以在 Javascript 中将米每秒 (m/s) 转换为Beaufort 比例。 我可以使用一系列 if 语句来做到这一点,但我更愿意用公式替换它来为我动态计算它。
到目前为止,这就是我的研究使我能够实现的目标:
function beaufort(ms) {
ms = Math.abs(ms);
if (ms <= 0.2) {
return 0;
}
if (ms <= 1.5) {
return 1;
}
if (ms <= 3.3) {
return 2;
}
if (ms <= 5.4) {
return 3;
}
if (ms <= 7.9) {
return 4;
}
if (ms <= 10.7) {
return 5;
}
if (ms <= 13.8) {
return 6;
}
if (ms <= 17.1) {
return 7;
}
if (ms <= 20.7) {
return 8;
}
if (ms <= 24.4) {
return 9;
}
if (ms <= 28.4) {
return 10;
}
if (ms <= 32.6) {
return 11;
}
return 12;
}
我想用一个使用正确公式自动计算的函数替换它。 有谁知道如何在没有多个 if 语句或 switch case 的情况下实现这一目标?
好的,所以在阅读了几篇文章之后,似乎有一个公式可以计算 Beaufort 与 m/s 之间的关系。 我将用我所做的几个功能来回答我自己的帖子。
计算 m/s 到 beaufort:
function msToBeaufort(ms) {
return Math.ceil(Math.cbrt(Math.pow(ms/0.836, 2)));
}
msToBeaufort(24.5);
output: 10
计算波弗特到 m/s:
function beaufortToMs(bf){
return Math.round(0.836 * Math.sqrt(Math.pow(bf, 3)) * 100)/ 100;
}
beaufortToMs(3)
output: 4.34
我知道这是一个罕见的话题,但希望这对某人有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.