Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Ensure the translated Java code behaves exactly like the original OCaml snippet.
# let li = ["big"; "fjords"; "vex"; "quick"; "waltz"; "nymph"] in List.iter print_endline li ;; big fjords vex quick waltz nymph - : unit = ()
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Convert this OCaml snippet to Python and keep its semantics consistent.
# let li = ["big"; "fjords"; "vex"; "quick"; "waltz"; "nymph"] in List.iter print_endline li ;; big fjords vex quick waltz nymph - : unit = ()
for node in lst: print node.value
Transform the following OCaml implementation into Go, maintaining the same output and logic.
# let li = ["big"; "fjords"; "vex"; "quick"; "waltz"; "nymph"] in List.iter print_endline li ;; big fjords vex quick waltz nymph - : unit = ()
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Preserve the algorithm and functionality while converting the code from Perl to C.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Port the following code from Perl to C# with equivalent syntax and logic.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Convert this Perl block to C++, preserving its control flow and logic.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Preserve the algorithm and functionality while converting the code from Perl to Java.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Change the programming language of this snippet from Perl to Python without modifying what it does.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
for node in lst: print node.value
Rewrite this program in VB while keeping its functionality equivalent to the Perl version.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Change the following Perl code into Go without altering its purpose.
package SSL_Node; use strict; use Class::Tiny qw( val next ); sub BUILD { my $self = shift; exists($self->{val}) or die "Must supply 'val'"; if (exists $self->{next}) { ref($self->{next}) eq 'SSL_Node' or die "If supplied, 'next' must be an SSL_Node"; } return; } package main; use strict; my @vals = 1 .. 10; my $countdown = SSL_Node->new(val => shift(@vals)); while (@vals) { my $head = SSL_Node->new(val => shift(@vals), next => $countdown); $countdown = $head; } my $node = $countdown; while ($node) { print $node->val, "... "; $node = $node->next; } print "\n";
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Write a version of this Racket function in C with identical behavior.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Produce a functionally identical C# code for the snippet given in Racket.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Preserve the algorithm and functionality while converting the code from Racket to C++.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Produce a functionally identical Java code for the snippet given in Racket.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Rewrite this program in Python while keeping its functionality equivalent to the Racket version.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
for node in lst: print node.value
Port the provided Racket code into VB while preserving the original functionality.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Maintain the same structure and functionality when rewriting this code in Go.
#lang racket (define l (list 1 2 3)) (map add1 l) (for-each displayln l) (foldl + 0 l) (for/list ([x (in-list l)]) (add1 x)) (for ([x (in-list l)]) (displayln x)) (for/fold ([sum 0]) ([x (in-list l)]) (+ x sum)) (for/vector ([x (in-list l)]) (add1 x)) (require racket/mpair) (define ml (mlist 1 2 3)) (mmap add1 ml) (mfor-each displayln ml) (for ([x (in-mlist ml)]) (displayln x))
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Translate this program into C but keep the logic exactly as in REXX.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Produce a functionally identical C# code for the snippet given in REXX.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Change the following REXX code into C++ without altering its purpose.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Generate a Java translation of this REXX snippet without changing its computational steps.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Produce a language-to-language conversion: from REXX to Python, same semantics.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
for node in lst: print node.value
Port the following code from REXX to VB with equivalent syntax and logic.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Produce a functionally identical Go code for the snippet given in REXX.
list=.list~of('A','B','X') say "Manual list traversal" index=list~first loop while index \== .nil say list~at(index) index = list~next(index) end say say "Do ... Over traversal" do value over list say value end
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Produce a functionally identical C code for the snippet given in Ruby.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Port the provided Ruby code into C# while preserving the original functionality.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Convert this Ruby snippet to C++ and keep its semantics consistent.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Generate an equivalent Java version of this Ruby code.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Translate the given Ruby code snippet into Python without altering its behavior.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
for node in lst: print node.value
Port the following code from Ruby to VB with equivalent syntax and logic.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Port the provided Ruby code into Go while preserving the original functionality.
head = ListNode.new("a", ListNode.new("b", ListNode.new("c"))) head.insertAfter("b", "b+") head.each {|node| print node.value, ","} puts current = head begin print current.value, "," end while current = current.succ puts
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Translate this program into C but keep the logic exactly as in Scala.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Can you help me rewrite this code in C# instead of Scala, keeping it the same logically?
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Convert this Scala snippet to C++ and keep its semantics consistent.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Produce a functionally identical Java code for the snippet given in Scala.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Please provide an equivalent version of this Scala code in Python.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
for node in lst: print node.value
Translate this program into VB but keep the logic exactly as in Scala.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Please provide an equivalent version of this Scala code in Go.
fun main(args: Array<String>) { val list = IntRange(1, 50).toList() for (i in list) { print("%4d ".format(i)); if (i % 10 == 0) println() } list.asReversed().forEach { print("%4d ".format(it)); if (it % 10 == 1) println() } }
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Can you help me rewrite this code in C instead of Tcl, keeping it the same logically?
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
Change the following Tcl code into C# without altering its purpose.
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
var current = [head of list to traverse] while(current != null) { current = current.Next; }
Change the programming language of this snippet from Tcl to C++ without modifying what it does.
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
Can you help me rewrite this code in Java instead of Tcl, keeping it the same logically?
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
Translate this program into Python but keep the logic exactly as in Tcl.
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
for node in lst: print node.value
Change the programming language of this snippet from Tcl to VB without modifying what it does.
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Write the same algorithm in Go as shown in this Tcl implementation.
oo::define List { method for {varName script} { upvar 1 $varName var set elem [self] while {$elem ne ""} { set var [$elem value] uplevel 1 $script set elem [$elem next] } } }
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
Write the same code in Rust as shown below in C.
struct link *first; struct link *iter; for(iter = first; iter != NULL; iter = iter->next) { }
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Convert the following code from C# to Rust, ensuring the logic remains intact.
var current = [head of list to traverse] while(current != null) { current = current.Next; }
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Write the same code in Rust as shown below in Java.
LinkedList<Type> list = new LinkedList<Type>(); for(Type i: list){ System.out.println(i); }
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Produce a language-to-language conversion: from Rust to VB, same semantics.
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Private Sub Iterate(ByVal list As LinkedList(Of Integer)) Dim node = list.First Do Until node Is Nothing node = node.Next Loop End Sub
Preserve the algorithm and functionality while converting the code from Go to Rust.
start := &Ele{"tacos", nil} end := start.Append("burritos") end = end.Append("fajitas") end = end.Append("enchilatas") for iter := start; iter != nil; iter = iter.Next { fmt.Println(iter) }
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Change the following C++ code into Rust without altering its purpose.
#include <iostream> #include <forward_list> int main() { std::forward_list<int> list{1, 2, 3, 4, 5}; for (int e : list) std::cout << e << std::endl; }
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
Ensure the translated Python code behaves exactly like the original Rust snippet.
pub struct IntoIter<T>(List<T>); impl<T> Iterator for IntoIter<T> { type Item = T; fn next(&mut self) -> Option<Self::Item> { self.0.head.take().map(|node| { let node = *node; self.0.head = node.next; node.elem }) } } pub struct Iter<'a, T: 'a> { next: Option<&'a Node<T>>, } impl<'a, T> Iterator for Iter<'a, T> { type Item = &'a T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_ref().map(|node| &**node); &node.elem }) } } pub struct IterMut<'a, T: 'a> { next: Option<&'a mut Node<T>>, } impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.next.take().map(|node| { self.next = node.next.as_mut().map(|node| &mut **node); &mut node.elem }) } } impl<T> List<T> { pub fn into_iter(self) -> IntoIter<T> { IntoIter(self) } pub fn iter<'a>(&'a self) -> Iter<'a,T> { Iter { next: self.head.as_ref().map(|node| &**node) } } pub fn iter_mut(&mut self) -> IterMut<T> { IterMut { next: self.head.as_mut().map(|node| &mut **node) } } }
for node in lst: print node.value
Convert the following code from Ada to C#, ensuring the logic remains intact.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Ensure the translated C# code behaves exactly like the original Ada snippet.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert the following code from Ada to C, ensuring the logic remains intact.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Please provide an equivalent version of this Ada code in C.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Generate a C++ translation of this Ada snippet without changing its computational steps.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Convert this Ada block to C++, preserving its control flow and logic.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Can you help me rewrite this code in Go instead of Ada, keeping it the same logically?
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Can you help me rewrite this code in Go instead of Ada, keeping it the same logically?
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Produce a language-to-language conversion: from Ada to Java, same semantics.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Can you help me rewrite this code in Java instead of Ada, keeping it the same logically?
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Change the following Ada code into Python without altering its purpose.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Write the same algorithm in Python as shown in this Ada implementation.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Generate an equivalent VB version of this Ada code.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Convert this Ada block to VB, preserving its control flow and logic.
with Ada.Text_IO; use Ada.Text_IO; with Ada.Long_Float_Text_IO; use Ada.Long_Float_Text_IO; with Ada.Numerics.Generic_Elementary_Functions; procedure Haversine_Formula is package Math is new Ada.Numerics.Generic_Elementary_Functions (Long_Float); use Math; function Great_Circle_Distance (lat1, long1, lat2, long2 : Long_Float) return Long_Float is Earth_Radius : constant := 6371.0; a : Long_Float := Sin (0.5 * (lat2 - lat1)); b : Long_Float := Sin (0.5 * (long2 - long1)); begin return 2.0 * Earth_Radius * ArcSin (Sqrt (a * a + Cos (lat1) * Cos (lat2) * b * b)); end Great_Circle_Distance; function DMS_To_Radians (Deg, Min, Sec : Long_Float := 0.0) return Long_Float is Pi_Over_180 : constant := 0.017453_292519_943295_769236_907684_886127; begin return (Deg + Min/60.0 + Sec/3600.0) * Pi_Over_180; end DMS_To_Radians; begin Put_Line("Distance in kilometers between BNA and LAX"); Put (Great_Circle_Distance ( DMS_To_Radians (36.0, 7.2), DMS_To_Radians (86.0, 40.2), DMS_To_Radians (33.0, 56.4), DMS_To_Radians (118.0, 24.0)), Aft=>3, Exp=>0); end Haversine_Formula;
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Please provide an equivalent version of this Arturo code in C.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Port the following code from Arturo to C with equivalent syntax and logic.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Port the following code from Arturo to C# with equivalent syntax and logic.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Generate a C# translation of this Arturo snippet without changing its computational steps.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Port the provided Arturo code into C++ while preserving the original functionality.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Keep all operations the same but rewrite the snippet in C++.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Write the same code in Java as shown below in Arturo.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Transform the following Arturo implementation into Java, maintaining the same output and logic.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Produce a functionally identical Python code for the snippet given in Arturo.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Keep all operations the same but rewrite the snippet in Python.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Preserve the algorithm and functionality while converting the code from Arturo to VB.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Maintain the same structure and functionality when rewriting this code in VB.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Rewrite this program in Go while keeping its functionality equivalent to the Arturo version.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Convert the following code from Arturo to Go, ensuring the logic remains intact.
radians: function [x]-> x * pi // 180 haversine: function [src,tgt][ dLat: radians tgt\0 - src\0 dLon: radians tgt\1 - src\1 lat1: radians src\0 lat2: radians tgt\0 a: add product @[cos lat1, cos lat2, sin dLon/2, sin dLon/2] (sin dLat/2) ^ 2 c: 2 * asin sqrt a return 6372.8 * c ] print haversine @[36.12 neg 86.67] @[33.94, neg 118.40]
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Change the programming language of this snippet from AutoHotKey to C without modifying what it does.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Transform the following AutoHotKey implementation into C, maintaining the same output and logic.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Write a version of this AutoHotKey function in C# with identical behavior.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert this AutoHotKey snippet to C# and keep its semantics consistent.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Keep all operations the same but rewrite the snippet in C++.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Produce a language-to-language conversion: from AutoHotKey to C++, same semantics.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Translate the given AutoHotKey code snippet into Java without altering its behavior.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Maintain the same structure and functionality when rewriting this code in Java.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
public class Haversine { public static final double R = 6372.8; public static double haversine(double lat1, double lon1, double lat2, double lon2) { lat1 = Math.toRadians(lat1); lat2 = Math.toRadians(lat2); double dLat = lat2 - lat1; double dLon = Math.toRadians(lon2 - lon1); double a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2); double c = 2 * Math.asin(Math.sqrt(a)); return R * c; } public static void main(String[] args) { System.out.println(haversine(36.12, -86.67, 33.94, -118.40)); } }
Translate the given AutoHotKey code snippet into Python without altering its behavior.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Ensure the translated Python code behaves exactly like the original AutoHotKey snippet.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
from math import radians, sin, cos, sqrt, asin def haversine(lat1, lon1, lat2, lon2): R = 6372.8 dLat = radians(lat2 - lat1) dLon = radians(lon2 - lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = sin(dLat / 2)**2 + cos(lat1) * cos(lat2) * sin(dLon / 2)**2 c = 2 * asin(sqrt(a)) return R * c >>> haversine(36.12, -86.67, 33.94, -118.40) 2887.2599506071106 >>>
Convert this AutoHotKey snippet to VB and keep its semantics consistent.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Transform the following AutoHotKey implementation into VB, maintaining the same output and logic.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
Const MER = 6371 Public DEG_TO_RAD As Double Function haversine(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double lat1 = lat1 * DEG_TO_RAD lat2 = lat2 * DEG_TO_RAD long1 = long1 * DEG_TO_RAD long2 = long2 * DEG_TO_RAD haversine = MER * WorksheetFunction.Acos(Sin(lat1) * Sin(lat2) + Cos(lat1) * Cos(lat2) * Cos(long2 - long1)) End Function Public Sub main() DEG_TO_RAD = WorksheetFunction.Pi / 180 d = haversine(36.12, -86.67, 33.94, -118.4) Debug.Print "Distance is "; Format(d, "#.######"); " km ("; Format(d / 1.609344, "#.######"); " miles)." End Sub
Ensure the translated Go code behaves exactly like the original AutoHotKey snippet.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Maintain the same structure and functionality when rewriting this code in Go.
MsgBox, % GreatCircleDist(36.12, 33.94, -86.67, -118.40, 6372.8, "km") GreatCircleDist(La1, La2, Lo1, Lo2, R, U) { return, 2 * R * ASin(Sqrt(Hs(Rad(La2 - La1)) + Cos(Rad(La1)) * Cos(Rad(La2)) * Hs(Rad(Lo2 - Lo1)))) A_Space U } Hs(n) { return, (1 - Cos(n)) / 2 } Rad(Deg) { return, Deg * 4 * ATan(1) / 180 }
package main import ( "fmt" "math" ) func haversine(θ float64) float64 { return .5 * (1 - math.Cos(θ)) } type pos struct { φ float64 ψ float64 } func degPos(lat, lon float64) pos { return pos{lat * math.Pi / 180, lon * math.Pi / 180} } const rEarth = 6372.8 func hsDist(p1, p2 pos) float64 { return 2 * rEarth * math.Asin(math.Sqrt(haversine(p2.φ-p1.φ)+ math.Cos(p1.φ)*math.Cos(p2.φ)*haversine(p2.ψ-p1.ψ))) } func main() { fmt.Println(hsDist(degPos(36.12, -86.67), degPos(33.94, -118.40))) }
Convert this AWK block to C, preserving its control flow and logic.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Translate this program into C but keep the logic exactly as in AWK.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
#include <stdio.h> #include <stdlib.h> #include <math.h> #define R 6371 #define TO_RAD (3.1415926536 / 180) double dist(double th1, double ph1, double th2, double ph2) { double dx, dy, dz; ph1 -= ph2; ph1 *= TO_RAD, th1 *= TO_RAD, th2 *= TO_RAD; dz = sin(th1) - sin(th2); dx = cos(ph1) * cos(th1) - cos(th2); dy = sin(ph1) * cos(th1); return asin(sqrt(dx * dx + dy * dy + dz * dz) / 2) * 2 * R; } int main() { double d = dist(36.12, -86.67, 33.94, -118.4); printf("dist: %.1f km (%.1f mi.)\n", d, d / 1.609344); return 0; }
Generate a C# translation of this AWK snippet without changing its computational steps.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Convert this AWK block to C#, preserving its control flow and logic.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
public static class Haversine { public static double calculate(double lat1, double lon1, double lat2, double lon2) { var R = 6372.8; var dLat = toRadians(lat2 - lat1); var dLon = toRadians(lon2 - lon1); lat1 = toRadians(lat1); lat2 = toRadians(lat2); var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) + Math.Sin(dLon / 2) * Math.Sin(dLon / 2) * Math.Cos(lat1) * Math.Cos(lat2); var c = 2 * Math.Asin(Math.Sqrt(a)); return R * 2 * Math.Asin(Math.Sqrt(a)); } public static double toRadians(double angle) { return Math.PI * angle / 180.0; } } void Main() { Console.WriteLine(String.Format("The distance between coordinates {0},{1} and {2},{3} is: {4}", 36.12, -86.67, 33.94, -118.40, Haversine.calculate(36.12, -86.67, 33.94, -118.40))); }
Produce a language-to-language conversion: from AWK to C++, same semantics.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }
Generate a C++ translation of this AWK snippet without changing its computational steps.
BEGIN { distance(36.12,-86.67,33.94,-118.40) exit(0) } function distance(lat1,lon1,lat2,lon2, a,c,dlat,dlon) { dlat = radians(lat2-lat1) dlon = radians(lon2-lon1) lat1 = radians(lat1) lat2 = radians(lat2) a = (sin(dlat/2))^2 + cos(lat1) * cos(lat2) * (sin(dlon/2))^2 c = 2 * atan2(sqrt(a),sqrt(1-a)) printf("distance: %.4f km\n",6372.8 * c) } function radians(degree) { return degree * (3.1415926 / 180.) }
#define _USE_MATH_DEFINES #include <math.h> #include <iostream> const static double EarthRadiusKm = 6372.8; inline double DegreeToRadian(double angle) { return M_PI * angle / 180.0; } class Coordinate { public: Coordinate(double latitude ,double longitude):myLatitude(latitude), myLongitude(longitude) {} double Latitude() const { return myLatitude; } double Longitude() const { return myLongitude; } private: double myLatitude; double myLongitude; }; double HaversineDistance(const Coordinate& p1, const Coordinate& p2) { double latRad1 = DegreeToRadian(p1.Latitude()); double latRad2 = DegreeToRadian(p2.Latitude()); double lonRad1 = DegreeToRadian(p1.Longitude()); double lonRad2 = DegreeToRadian(p2.Longitude()); double diffLa = latRad2 - latRad1; double doffLo = lonRad2 - lonRad1; double computation = asin(sqrt(sin(diffLa / 2) * sin(diffLa / 2) + cos(latRad1) * cos(latRad2) * sin(doffLo / 2) * sin(doffLo / 2))); return 2 * EarthRadiusKm * computation; } int main() { Coordinate c1(36.12, -86.67); Coordinate c2(33.94, -118.4); std::cout << "Distance = " << HaversineDistance(c1, c2) << std::endl; return 0; }