Three word location: Difference between revisions
Content added Content deleted
(Added Wren) |
(Added Go) |
||
Line 20: | Line 20: | ||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
Though no need for big integers as we have int64 built in. |
|||
<lang go>package main |
|||
import ( |
|||
"fmt" |
|||
"strconv" |
|||
) |
|||
func toWord(w int64) string { return fmt.Sprintf("W%05d", w) } |
|||
func fromWord(ws string) int64 { |
|||
var u, _ = strconv.ParseUint(ws[1:], 10, 64) |
|||
return int64(u) |
|||
} |
|||
func main() { |
|||
fmt.Println("Starting figures:") |
|||
lat := 28.3852 |
|||
lon := -81.5638 |
|||
fmt.Printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon) |
|||
// convert lat and lon to positive integers |
|||
ilat := int64(lat*10000 + 900000) |
|||
ilon := int64(lon*10000 + 1800000) |
|||
// build 43 bit BigInt comprising 21 bits (lat) and 22 bits (lon) |
|||
latlon := (ilat << 22) + ilon |
|||
// isolate relevant bits |
|||
w1 := (latlon >> 28) & 0xefff |
|||
w2 := (latlon >> 14) & 0x7fff |
|||
w3 := latlon & 0x7fff |
|||
// convert to word format |
|||
w1s := toWord(w1) |
|||
w2s := toWord(w2) |
|||
w3s := toWord(w3) |
|||
// and print the results |
|||
fmt.Println("\nThree word location is:") |
|||
fmt.Printf(" %s %s %s\n", w1s, w2s, w3s) |
|||
/* now reverse the procedure */ |
|||
w1 = fromWord(w1s) |
|||
w2 = fromWord(w2s) |
|||
w3 = fromWord(w3s) |
|||
latlon = (w1 << 28) | (w2 << 14) | w3 |
|||
ilat = latlon >> 22 |
|||
ilon = latlon & 0xfffff |
|||
lat = float64(ilat-900000) / 10000 |
|||
lon = float64(ilon-1800000) / 10000 |
|||
// and print the results |
|||
fmt.Println("\nAfter reversing the procedure:") |
|||
fmt.Printf(" latitude = %0.4f, longitude = %0.4f\n", lat, lon) |
|||
}</lang> |
|||
{{out}} |
|||
<pre> |
|||
Starting figures: |
|||
latitude = 28.3852, longitude = -81.5638 |
|||
Three word location is: |
|||
W18497 W27708 W01322 |
|||
After reversing the procedure: |
|||
latitude = 28.3852, longitude = -81.5638 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |