gpt4 book ai didi

go - 将数字从一个范围重新映射到另一个范围

转载 作者:数据小太阳 更新时间:2023-10-29 03:45:44 25 4
gpt4 key购买 nike

Arduino map 是否有任何等效项?功能?

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

Description

Re-maps a number from one range to another. That is, a value of fromLow would get mapped to toLow, a value of fromHigh to toHigh, values in-between to values in-between, etc

如果没有,我将如何在 go 中实现它?

最佳答案

Is there any equivalent in go for the Arduino map function?

标准库,或更具体地说 math package ,不提供这样的功能,不。

If not, how would I implement this in go?

通过获取原始代码并将其翻译成 Go。 C 和 Go 在语法上非常相关,因此这个任务非常非常容易。您链接的 map 的手册页为您提供了代码。如前所述,要进行的翻译是微不足道的。

原创自page you linked :

For the mathematically inclined, here's the whole function

long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

你会把它翻译成类似的东西

func Map(x, in_min, in_max, out_min, out_max int64) int64 {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
}

Here是 go playground 上的一个例子。

请注意 map 在 Go 中不是有效的函数名称,因为已经有 map 内置类型使 map 保留关键字。 用于定义 map 类型的关键字,类似于 []T 语法。

关于go - 将数字从一个范围重新映射到另一个范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25839880/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com