Three word location: Difference between revisions

added RPL
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
(added RPL)
 
(3 intermediate revisions by 3 users not shown)
Line 28:
Extra credit: Find a way to populate the word array with actual words.
 
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">
-- Three-word location
-- J. Carter 2023 May
-- Uses the PragmAda Reusable Components (https://github.com/jrcarter/PragmARC)
 
with Ada.Text_IO;
with PragmARC.Images;
 
procedure Three_Word is
type U43 is mod 2 ** 43;
subtype Word is String (1 .. 6);
function Image is new PragmARC.Images.Modular_Image (Number => U43);
function Image is new PragmARC.Images.Float_Image (Number => Float);
Lat : constant String := "28.3852";
Long : constant String := "-81.5638";
LL_Mix : U43 := U43 ( (Float'Value (Lat) + 90.0) * 10_000.0) * 2 ** 22 + U43 ( (Float'Value (Long) + 180.0) * 10_000.0);
W3n : U43 := LL_Mix rem 2 ** 14; -- Number for 3rd word
W2n : U43 := (LL_Mix / 2 ** 14) rem 2 ** 14; -- " " 2nd "
W1n : U43 := LL_Mix / 2 ** 28; -- " " 1st "
W1 : constant Word := 'W' & Image (W1n, Width => 5, Zero_Filled => True); -- 1st word
W2 : constant Word := 'W' & Image (W2n, Width => 5, Zero_Filled => True); -- 2nd "
W3 : constant Word := 'W' & Image (W3n, Width => 5, Zero_Filled => True); -- 3rd "
begin -- Three_Word
Ada.Text_IO.Put (Item => Lat & ", " & Long & " => " & W1 & ' ' & W2 & ' ' & W3 & " => ");
-- Reverse the process
W1n := U43'Value (W1 (2 .. 6) );
W2n := U43'Value (W2 (2 .. 6) );
W3n := U43'Value (W3 (2 .. 6) );
LL_Mix := W1n * 2 ** 28 + W2n * 2 ** 14 + W3n;
Ada.Text_IO.Put_Line (Item => Image (Float (LL_Mix / 2 ** 22) / 10_000.0 - 90.0, Fore => 0, Aft => 4, Exp => 0) & ", " &
Image (Float (LL_Mix rem 2 ** 22) / 10_000.0 - 180.0, Fore => 0, Aft => 4, Exp => 0) );
end Three_Word;
</syntaxhighlight>
 
{{out}}
<pre>
28.3852, -81.5638 => W18497 W11324 W01322 => 28.3852, -81.5638
</pre>
 
=={{header|AppleScript}}==
Line 560 ⟶ 605:
colw wloc 28.3852 _81.5638
28.3852 _81.5638</syntaxhighlight>
 
=={{header|jq}}==
'''Adapted from [[#Wren|Wren]]'''
 
'''Works with jq, the C implementation of jq'''
 
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
## Generic functions
 
# If $j is 0, then an error condition is raised;
# otherwise, assuming infinite-precision integer arithmetic,
# if the input and $j are integers, then the result will be an integer.
def idivide($j):
. as $i
| ($i % $j) as $mod
| ($i - $mod) / $j ;
 
# From bitwise.jq
# integer to stream of 0s and 1s, least significant bit first
def bitwise:
recurse( if . >= 2 then idivide(2) else empty end) | . % 2;
 
# inverse of `bitwise`
def stream_to_integer(stream):
reduce stream as $c ( {power:1 , ans: 0};
.ans += ($c * .power) | .power *= 2 )
| .ans;
 
# Convert the $j least-significant bits of the input integer to an integer
def to_int($j):
stream_to_integer(limit($j; bitwise));
 
# Take advantage of gojq's support for infinite-precision integer arithmetic:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
 
# Input is assumed to be a non-negative integer
def rightshift($n):
reduce range(0;$n) as $i (.; idivide(2)) ;
 
def lpad($len; $fill):
tostring
| ($len - length) as $l
| if $l <= 0 then .
else ($fill * $l)[:$l] + .
end;
 
## Functions to convert to and from the 'W00000' format
def toWord: "W\(lpad(5; "0"))";
 
def fromWord: .[1:] | tonumber;
 
# Latitude should be presented as a number in [-90, 90]
# and longitude as a number in [-180, 180].
def task($lat; $lon):
# convert lat and lon to positive integers
(($lat * 10000 | trunc) + 900000 ) as $ilat
| (($lon * 10000 | trunc) + 1800000) as $ilon
# build 43 bit integer comprising 21 bits (lat) and 22 bits (lon)
| ($ilat * (2 | power(22)) + $ilon) as $latlon
# isolate relevant bits
| ($latlon | rightshift(28) | to_int(15) | toWord) as $w1
| ($latlon | rightshift(14) | to_int(14) | toWord) as $w2
| ($latlon | to_int(14) | toWord) as $w3
| "Starting figures:",
" latitude = \($lat), longitude = \($lon)",
"\nThree word location is:",
([$w1, $w2, $w3] | join(" ")),
 
# now reverse the procedure
({}
| .latlon = ( ($w1 | fromWord) * (2 | power(28))
+ ($w2 | fromWord) * (2 | power(14))
+ ($w3 | fromWord ) )
| .ilat = (.latlon | rightshift(22))
| .ilon = (.latlon | to_int(22))
| .lat = ((.ilat - 900000) / 10000)
| .lon = ((.ilon - 1800000) / 10000)
| "\nAfter reversing the procedure:",
" latitude = \(.lat), longitude = \(.lon)" )
;
 
task(28.3852; -81.5638)
</syntaxhighlight>
{{output}}
<pre>
Starting figures:
latitude = 28.3852, longitude = -81.5638
 
Three word location is:
W18497 W11324 W01322
 
After reversing the procedure:
latitude = 28.3852, longitude = -81.5638
</pre>
 
=={{header|Julia}}==
Line 1,224 ⟶ 1,365:
To 3-word: ba duji be
From 3-word: -89.99999, -179.99999</pre>
 
=={{header|RPL}}==
{{works with|RPL|HP-49C}}
« 43 STWS DEC
{ 90 180 } ADD 10000 * IP R→I
EVAL SWAP 4194304 * + R→B
1 2 '''START'''
#3FFFh AND LASTARG 1 + /
'''NEXT'''
3 →LIST REVLIST
100000 ADD
1 « →STR 4 OVER SIZE 1 - SUB "W" SWAP + » DOLIST
» '<span style="color:blue">LL→W</span>' STO <span style="color:grey">''@ ( { latitude longitude } → { "word1" .. "word3" )''</span>
« « 2 OVER SIZE SUB STR→ » MAP
DUP 1 GET
2 3 '''FOR''' j
16384 * OVER j GET +
'''NEXT'''
NIP R→B #400000h / LASTARG 1 - AND
2 →LIST B→R 10000 / { 90 180 } -
» '<span style="color:blue">W→LL</span>' STO <span style="color:grey">''@ ( → { "word1" .. "word3" → { latitude longitude } )''</span>
 
{ 28.3852 -81.5638 } <span style="color:blue">LL→W</span>
DUP <span style="color:blue">W→LL</span>
{{out}}
<pre>
2: { "W18497" "W11324" "W01322" }
1: { 28.3852 -81.5638 }
</pre>
 
=={{header|Symsyn}}==
Line 1,550 ⟶ 1,721:
 
Note that bitwise operations are limited to 32-bit unsigned integers in Wren which isn't big enough here so we use BigInts instead.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./big" for BigInt
 
// functions to convert to and from the word format 'W00000'
1,150

edits