Map range: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Quackery.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(12 intermediate revisions by 6 users not shown)
Line 1,085:
9.0 maps to -0.1
10.0 maps to 0.0</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">define a1 = 0, b1 = 0, a2 = 0, b2 = 0
 
for i = 0 to 10
 
let s = i
let a1 = 0
let a2 = 10
let b1 = -1
let b2 = 0
 
print i, " : ", b1 + ( s - a1 ) * ( b2 - b1 ) / ( a2 - a1 )
 
next i</syntaxhighlight>
{{out| Output}}<pre>0 : -1
1 : -0.9000
2 : -0.8000
3 : -0.7000
4 : -0.6000
5 : -0.5000
6 : -0.4000
7 : -0.3000
8 : -0.2000
9 : -0.1000
10 : 0</pre>
 
=={{header|D}}==
Line 2,163 ⟶ 2,189:
print( string.format( "f(%d) = %f", i, map_range( 0, 10, -1, 0, i ) ) )
end</syntaxhighlight>
 
=={{header|M2000 Interpreter}}==
 
=== Using Class ===
<syntaxhighlight lang="m2000 interpreter">
module MapRange {
class Map {
private:
a, b, f
public:
value (x){
=.b+(x-.a)*.f
}
class:
module Map (.a,a2,.b,b2) {
if a2-.a=0 then error "wrong parameters"
.f<=(b2-.b)/(a2-.a)
}
}
m1=Map(0,10, -1, 0)
for i=0 to 10
Print i," maps to ";m1(i)
next
}
MapRange
</syntaxhighlight>
 
=== Using Lambda ===
 
<syntaxhighlight lang="m2000 interpreter">
module MapRange {
Map=lambda (a,a2,b,b2) -> {
if a2-a=0 then error "wrong parameters"
f=(b2-b)/(a2-a)
=lambda a,b,f (x)->b+(x-a)*f
}
m1=Map(0,10, -1, 0)
for i=0 to 10
Print i," maps to ";m1(i)
next
}
MapRange
</syntaxhighlight>
 
Same output for both versions
 
{{out}}
<pre>
0 maps to -1
1 maps to -0.9
2 maps to -0.8
3 maps to -0.7
4 maps to -0.6
5 maps to -0.5
6 maps to -0.4
7 maps to -0.3
8 maps to -0.2
9 maps to -0.1
10 maps to 0
</pre>
 
=={{header|Maple}}==
Line 2,704 ⟶ 2,791:
=={{header|Quackery}}==
 
As Quackery does not support reals (or floating point), the function takes the argument s as a decimal string, and returns the result, t as a rational number.
 
<syntaxhighlight lang="Quackery"> [ $ "bigrat.qky" loadfile ] now!
Line 3,009 ⟶ 3,096:
9 maps to -0.1
10 maps to 0
</pre>
 
=={{header|RPL}}==
Ranges are entered as complex numbers to ease input and shorten code
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ → ra rb s
≪ rb ra - DUP RE SWAP IM /
s ra RE - * rb RE +
≫ ≫ 'MAP' STO
|
''( (a1, a2) (b1, b2) s -- t )''
Get (b2 - b1)/(a2 - a1)
Multiply by (s - a1) and add b1
|}
{{in}}
<pre>
(0,10) (-1,0) 0 MAP
(0,10) (-1,0) 4 MAP
(0,10) (-1,0) 10 MAP
</pre>
{{out}}
<pre>
3: -1
2: -0.6
1: 0
</pre>
===Basic RPL code===
No specific data structure, no local variable, full stack calculation. Shorter, but less practical in use and difficult to read.
≪ SWAP 3 PICK -
SWAP 5 PICK - *
ROT 4 ROLL - / +
≫ 'MAP' STO
{{in}}
<pre>
0 10 -1 0 0 MAP
0 10 -1 0 4 MAP
0 10 -1 0 10 MAP
</pre>
{{out}}
<pre>
3: -1
2: -0.6
1: 0
</pre>
 
Line 3,155 ⟶ 3,291:
10 maps to 0</pre>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "mapping" )
@( description, "The task is to write a function/subroutine/... that takes" )
@( description, "two ranges and a real number, and returns the mapping of" )
@( description, "the real number from the first to the second range. Use" )
@( description, "this function to map values from the range [0, 10] to the" )
@( description, "range [-1, 0]." )
@( see_also, "http://rosettacode.org/wiki/Map_range" )
@( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure mapping is
type first_range is new float;
type second_range is new float;
-- Spar doesn't implement ranges so we'll use constants
first_range_first : constant first_range := 0.0;
first_range_last : constant first_range := 10.0;
second_range_first : constant second_range := -1.0;
second_range_last : constant second_range := 0.0;
 
function translate (first_range_value : first_range) return second_range is
b1 : constant float := float( second_range_first );
b2 : constant float := float( second_range_last );
a1 : constant float := float( first_range_first );
a2 : constant float := float( first_range_last );
result : float;
begin
result := b1 + (float (first_range_value) - a1) * (b2 - b1) / (a2 - a1);
return second_range(result);
end translate;
 
function translate_back (second_range_value : second_range) return first_range is
b1 : constant float := float (first_range_first);
b2 : constant float := float (first_range_last);
a1 : constant float := float (second_range_first);
a2 : constant float := float (second_range_last);
result : float;
begin
result := b1 + (float (second_range_value) - a1) * (b2 - b1) / (a2 - a1);
return first_range (result);
end translate_back;
 
test_value : first_range := first_range_first;
translated_value : second_range;
translated_back_value : first_range;
begin
loop
translated_value := translate( test_value );
translated_back_value := translate_back( translated_value );
 
? strings.image(test_value) & " maps to: "
& strings.image (translated_value);
? strings.image(translated_value) & " maps back to: "
& strings.image (translated_back_value);
exit when test_value = first_range_last;
test_value := @ + 1.0;
end loop;
end mapping;</syntaxhighlight>
{{out}}
<pre>
$ spar mapping.sp
0.0 maps to: -1.00000000000000E+00
-1.00000000000000E+00 maps back to: 0.00000000000000E+00
1.00000000000000E+00 maps to: -9.00000000000000E-01
-9.00000000000000E-01 maps back to: 1.00000000000000E+00
2.00000000000000E+00 maps to: -8.00000000000000E-01
-8.00000000000000E-01 maps back to: 2.00000000000000E+00
3.00000000000000E+00 maps to: -7.00000000000000E-01
-7.00000000000000E-01 maps back to: 3.00000000000000E+00
4.00000000000000E+00 maps to: -6.00000000000000E-01
-6.00000000000000E-01 maps back to: 4.00000000000000E+00
5.00000000000000E+00 maps to: -5.00000000000000E-01
-5.00000000000000E-01 maps back to: 5.00000000000000E+00
6.00000000000000E+00 maps to: -4.00000000000000E-01
-4.00000000000000E-01 maps back to: 6.00000000000000E+00
7.00000000000000E+00 maps to: -3.00000000000000E-01
-3.00000000000000E-01 maps back to: 7.00000000000000E+00
8.00000000000000E+00 maps to: -2.00000000000000E-01
-2.00000000000000E-01 maps back to: 8.00000000000000E+00
9.00000000000000E+00 maps to: -1.00000000000000E-01
-1.00000000000000E-01 maps back to: 9.00000000000000E+00
1.00000000000000E+01 maps to: 0.00000000000000E+00
0.00000000000000E+00 maps back to: 1.00000000000000E+01</pre>
=={{header|Stata}}==
The following program will map a variable to a new variable. It accepts '''if''' and '''in''' conditions.
Line 3,365 ⟶ 3,588:
=={{header|Wren}}==
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var mapRange = Fn.new { |a, b, s| b.from + (s - a.from) * (b.to - b.from) / (a.to - a.from) }
Line 3,373 ⟶ 3,596:
for (s in a) {
var t = mapRange.call(a, b, s)
var f = Fmt.print(t"$2d >=maps 0)to ?$ h", "s, : ""t)
System.print("%(Fmt.d(2, s)) maps to %(f)%(t)")
}</syntaxhighlight>
 
9,482

edits