Three word location: Difference between revisions

added RPL
(added RPL)
 
(12 intermediate revisions by 11 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}}==
===Index words===
When the words are index-based as in the task description, it's not necessary to generate all 28126.
<langsyntaxhighlight lang="applescript">on locationToWords({latitude, longitude})
-- "Convert" the coordinates to positive integers by adding enough degrees to ensure positive results,
-- multiplying by enough to left shift by four decimal places, and rounding.
Line 63 ⟶ 108:
set threeWords to locationToWords(location)
set checkLocation to wordsToLocation(threeWords)
return {location, threeWords, checkLocation}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{28.3852, -81.5638}, {"W18497", "W11324", "W01322"}, {28.3852, -81.5638}}</langsyntaxhighlight>
 
===Actual words===
<langsyntaxhighlight lang="applescript">on locationToWords({latitude, longitude}, listOfWords)
script o
property wordList : listOfWords
Line 128 ⟶ 173:
set threeWords to locationToWords(location, o's wordList)
set checkLocation to wordsToLocation(threeWords, o's wordList)
return {location, threeWords, checkLocation}</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{{28.3852, -81.5638}, {"quote", "hygiene", "aristotelean"}, {28.3852, -81.5638}}</langsyntaxhighlight>
 
=={{header|AutoHotkey}}==
Conversion based on Wren<br>
WordList From link suggested by Symsyn
<syntaxhighlight lang="autohotkey">URLDownloadToFile, http://www-personal.umich.edu/~jlawler/wordlist, % A_Temp "\wordlist.txt"
FileRead, wordList, % A_Temp "\wordlist.txt"
 
LL := [28.3852, -81.5638]
num := LL2num(LL)
words := LL2words(wordList, LL)
LL2 := words2LL(wordList, words)
 
MsgBox, 262144, , % result := "
(
LL = " LL.1 ", " LL.2 "
LL2num = " num.1 ", " num.2 ", " num.3 "
LL2words = " words.1 ", " words.2 ", " words.3 "
words2LL = " LL2.1 ", " LL2.2 "
)"
return
;-----------------------------------------------
LL2words(wordList, LL){ ; Latitude/Longitude to 3 words
num := LL2num(LL)
wli := wordList(wordList).1
return [wli[num.1], wli[num.2], wli[num.3]]
}
;-----------------------------------------------
words2LL(wordList, w){ ; 3 words to Latitude/Longitude
iow := wordList(wordList).2
LL := num2LL([iow[w.1], iow[w.2], iow[w.3]])
return [ll.1, ll.2]
}
;-----------------------------------------------
wordList(wordList){ ; word list to two arrays
wli:=[], iow:=[] ; word list index, index of word
for i, word in StrSplit(wordList, "`n", "`r")
if (word ~= "^[a-z]+$") && (StrLen(word) <= 8) && (StrLen(word) > 3)
wli.Push(word), iow[word] := wli.MaxIndex()
return [wli, iow]
}
;-----------------------------------------------
LL2num(LL){ ; Latitude/Longitude to 3 numbers
ilat := LL.1*10000 + 900000
ilon := LL.2*10000 + 1800000
latlon := (ilat << 22) + ilon
return [(latlon >> 28) & 0x7fff, (latlon >> 14) & 0x3fff, latlon & 0x3fff]
}
;-----------------------------------------------
num2LL(w){ ; 3 numbers to Latitude/Longitude
latlon := (w.1 << 28) | (w.2 << 14) | w.3
ilat := latlon >> 22
ilon := latlon & 0x3fffff
return [(ilat-900000)/10000, (ilon-1800000)/10000]
}
;-----------------------------------------------</syntaxhighlight>
{{out}}
<pre>LL = 28.3852, -81.5638
LL2num = 18497, 11324, 1322
LL2words = malleus, fasten, analytic
words2LL = 28.385200, -81.563800</pre>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 193 ⟶ 298:
printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 210 ⟶ 315:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Three_word_location;
 
Line 356 ⟶ 461:
end.
 
</syntaxhighlight>
</lang>
 
{{out}}
Line 371 ⟶ 476:
 
 
=={{header|FreeBASIC}}==
{{trans|Nim}}
<syntaxhighlight lang="freebasic">Print "Starting figures:"
Dim As Double lat = 28.3852, longi = -81.5638
Print Using " latitude = &, longitude = &"; lat; longi
 
' Convert "lat" and "long" to positive integers.
Dim As Integer ilat = Cint(lat * 10000 + 900000)
Dim As Integer ilong = Cint(longi * 10000 + 1800000)
 
' Build 43 bit int comprising 21 bits (lat) and 22 bits (lon).
Dim As Double latlong = ilat Shl 22 + ilong
 
' Isolate relevant bits.
Dim As Integer w1 = latlong Shr 28 And &H7fff
Dim As Integer w2 = latlong Shr 14 And &H3fff
Dim As Integer w3 = latlong And &H3fff
 
' Convert to word format.
Dim As String*5 w1s = String(5, "0"), w2s = String(5, "0"), w3s = String(5, "0")
Mid(w1s, 6-Len(Str(w1))) = Str(w1)
Mid(w2s, 6-Len(Str(w2))) = Str(w2)
Mid(w3s, 6-Len(Str(w3))) = Str(w3)
 
' Print the results.
Print !"\nThree word location is:"
Print Using " W\ \ W\ \ W\ \"; w1s; w2s; w3s
latlong = w1 Shl 28 Or w2 Shl 14 Or w3
ilat = latlong Shr 22
ilong = latlong And &H3fffff
lat = (ilat - 900000) / 10000
longi = (ilong - 1800000) / 10000
' Print the results.
Print !"\nAfter reversing the procedure:"
Print Using " latitude = &, longitude = &"; lat; longi
Sleep</syntaxhighlight>
{{out}}
<pre>Igual que la entrada de Nim</pre>
 
=={{header|Go}}==
{{trans|Wren}}
Though no need for big integers as we have int64 built in.
<langsyntaxhighlight lang="go">package main
 
import (
Line 430 ⟶ 575:
fmt.Println("\nAfter reversing the procedure:")
fmt.Printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon)
}</langsyntaxhighlight>
 
{{out}}
Line 439 ⟶ 584:
Three word location is:
W18497 W11324 W01322
 
After reversing the procedure:
latitude = 28.3852, longitude = -81.5638
</pre>
 
=={{header|J}}==
 
To take full advantage of the bit space, I think we should use 11650.8444 for the multiplier (2^21 - log2 180), but that's not what was asked for here.<syntaxhighlight lang="j">wloc=: {{ ;:inv wordlist{~ (15 14 14#i.3)#./.;(21 22#&.>2) #:&.> <.0.5+10000*90 180+y }}
colw=: {{ _90 _180+1e_4*21({.,&#.}.);(15 14 14#&.>2)#:&.>wordlist i.;:y }}</syntaxhighlight>
 
With <code>wordlist=: ('W',}.@":)&.> 1e5+i.3e4</code>
 
<syntaxhighlight lang="j"> wloc 28.3852 _81.5638
W18497 W11324 W01322
colw wloc 28.3852 _81.5638
28.3852 _81.5638</syntaxhighlight>
 
With <code>wordlist=: cutLF CR-.~fread 'wordlist'</code> based on the file 'wordlist' from http://www-personal.umich.edu/~jlawler/wordlist <syntaxhighlight lang="j"> wloc 28.3852 _81.5638
diplotene chamomile aeroplanist
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:
Line 446 ⟶ 704:
=={{header|Julia}}==
Direct translation from the SymSyn example given by the task creator, though note that idiomatic Julia would usually code this as two small encode() and decode() functions.
<langsyntaxhighlight lang="julia">
# Three Word Location - convert latitude and longitude to three words
LAT = 28.3852
Line 496 ⟶ 754:
# display values
println("latitude = $lat longitude = $lon")
</langsyntaxhighlight>{{out}}
<pre>
W18497 W11324 W01322
Line 503 ⟶ 761:
 
=== Idiomatic version with scrambling===
<langsyntaxhighlight lang="julia">using Random
 
const LAT = 28.3852
Line 544 ⟶ 802:
lat, lon = threeworddecode(words..., 12345678)
println("latitude = $lat longitude = $lon")
</langsyntaxhighlight>{{out}}
<pre>
W18497 W11324 W01322
Line 553 ⟶ 811:
latitude = -81.5638 longitude = 28.3852
</pre>
 
=={{header|Kotlin}}==
{{trans|Go}}
<syntaxhighlight lang="scala">fun toWord(w: Long): String {
return "W%05d".format(w)
}
 
fun fromWord(ws: String): Long {
return ws.substring(1).toUInt().toLong()
}
 
fun main() {
println("Starting figures:")
var lat = 28.3852
var lon = -81.5638
println(" latitude = %.4f, longitude = %.4f".format(lat, lon))
println()
 
// convert lat and lon to positive integers
var ilat = (lat * 10000 + 900000).toLong()
var ilon = (lon * 10000 + 1800000).toLong()
 
// build 43 bit BigInt comprising 21 bits (lat) and 22 bits (lon)
var latlon = (ilat shl 22) + ilon
 
// isolate relevant bits
var w1 = (latlon shr 28) and 0x7fff
var w2 = (latlon shr 14) and 0x3fff
var w3 = latlon and 0x3fff
 
// convert to word format
val w1s = toWord(w1)
val w2s = toWord(w2)
val w3s = toWord(w3)
 
// and print the results
println("Three word location is:")
println(" $w1s $w2s $w3s")
println()
 
/* now reverse the procedure */
w1 = fromWord(w1s)
w2 = fromWord(w2s)
w3 = fromWord(w3s)
 
latlon = (w1 shl 28) or (w2 shl 14) or w3
ilat = latlon shr 22
ilon = latlon and 0x3fffff
lat = (ilat - 900000).toDouble() / 10000
lon = (ilon - 1800000).toDouble() / 10000
 
// and print the results
println("After reversing the procedure:")
println(" latitude = %.4f, longitude = %.4f".format(lat, lon))
}</syntaxhighlight>
{{out}}
<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|Lua}}==
{{trans|C}}
<syntaxhighlight lang="lua">function toWord(w)
return string.format("W%05d", w)
end
 
function fromWord(ws)
return tonumber(string.sub(ws, 2, -1))
end
 
-------------------------------------------------------------------------
 
print("Starting figures:")
lat = 28.3852
lon = -81.5638
print(string.format(" latitude = %0.4f, longitude = %0.4f", lat, lon))
print()
 
-- convert from lat and lon to positive integers
ilat = lat * 10000 + 900000
ilon = lon * 10000 + 1800000
 
-- build 43 bit number comprising 21 bits (lat) and 22 bits (lon)
latlon = math.floor((ilat << 22) + ilon)
 
-- isloate relevant bits
w1 = (latlon >> 28) & 0x7fff
w2 = (latlon >> 14) & 0x3fff
w3 = latlon & 0x3fff
 
-- convert to word format
w1s = toWord(w1)
w2s = toWord(w2)
w3s = toWord(w3)
 
-- and print the results
print("Three word location is:")
print(" " .. w1s .. " " .. w2s .. " " .. w3s)
print()
 
-------------------------------------------------------------------------
 
-- now reverse the procedure
w1 = fromWord(w1s)
w2 = fromWord(w2s)
w3 = fromWord(w3s)
 
latlon = (w1 << 28) | (w2 << 14) | w3
ilat = latlon >> 22
ilon = latlon & 0x3fffff
lat = (ilat - 900000) / 10000
lon = (ilon - 1800000) / 10000
 
-- and print the results
print("After reversing the procedure:")
print(string.format(" latitude = %0.4f, longitude = %0.4f", lat, lon))</syntaxhighlight>
{{out}}
<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|Nim}}==
{{trans|Go}}
<langsyntaxhighlight Nimlang="nim">import strformat, strutils
 
func toWord(w: int64): string = &"W{w:05}"
Line 605 ⟶ 993:
# Print the results.
echo "\nAfter reversing the procedure:"
echo &" latitude = {lat:0.4f}, longitude = {long:0.4f}"</langsyntaxhighlight>
 
{{out}}
Line 619 ⟶ 1,007:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 676 ⟶ 1,064:
printf "Coordinates: %s, %s (%s)\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n From Index: %s, %s\n\n",
$lat, $lon, $description, join(' ',@index), join(' ',@words), w_decode(\@words), w_decode(\@index, sub { shift() });
}</langsyntaxhighlight>
{{out}}
<pre>Coordinates: 51.4337, -0.2141 (Wimbledon)
Line 716 ⟶ 1,104:
=={{header|Phix}}==
{{trans|Go}}
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>requires("0.8.2") -- (new || && << and >> operators, pre-0.8.2 code left in as comments)
<span style="color: #000080;font-style:italic;">--
 
-- demo\rosetta\Three_word_location.exw
function toWord(integer w)
-- ====================================
return sprintf("W%05d", w)
--</span>
end function
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
 
function fromWord(string ws)
<span style="color: #008080;">function</span> <span style="color: #000000;">toWord</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">)</span>
sequence r = scanf(ws,"W%05d")
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"W%05d"</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w</span><span style="color: #0000FF;">)</span>
integer res = r[1][1]
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return res
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">fromWord</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">ws</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ws</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"W%05d"</span><span style="color: #0000FF;">)</span>
printf(1,"Starting figures:\n")
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
atom lat = 28.3852,
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
lon = -81.5638
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
printf(1," latitude = %0.4f, longitude = %0.4f\n", {lat, lon})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Starting figures:\n"</span><span style="color: #0000FF;">)</span>
-- convert lat and lon to positive integers
<span style="color: #004080;">atom</span> <span style="color: #000000;">lat</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">28.3852</span><span style="color: #0000FF;">,</span>
integer ilat := floor((lat+90)*10000),
<span style="color: #000000;">lon</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">81.5638</span>
ilon := floor((lon+180)*10000)
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" latitude = %0.4f, longitude = %0.4f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">lat</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lon</span><span style="color: #0000FF;">})</span>
-- build 43 bit BigInt comprising 21 bits (lat) and 22 bits (lon)
<span style="color: #000080;font-style:italic;">-- convert lat and lon to positive integers</span>
-- (Std phix atoms have 53/64 bits of precision on 32/64 bit, both plenty)
<span style="color: #004080;">integer</span> <span style="color: #000000;">ilat</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">lat</span><span style="color: #0000FF;">+</span><span style="color: #000000;">90</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">),</span>
--atom latlon := ilat*power(2,22) + ilon -- (pre-0.8.2)
<span style="color: #000000;">ilon</span> <span style="color: #0000FF;">:=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">((</span><span style="color: #000000;">lon</span><span style="color: #0000FF;">+</span><span style="color: #000000;">180</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">)</span>
atom latlon := (ilat << 22) + ilon
<span style="color: #000080;font-style:italic;">-- build 43 bit BigInt comprising 21 bits (lat) and 22 bits (lon)
-- isolate relevant bits
-- (std phix atoms have 53/64 bits of precision on 32/64 bit, both plenty)</span>
--integer w1 = and_bits(floor(latlon/power(2,28)),0x7fff), --("")
<span style="color: #004080;">atom</span> <span style="color: #000000;">latlon</span> <span style="color: #0000FF;">:=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">ilat</span> <span style="color: #0000FF;"><<</span> <span style="color: #000000;">22</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">ilon</span>
-- w2 = and_bits(floor(latlon/power(2,14)),0x3fff),
-- w3 = and_bits(latlon,0x3fff)
<span style="color: #000080;font-style:italic;">-- isolate relevant bits</span>
integer w1 = (latlon >> 28) && 0x7fff,
<span style="color: #004080;">integer</span> <span style="color: #000000;">w1</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">latlon</span> <span style="color: #0000FF;">>></span> <span style="color: #000000;">28</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&&</span> <span style="color: #000000;">0x7fff</span><span style="color: #0000FF;">,</span>
w2 = (latlon >> 14) && 0x3fff,
<span style="color: #000000;">w2</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">latlon</span> <span style="color: #0000FF;">>></span> <span style="color: #000000;">14</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">&&</span> <span style="color: #000000;">0x3fff</span><span style="color: #0000FF;">,</span>
w3 = latlon && 0x3fff
<span style="color: #000000;">w3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">latlon</span> <span style="color: #0000FF;">&&</span> <span style="color: #000000;">0x3fff</span>
-- convert to word format
<span style="color: #000080;font-style:italic;">-- convert to word format</span>
string w1s = toWord(w1),
<span style="color: #004080;">string</span> <span style="color: #000000;">w1s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">toWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w1</span><span style="color: #0000FF;">),</span>
w2s = toWord(w2),
<span style="color: #000000;">w2s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">toWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w2</span><span style="color: #0000FF;">),</span>
w3s = toWord(w3)
<span style="color: #000000;">w3s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">toWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w3</span><span style="color: #0000FF;">)</span>
-- and print the results
<span style="color: #000080;font-style:italic;">-- and print the results</span>
printf(1,"\nThree word location is:\n")
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nThree word location is:\n"</span><span style="color: #0000FF;">)</span>
printf(1," %s %s %s\n", {w1s, w2s, w3s})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" %s %s %s\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">w1s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w2s</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">w3s</span><span style="color: #0000FF;">})</span>
-- now reverse the procedure
<span style="color: #000080;font-style:italic;">-- now reverse the procedure</span>
w1 = fromWord(w1s)
<span style="color: #000000;">w1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fromWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w1s</span><span style="color: #0000FF;">)</span>
w2 = fromWord(w2s)
<span style="color: #000000;">w2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fromWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w2s</span><span style="color: #0000FF;">)</span>
w3 = fromWord(w3s)
<span style="color: #000000;">w3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fromWord</span><span style="color: #0000FF;">(</span><span style="color: #000000;">w3s</span><span style="color: #0000FF;">)</span>
-- NB: or_bits (likewise ||), being expressly 32-bit, is NOT appropriate here...
<span style="color: #000080;font-style:italic;">-- NB: or_bits (likewise ||), being expressly 32-bit, is NOT appropriate here...</span>
--latlon = w1*power(2,28) + w2*power(2,14) + w3 -- (pre-0.8.2)
<span style="color: #000000;">latlon</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">w1</span> <span style="color: #0000FF;"><<</span> <span style="color: #000000;">28</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">w2</span> <span style="color: #0000FF;"><<</span> <span style="color: #000000;">14</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">w3</span>
--ilat = floor(latlon/power(2,22))
<span style="color: #000000;">ilat</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">latlon</span> <span style="color: #0000FF;">>></span> <span style="color: #000000;">22</span>
--ilon = and_bits(latlon,0x3fffff)
<span style="color: #000000;">ilon</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">latlon</span> <span style="color: #0000FF;">&&</span> <span style="color: #000000;">0x3fffff</span>
latlon = (w1 << 28) + (w2 << 14) + w3
<span style="color: #000000;">lat</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ilat</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10000</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">90</span>
ilat = latlon >> 22
<span style="color: #000000;">lon</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ilon</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10000</span> <span style="color: #0000FF;">-</span> <span style="color: #000000;">180</span>
ilon = latlon && 0x3fffff
lat = ilat/10000 - 90
<span style="color: #000080;font-style:italic;">-- and print the results</span>
lon = ilon/10000 - 180
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\nAfter reversing the procedure:\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" latitude = %0.4f, longitude = %0.4f\n"</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">lat</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lon</span><span style="color: #0000FF;">})</span>
-- and print the results
<!--</syntaxhighlight>-->
printf(1,"\nAfter reversing the procedure:\n")
printf(1," latitude = %0.4f, longitude = %0.4f\n", {lat, lon})</lang>
{{out}}
<pre>
Line 810 ⟶ 1,197:
* u - long u (due boo moo)
 
<syntaxhighlight lang="raku" perl6line># SYNTHETICS HANDLING
my @synth = flat < b d f h j k l m n p r s t w y z > X~ < a e i o u >;
my %htnys = @synth.antipairs;
Line 845 ⟶ 1,232:
printf "Coordinates: %s, %s\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n From Index: %s, %s\n\n",
$lat, $lon, @index.Str, @words.Str, w-decode(@words), w-decode @index, :f( { $_ } );
}</langsyntaxhighlight>
{{out}}
 
Line 912 ⟶ 1,299:
10 times better accuracy in the same three, 6-letter word space.
 
<syntaxhighlight lang="raku" perl6line># SYNTHETICS HANDLING
my @synth = flat < b d f j k n p r s t w > X~ < a e i o u >;
my %htnys = @synth.antipairs;
Line 947 ⟶ 1,334:
printf "Coordinates: %s, %s\n To Index: %s\n To 3-word: %s\nFrom 3-word: %s, %s\n\n",
$lat, $lon, w-encode($lat, $lon, :f({$_})).Str, @words.Str, w-decode(@words);
}</langsyntaxhighlight>
{{out}}
<pre>Coordinates: 51.43372, -0.21412
Line 978 ⟶ 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}}==
<syntaxhighlight lang="symsyn">
<lang Symsyn>
| Three Word Location - convert latitude and longitude to three words
 
Line 1,091 ⟶ 1,508:
return -1
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,100 ⟶ 1,517:
Using Real Words
 
<syntaxhighlight lang="symsyn">
<lang Symsyn>
 
| Three Word Location - convert latitude and longitude to three words
Line 1,224 ⟶ 1,641:
return -1
 
</syntaxhighlight>
</lang>
 
{{Output}}
Line 1,230 ⟶ 1,647:
ling everyone amoral
latitude = 28.3852 longitude = -81.5638
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="go">import strconv
fn to_word(w i64) string { return 'W${w:05}' }
fn from_word(ws string) i64 {
u, _ := strconv.common_parse_uint2(ws[1..], 10, 64)
return i64(u)
}
fn main() {
println("Starting figures:")
mut lat := 28.3852
mut lon := -81.5638
println(" latitude = ${lat:.4}, longitude = ${lon:.4}")
// convert lat and lon to positive integers
mut ilat := i64(lat*10000 + 900000)
mut ilon := i64(lon*10000 + 1800000)
// build 43 bit BigInt comprising 21 bits (lat) and 22 bits (lon)
mut latlon := (ilat << 22) + ilon
// isolate relevant bits
mut w1 := (latlon >> 28) & 0x7fff
mut w2 := (latlon >> 14) & 0x3fff
mut w3 := latlon & 0x3fff
// convert to word format
w1s := to_word(w1)
w2s := to_word(w2)
w3s := to_word(w3)
// and print the results
println("\nThree word location is:")
println(" $w1s $w2s $w3s")
/* now reverse the procedure */
w1 = from_word(w1s)
w2 = from_word(w2s)
w3 = from_word(w3s)
latlon = (w1 << 28) | (w2 << 14) | w3
ilat = latlon >> 22
ilon = latlon & 0x3fffff
lat = f64(ilat-900000) / 10000
lon = f64(ilon-1800000) / 10000
// and print the results
println("\nAfter reversing the procedure:")
println(" latitude = ${lat:.4}, longitude = ${lon:.4}")
}</syntaxhighlight>
 
{{out}}
<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>
 
Line 1,238 ⟶ 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.
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./big" for BigInt
 
// functions to convert to and from the word format 'W00000'
Line 1,284 ⟶ 1,767:
// and print the results
System.print("\nAfter reversing the procedure:")
Fmt.print(" latitude = $0.4f, longitude = $0.4f", lat, lon)</langsyntaxhighlight>
 
{{out}}
1,150

edits