Skip to content

Instantly share code, notes, and snippets.

@ahue
Created February 14, 2021 10:04
Show Gist options
  • Select an option

  • Save ahue/f7beb83c154451fe21517a34e41e3bb6 to your computer and use it in GitHub Desktop.

Select an option

Save ahue/f7beb83c154451fe21517a34e41e3bb6 to your computer and use it in GitHub Desktop.
from typing import Union
def scale(x: Union[int|float],
min_x: Union[int|float], max_x: Union[int|float],
a: Union[int|float], b: Union[int|float]) -> float:
"""
Scale a number x relative to domain [min_x, max_x] to a domain [a,b]
Parameters:
x (int|float): The value to scale
min_x (int|float): Lower bound of the domain x is coming from
max_x (int|float): Upper bound of the domain x is coming from
a (int|float): Lower bound of the target domain
b (int|float): Upper bound of the target domain
Returns:
scaled_x (float): The scaled value of x
"""
return (b-a) * (x-min_x) / (max_x-min_x) + a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment