0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

お疲れ様です。秋並です。

入力された数値の範囲を変更する関数(arduinoでいうmap関数)をPythonでは以下のように実装できます。

    def range_transform(value, before_min, before_max, after_min, after_max):
        return after_min + (after_max - after_min) * ((value - before_min) / (before_max - before_min))

各引数の意味は以下になります。

  • value:変換する値
  • before_min:範囲変換前の最小値
  • before_max:範囲変換前の最大値
  • after_min:範囲変換後の最小値
  • after_max:範囲変換後の最大値

返り値は以下になります。

  • 範囲変換後の value

使用例:

# 変換前の値
before_value = 0.2

# 変換前の範囲
before_min = 0.1
before_max = 1

# 変換後の範囲
after_min = 1
after_max = 10

after_value = range_transform(before_value, before_min, before_max, after_min, after_max)
print(after_value) # 2
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?