/
mapping.html
107 lines (98 loc) · 4.54 KB
/
mapping.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<HEAD><TITLE>mapping.sp</TITLE></HEAD>
<BODY>
<HR><DIV ALIGN="center"><H1> File : mapping.sp </H1></DIV><HR>
<DIV ALIGN="center">
<TABLE CELLSPACING="0" CELLPADDING="3" WIDTH="80%" SUMMARY="">
<TR>
<TD BGCOLOR="black"><SPAN STYLE="color: #00CC00">
<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>
</SPAN>
</TD>
</TR>
</TABLE>
</DIV>
<HR>
<PRE>
#!/usr/local/bin/spar
<b>pragma</b> 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]." )
@( category, "algorithms" )
@( see_also, "http://rosettacode.org/wiki/Map_range" )
@( author, "Ken O. Burtch" );
<b>pragma</b> license( unrestricted );
<b>pragma</b> software_model( nonstandard );
<b>pragma</b> restriction( no_external_commands );
<b>procedure</b> mapping <b>is</b>
<b>type</b> first_range <b>is</b> <b>new</b> float;
<b>type</b> second_range <b>is</b> <b>new</b> float;
<FONT COLOR=green><EM>-- Spar doesn't implement ranges so we'll use constants</EM></FONT>
first_range_first : <b>constant</b> first_range := 0.0;
first_range_last : <b>constant</b> first_range := 10.0;
second_range_first : <b>constant</b> second_range := -1.0;
second_range_last : <b>constant</b> second_range := 0.0;
<b>function</b> translate (first_range_value : first_range) <b>return</b> second_range <b>is</b>
b1 : <b>constant</b> float := float( second_range_first );
b2 : <b>constant</b> float := float( second_range_last );
a1 : <b>constant</b> float := float( first_range_first );
a2 : <b>constant</b> float := float( first_range_last );
result : float;
<b>begin</b>
result := b1 + (float (first_range_value) - a1) * (b2 - b1) / (a2 - a1);
<b>return</b> second_range(result);
<b>end</b> translate;
<b>function</b> translate_back (second_range_value : second_range) <b>return</b> first_range <b>is</b>
b1 : <b>constant</b> float := float (first_range_first);
b2 : <b>constant</b> float := float (first_range_last);
a1 : <b>constant</b> float := float (second_range_first);
a2 : <b>constant</b> float := float (second_range_last);
result : float;
<b>begin</b>
result := b1 + (float (second_range_value) - a1) * (b2 - b1) / (a2 - a1);
<b>return</b> first_range (result);
<b>end</b> translate_back;
test_value : first_range := first_range_first;
translated_value : second_range;
translated_back_value : first_range;
<b>begin</b>
<b>loop</b>
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);
<b>exit</b> <b>when</b> test_value = first_range_last;
test_value := @ + 1.0;
<b>end</b> <b>loop</b>;
<b>end</b> mapping;
<FONT COLOR=green><EM>-- VIM editor formatting instructions</EM></FONT>
<FONT COLOR=green><EM>-- vim: ft=spar</EM></FONT>
</PRE></BODY></HTML>