php - 使用 fromRange 和 toRange 映射值?

发布于 2024-12-09 14:51:33 字数 389 浏览 2 评论 0原文

我试图弄清楚如何将 1 到 1000 之间的数字映射到 1 到 5 之间的数字。

例如:

我有一个包含 1000 条记录的数据库,我想为每条记录分配一个 1 到 5 之间的 id 号。我不希望它是随机的,使用 rand(1,5) 就足够简单了。

在Arduino语言中,它有一个我希望PHP有的功能:

result = map(value, fromLow, fromHigh, toLow, toHigh)

这样做的目的是我不想将映射值存储在数据库中,我需要一个可以调用的php函数,如果说db记录的话是 100 无论函数被调用多少次,它总是返回相同的映射值。

谢谢!

I'm trying to figure out how to map a number between 1 and 1000 to a number between 1 and 5.

For example:

I have a database of 1000 records and I want to assign an id number between 1 and 5 to each record. I don't want it to be random, thats easy enough with rand(1,5).

In the Arduino language it has a function that I'm hoping PHP has:

result = map(value, fromLow, fromHigh, toLow, toHigh)

The purpose of this is I don't want to store that mapped value in the database, I need a php function that I can call and if say the db record is 100 no matter how often the function is called it will always return the same mapped value.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-12-16 14:51:33

您正在寻找的函数通过使用不同的比例来绘制范围。所以这很容易做到:

function map($value, $fromLow, $fromHigh, $toLow, $toHigh) {
    $fromRange = $fromHigh - $fromLow;
    $toRange = $toHigh - $toLow;
    $scaleFactor = $toRange / $fromRange;

    // Re-zero the value within the from range
    $tmpValue = $value - $fromLow;
    // Rescale the value to the to range
    $tmpValue *= $scaleFactor;
    // Re-zero back to the to range
    return $tmpValue + $toLow;
}

基本上,它会在范围内重新确定数字的基数。现在,请注意,如果值在任一范围内,则不会进行错误检查。原因是它映射的是比例,而不是范围。因此,您可以使用它进行基数转换:

$feet = map($inches, 0, 12, 0, 1);

您也可以映射“范围”,因为它重新设置数字的基数(沿数轴移动它):

5 == map(15, 10, 20, 0, 10);

因此对于 from range (0, 1000) 和 to range (0, 5 ),下表将成立:

  • -200 | -1
  • 0 | 0
  • 1 | 0.005
  • 100 | 0.5
  • 200 | 1
  • 400 | 2
  • 600 | 3
  • 800 | 4
  • 1000 | 5
  • 2000 | 10
  • 3000 | 15

为了显示重新定位,如果我们将 (0, 1000) 映射到 (5, 10):

  • -200 | 4
  • 0 | 5
  • 1 | 5.005
  • 100 | 5.005 100 5.5
  • 200 | 6
  • 400 | 7
  • 600 | 8
  • 800 | 9
  • 1000 | 10
  • 2000 | 15
  • 3000 | 20

The function you're looking for maps ranges by using different scales. So that's easy to do:

function map($value, $fromLow, $fromHigh, $toLow, $toHigh) {
    $fromRange = $fromHigh - $fromLow;
    $toRange = $toHigh - $toLow;
    $scaleFactor = $toRange / $fromRange;

    // Re-zero the value within the from range
    $tmpValue = $value - $fromLow;
    // Rescale the value to the to range
    $tmpValue *= $scaleFactor;
    // Re-zero back to the to range
    return $tmpValue + $toLow;
}

So basically, it'll re-base the number within the range. Now, note that there is no error checking if value is within either range. The reason is that it maps scales, not ranges. So you can use it for base conversion:

$feet = map($inches, 0, 12, 0, 1);

And you can map "ranges" as well since it re-bases the number (moves it along the number line):

5 == map(15, 10, 20, 0, 10);

So for from range (0, 1000) and to range (0, 5), the following table will hold true:

  • -200 | -1
  • 0 | 0
  • 1 | 0.005
  • 100 | 0.5
  • 200 | 1
  • 400 | 2
  • 600 | 3
  • 800 | 4
  • 1000 | 5
  • 2000 | 10
  • 3000 | 15

And to show the re-basing, if we map (0, 1000) to (5, 10):

  • -200 | 4
  • 0 | 5
  • 1 | 5.005
  • 100 | 5.5
  • 200 | 6
  • 400 | 7
  • 600 | 8
  • 800 | 9
  • 1000 | 10
  • 2000 | 15
  • 3000 | 20
岁吢 2024-12-16 14:51:33

您是否考虑过:$mappedValue = ($value % 5) + 1;?将返回该值除以 5(即 0-4)后的余数,然后加一。

Have you considered: $mappedValue = ($value % 5) + 1;? Will return the remainder after dividing the value by 5 (i.e. 0-4) then adds one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文