Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
|
|
@ -2,3 +2,13 @@
|
|||
(+ b1 (/ (* (- s a1)
|
||||
(- b2 b1))
|
||||
(- a2 a1))))
|
||||
|
||||
(defun map-each (a1 a2 b1 b2 ss)
|
||||
(if (endp ss)
|
||||
nil
|
||||
(cons (mapping a1 a2 b1 b2 (first ss))
|
||||
(map-each a1 a2 b1 b2 (rest ss)))))
|
||||
|
||||
(map-each 0 10 -1 0 '(0 1 2 3 4 5 6 7 8 9 10))
|
||||
|
||||
;; (-1 -9/10 -4/5 -7/10 -3/5 -1/2 -2/5 -3/10 -1/5 -1/10 0)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
import std.stdio;
|
||||
|
||||
double mapRange(in double[] a, in double[] b, in double s)
|
||||
pure nothrow {
|
||||
return b[0] + ((s - a[0]) * (b[1] - b[0]) / (a[1] - a[0]));
|
||||
pure nothrow @nogc {
|
||||
return b[0] + ((s - a[0]) * (b[1] - b[0]) / (a[1] - a[0]));
|
||||
}
|
||||
|
||||
void main() {
|
||||
const r1 = [0.0, 10.0];
|
||||
const r2 = [-1.0, 0.0];
|
||||
foreach (s; 0 .. 11)
|
||||
import std.stdio;
|
||||
|
||||
immutable r1 = [0.0, 10.0];
|
||||
immutable r2 = [-1.0, 0.0];
|
||||
foreach (immutable s; 0 .. 11)
|
||||
writefln("%2d maps to %5.2f", s, mapRange(r1, r2, s));
|
||||
}
|
||||
|
|
|
|||
56
Task/Map-range/Pascal/map-range-2.pascal
Normal file
56
Task/Map-range/Pascal/map-range-2.pascal
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Program Map(output);
|
||||
|
||||
type
|
||||
real = double;
|
||||
tRange = Array [0..1] of real;
|
||||
tMapRec = record
|
||||
mrFrom,
|
||||
mrTo : tRange;
|
||||
mrScale : real
|
||||
end;
|
||||
|
||||
function InitRange(rfrom,rTo:real):tRange;
|
||||
begin
|
||||
InitRange[0] :=rfrom;
|
||||
InitRange[1] :=rTo;
|
||||
end;
|
||||
|
||||
function InitMapRec(const fromRange, toRange: tRange):tMapRec;
|
||||
begin
|
||||
With InitMapRec do
|
||||
Begin
|
||||
mrFrom := fromRange;
|
||||
mrTo := toRange;
|
||||
mrScale := (toRange[1]-toRange[0]) / (fromRange[1]-fromRange[0]);
|
||||
end;
|
||||
end;
|
||||
|
||||
function MapRecRange(const value: real;var MR :tMapRec): real;
|
||||
begin
|
||||
with MR do
|
||||
MapRecRange := (value-mrFrom[0]) * mrScale + mrTo[0];
|
||||
end;
|
||||
|
||||
function MapRange(const value: real;const fromRange, toRange: tRange): real;
|
||||
begin
|
||||
MapRange := (value-fromRange[0]) * (toRange[1]-toRange[0]) / (fromRange[1]-fromRange[0]) + toRange[0];
|
||||
end;
|
||||
|
||||
var
|
||||
value:real;
|
||||
rFrom,rTo : tRange;
|
||||
mr : tMapRec;
|
||||
i: LongInt;
|
||||
|
||||
begin
|
||||
rFrom:= InitRange( 0, 10);
|
||||
rTo := InitRange( -1, 0);
|
||||
mr:= InitMapRec(rFrom,rTo);
|
||||
|
||||
for i := 0 to 10 do
|
||||
Begin
|
||||
value := i;
|
||||
writeln (i:4, ' maps to: ', MapRange(value,rFrom, rTo):10:6,
|
||||
MapRecRange(value,mr):10:6);
|
||||
end;
|
||||
end.
|
||||
|
|
@ -4,8 +4,7 @@
|
|||
do j=0 to 10
|
||||
say right(j,3) ' maps to ' mapRange(0,10,-1,0,j)
|
||||
end
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
|
||||
exit
|
||||
/*──────────────────────────────────MAPRANGE subroutine─────────────────*/
|
||||
mapRange: return arg(3)+(arg(5)-arg(1))*(arg(4)-arg(3))/(arg(2)-arg(1))
|
||||
/* Arguments are arg a1,a2,b1,b2,x */
|
||||
|
|
|
|||
6
Task/Map-range/Ruby/map-range-1.rb
Normal file
6
Task/Map-range/Ruby/map-range-1.rb
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def map_range(a, b, s)
|
||||
af, al, bf, bl = a.first, a.last, b.first, b.last
|
||||
bf + (s - af)*(bl - bf).quo(al - af)
|
||||
end
|
||||
|
||||
(0..10).each{|s| puts "%s maps to %g" % [s, map_range(0..10, -1..0, s)]}
|
||||
3
Task/Map-range/Ruby/map-range-2.rb
Normal file
3
Task/Map-range/Ruby/map-range-2.rb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
(0..10).each do |s|
|
||||
puts "%s maps to %s" % [s, map_range(0..10, -1..0, s)]
|
||||
end
|
||||
|
|
@ -1,9 +0,0 @@
|
|||
def map_range(a, b, s)
|
||||
af, al, bf, bl = a.first, a.last, b.first, b.last
|
||||
bf + (s - af).*(bl - bf).quo(al - af)
|
||||
end
|
||||
|
||||
11.times do |s|
|
||||
s *= 1.0 # force floating point
|
||||
print s, " maps to ", map_range((0..10), (-1..0), s), "\n"
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue