温馨提示:本文翻译自stackoverflow.com,查看原文请点击:formula - How to convert windspeed between Beaufort Scale and M/S and vice versa in Javascript?
javascript formula

formula - 如何在Javascript中的Beaufort Scale和M / S之间转换风速,反之亦然?

发布于 2020-03-29 21:38:08

我正在尝试创建一个函数,以将米每秒(m / s)转换为Javascript中的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可以实现吗?

查看更多

提问者
Mr L
被浏览
21
Mr L 2020-01-31 18:21

好的,因此,在阅读几篇文章之后,似乎有一个公式可以计算出往返于m / s的Beaufort。我将用我已经完成的一些功能回答我自己的帖子。

计算m / s到beaufort:

function msToBeaufort(ms) {
    return Math.ceil(Math.cbrt(Math.pow(ms/0.836, 2)));
}

msToBeaufort(24.5);
output: 10

计算beaufort至m / s:

function beaufortToMs(bf){
    return Math.round(0.836 * Math.sqrt(Math.pow(bf, 3)) * 100)/ 100;
}

beaufortToMs(3)
output: 4.34

我知道这是一个难得的话题,但希望这对某人有帮助。