2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,6 +1,20 @@
|
|||
Given two [[wp:Interval (mathematics)|ranges]], <math>[a_1,a_2]</math> and <math>[b_1,b_2]</math>; then a value <math>s</math> in range <math>[a_1,a_2]</math> is linearly mapped to a value <math>t</math> in range <math>[b_1,b_2]</math> when:
|
||||
:<math>t = b_1 + {(s - a_1)(b_2 - b_1) \over (a_2 - a_1)}</math>
|
||||
Given two [[wp:Interval (mathematics)|ranges]]:
|
||||
:::* <big><math>[a_1,a_2]</math></big> and
|
||||
:::* <big><math>[b_1,b_2]</math></big>;
|
||||
:::* then a value <big><math>s</math></big> in range <big><math>[a_1,a_2]</math></big>
|
||||
:::* is linearly mapped to a value <big><math>t</math></big> in range <big><math>[b_1,b_2]</math>
|
||||
</big> where:
|
||||
<br>
|
||||
|
||||
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 values from the range <code>[0, 10]</code> to the range <code>[-1, 0]</code>.
|
||||
:::* <big><big><math>t = b_1 + {(s - a_1)(b_2 - b_1) \over (a_2 - a_1)}</math></big></big>
|
||||
|
||||
'''Extra credit:''' Show additional idiomatic ways of performing the mapping, using tools available to the language.
|
||||
|
||||
;Task:
|
||||
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 values from the range <big><code> [0, 10] </code></big> to the range <big><code> [-1, 0]. </code></big>
|
||||
|
||||
|
||||
;Extra credit:
|
||||
Show additional idiomatic ways of performing the mapping, using tools available to the language.
|
||||
<br><br>
|
||||
|
|
|
|||
9
Task/Map-range/ALGOL-68/map-range.alg
Normal file
9
Task/Map-range/ALGOL-68/map-range.alg
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
# maps a real s in the range [ a1, a2 ] to the range [ b1, b2 ] #
|
||||
# there are no checks that s is in the range or that the ranges are valid #
|
||||
PROC map range = ( REAL s, a1, a2, b1, b2 )REAL:
|
||||
b1 + ( ( s - a1 ) * ( b2 - b1 ) ) / ( a2 - a1 );
|
||||
|
||||
# test the mapping #
|
||||
FOR i FROM 0 TO 10 DO
|
||||
print( ( whole( i, -2 ), " maps to ", fixed( map range( i, 0, 10, -1, 0 ), -8, 2 ), newline ) )
|
||||
OD
|
||||
12
Task/Map-range/C-sharp/map-range.cs
Normal file
12
Task/Map-range/C-sharp/map-range.cs
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
using System;
|
||||
using System.Linq;
|
||||
|
||||
public class MapRange
|
||||
{
|
||||
public static void Main() {
|
||||
foreach (int i in Enumerable.Range(0, 11))
|
||||
Console.WriteLine($"{i} maps to {Map(0, 10, -1, 0, i)}");
|
||||
}
|
||||
|
||||
static double Map(double a1, double a2, double b1, double b2, double s) => b1 + (s - a1) * (b2 - b1) / (a2 - a1);
|
||||
}
|
||||
19
Task/Map-range/C/map-range.c
Normal file
19
Task/Map-range/C/map-range.c
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
#include <stdio.h>
|
||||
|
||||
double mapRange(double a1,double a2,double b1,double b2,double s)
|
||||
{
|
||||
return b1 + (s-a1)*(b2-b1)/(a2-a1);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int i;
|
||||
puts("Mapping [0,10] to [-1,0] at intervals of 1:");
|
||||
|
||||
for(i=0;i<=10;i++)
|
||||
{
|
||||
printf("f(%d) = %g\n",i,mapRange(0,10,-1,0,i));
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
/*REXX program maps a range of numbers from one range to another range. */
|
||||
rangeA = 0 10 /*or: rangeA = ' 0 10 ' */
|
||||
rangeB = -1 0 /*or: rangeB = " -1 0 " */
|
||||
/*REXX program maps and displays a range of numbers from one range to another range.*/
|
||||
rangeA = 0 10 /*or: rangeA = ' 0 10 ' */
|
||||
rangeB = -1 0 /*or: rangeB = " -1 0 " */
|
||||
parse var RangeA L H
|
||||
inc=1
|
||||
do j=L to H by inc*(1-2*sign(H<L)) /*BY: +inc │ -inc) */
|
||||
say right(j,digits()) ' maps to ' mapRange(rangeA,rangeB,j)
|
||||
do j=L to H by inc * (1 - 2 * sign(H<L) ) /*BY: either +inc or -inc */
|
||||
say right(j, digits()) ' maps to ' mapRange(rangeA, rangeB, j)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MAPRANGE subroutine─────────────────*/
|
||||
mapRange: procedure; arg a1 a2,b1 b2,s; return b1+ (s-a1)*(b2-b1)/(a2-a1)
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mapRange: procedure; parse arg a1 a2,b1 b2,s; return b1 + (s-a1) * (b2-b1) / (a2-a1)
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
/*REXX pgm maps a range of numbers from one range to another with inc=½.*/
|
||||
rangeA = 10 0 /*or: rangeA = ' 10 0 ' */
|
||||
rangeB = -1 0 /*or: rangeB = " -1 0 " */
|
||||
parse var RangeA L H /*note the LOW & HIGH values in A*/
|
||||
inc= 1/2 /*use a different step size (inc)*/
|
||||
do j=L to H by inc*(1-2*sign(H<L)) /*BY: +inc │ -inc) */
|
||||
say right(j,9) ' maps to ' mapRange(rangeA,rangeB,j)
|
||||
/*REXX program maps and displays a range of numbers from one range to another range.*/
|
||||
rangeA = 10 0 /*or: rangeA = ' 10 0 ' */
|
||||
rangeB = -1 0 /*or: rangeB = " -1 0 " */
|
||||
parse var RangeA L H /*note the LOW & HIGH values in A*/
|
||||
inc= 1/2 /*use a different step size (inc)*/
|
||||
do j=L to H by inc * (1 - 2 * sign(H<L) ) /*BY: either +inc or -inc */
|
||||
say right(j, digits()) ' maps to ' mapRange(rangeA, rangeB, j)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MAPRANGE subroutine─────────────────*/
|
||||
mapRange: procedure; arg a1 a2,b1 b2,s; return b1+ (s-a1)*(b2-b1)/(a2-a1)
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mapRange: procedure; parse arg a1 a2,b1 b2,s; return b1 + (s-a1) * (b2-b1) / (a2-a1)
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
/*REXX program maps a range of numbers from one range to another range. */
|
||||
/*REXX program maps and displays a range of numbers from one range to another range.*/
|
||||
rangeA = 0 10
|
||||
rangeB = -1 0
|
||||
inc=1
|
||||
inc = 1
|
||||
call mapRange rangeA, RangeB, inc
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────MAPRANGE subroutine─────────────────*/
|
||||
mapRange: procedure; parse a1 a2, b1 b2, inc
|
||||
do s=a1 to a2 by inc*(1-2*sign(a2<a1)) /*BY: +inc │ -inc */
|
||||
say right(s,digits()) ' maps to ' b1+(s-a1)*(b2-b1)/(a2-a1)
|
||||
end /*s*/ /*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
|
||||
return /*════════════t════════════*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
mapRange: procedure; parse arg a1 a2, b1 b2, inc /* [↓] BY: either +inc or -inc*/
|
||||
do s=a1 to a2 by inc * (1 - 2 * sign(a2 < a1) )
|
||||
say right(s,digits()) ' maps to ' b1 + (s-a1) * (b2-b1) / (a2-a1)
|
||||
end /*s*/ /*↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑*/
|
||||
return /*═══════════════t═══════════════*/
|
||||
|
|
|
|||
13
Task/Map-range/Rust/map-range.rust
Normal file
13
Task/Map-range/Rust/map-range.rust
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
use std::f64;
|
||||
|
||||
fn map_range(from_range: (f64, f64), to_range: (f64, f64), s: f64) -> f64 {
|
||||
to_range.0 + (s - from_range.0) * (to_range.1 - to_range.0) / (from_range.1 - from_range.0)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let input: Vec<f64> = vec![0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0];
|
||||
let result = input.into_iter()
|
||||
.map(|x| map_range((0.0, 10.0), (-1.0, 0.0), x))
|
||||
.collect::<Vec<f64>>();
|
||||
print!("{:?}", result);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue