Map range: Difference between revisions

Content added Content deleted
(Hope that looks nice while being clear/easily readable?)
(Slight mod to wording of task.)
Line 1: Line 1:
{{draft task}}Given two [[wp:Interval (mathematics)|ranges]], <math>[a1,a2]</math> and <math>[b1,b2]</math>, and a value <math>s</math> in range
{{draft task}}Given two [[wp:Interval (mathematics)|ranges]], <math>[a1,a2]</math> and <math>[b1,b2]</math>; then a value <math>s</math> in range <math>[a1,a2]</math> is linearly mapped to a value <math>t</math> in range <math>[b1,b2]</math> when:

Then a value <math>s</math> in range <math>A</math> is linearly mapped to a value <math>t</math> in range <math>B</math> where:
:<math>t = b1 + {(s - a1)(b2 - b1) \over (a2 - a1)}</math>
:<math>t = b1 + {(s - a1)(b2 - b1) \over (a2 - a1)}</math>


The task is to write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map the values of the integers <math>0..10</math> from the range <math>[0, 10]</math> to the range <math>[\neg1, 0]</math>.
The task is to write a function/subroutine/... that takes two ranges and a real number, and returns the mapping of the real number from the first to the second range. Use this function to map the values of the integers <math>0..10</math> from the range <code>[0, 10]</code> to the range <code>[-1, 0]</code>.


'''Extra credit:''' Show additional idiomatic ways of performing the mapping, using tools available to the language.
'''Extra credit:''' Show additional idiomatic ways of performing the mapping, using tools available to the language.