Skip to content

New Feature: Function to scale values with different ranges #102

Closed
@microbit-carlos

Description

@microbit-carlos

A function like this is quite useful to be able to map inputs an outputs with different value ranges.
For example the output from one sensor to an LED brightness value, sound level, or sound effect attribute.

Arduino, and MakeCode have a map() function with the same argument order, so it'd be good to follow the convention:

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

Although it's tempting to try to improve it:

map(value, from=(low,high), to=(low, high))

Would that be better? I personally don't feel strongly either way.

Options for the name

  • map() and range() are already taken in Python, so we shouldn't shadow them
  • transform() is a tiny bit longer and not very clear what it might be
  • scale() seems like a good word

Where to we put the function?

  • It's probably not a good idea to add it globally
  • The easiest place for users to find it is inside the microbit module, as many programmes do from microbit import *
  • It would kind of make sense to have it inside the math module, but that would divert from the CPython implementation and we should probably avoid that
    • Diverting from "generic" MicroPython's umath is probably also undesirable?
  • It might not be worth creating a new module just for this unless we had more stuff to put in it

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions