douxia2137 2014-09-15 02:02
浏览 745
已采纳

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

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

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

If not, how would I implement this in go?

  • 写回答

1条回答 默认 最新

  • dongshuo6503 2014-09-15 02:25
    关注

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

    The standard library, or more specifically the math package, does not offer such a function, no.

    If not, how would I implement this in go?

    By taking the original code and translating it to Go. C and Go are very related syntactically and therefore this task is very, very easy. The manual page for map that you linked gives you the code. A translation to go is, as already mentioned, trivial.

    Original from the 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;
    }
    

    You would translate that to something like

    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 is an example on the go playground.

    Note that map is not a valid function name in Go since there is already the map built-in type which makes map a reserved keyword. keyword for defining map types, similar to the []T syntax.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题