Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Convert the following code from J to VB, ensuring the logic remains intact. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Generate a VB translation of this J snippet without changing its computational steps. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Convert the following code from J to Go, ensuring the logic remains intact. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Ensure the translated Go code behaves exactly like the original J snippet. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Write a version of this Julia function in C with identical behavior. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Write a version of this Julia function in C with identical behavior. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Preserve the algorithm and functionality while converting the code from Julia to C#. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Ensure the translated C# code behaves exactly like the original Julia snippet. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Translate the given Julia code snippet into C++ without altering its behavior. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Produce a language-to-language conversion: from Julia to C++, same semantics. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Rewrite the snippet below in Java so it works the same as the original Julia code. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Can you help me rewrite this code in Java instead of Julia, keeping it the same logically? | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Can you help me rewrite this code in Python instead of Julia, keeping it the same logically? | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Convert this Julia snippet to Python and keep its semantics consistent. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Change the programming language of this snippet from Julia to VB without modifying what it does. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Generate a VB translation of this Julia snippet without changing its computational steps. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Generate an equivalent Go version of this Julia code. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Produce a functionally identical Go code for the snippet given in Julia. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Rewrite this program in C while keeping its functionality equivalent to the Lua version. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Write a version of this Lua function in C with identical behavior. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Change the following Lua code into C# without altering its purpose. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Produce a functionally identical C# code for the snippet given in Lua. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Generate an equivalent C++ version of this Lua code. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Preserve the algorithm and functionality while converting the code from Lua to C++. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Produce a functionally identical Java code for the snippet given in Lua. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Keep all operations the same but rewrite the snippet in Java. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Convert the following code from Lua to Python, ensuring the logic remains intact. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Convert the following code from Lua to Python, ensuring the logic remains intact. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Rewrite the snippet below in VB so it works the same as the original Lua code. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Change the programming language of this snippet from Lua to VB without modifying what it does. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Please provide an equivalent version of this Lua code in Go. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Write the same code in Go as shown below in Lua. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Rewrite this program in C while keeping its functionality equivalent to the Mathematica version. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Keep all operations the same but rewrite the snippet in C. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Generate an equivalent C# version of this Mathematica code. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Write the same algorithm in C++ as shown in this Mathematica implementation. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Rewrite the snippet below in C++ so it works the same as the original Mathematica code. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Please provide an equivalent version of this Mathematica code in Java. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Port the following code from Mathematica to Java with equivalent syntax and logic. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Change the programming language of this snippet from Mathematica to Python without modifying what it does. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Maintain the same structure and functionality when rewriting this code in Python. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Change the following Mathematica code into VB without altering its purpose. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Change the programming language of this snippet from Mathematica to VB without modifying what it does. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Translate this program into Go but keep the logic exactly as in Mathematica. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Keep all operations the same but rewrite the snippet in Go. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Produce a language-to-language conversion: from MATLAB to C, same semantics. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Change the programming language of this snippet from MATLAB to C without modifying what it does. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Convert this MATLAB snippet to C# and keep its semantics consistent. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Rewrite the snippet below in C# so it works the same as the original MATLAB code. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Rewrite this program in C++ while keeping its functionality equivalent to the MATLAB version. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Convert this MATLAB block to C++, preserving its control flow and logic. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Write a version of this MATLAB function in Java with identical behavior. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Change the following MATLAB code into Java without altering its purpose. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write a version of this MATLAB function in Python with identical behavior. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Generate an equivalent Python version of this MATLAB code. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Port the provided MATLAB code into VB while preserving the original functionality. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Rewrite this program in VB while keeping its functionality equivalent to the MATLAB version. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Convert the following code from MATLAB to Go, ensuring the logic remains intact. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Write the same algorithm in Go as shown in this MATLAB implementation. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Produce a functionally identical C code for the snippet given in Nim. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Ensure the translated C code behaves exactly like the original Nim snippet. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Keep all operations the same but rewrite the snippet in C#. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Translate this program into C# but keep the logic exactly as in Nim. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Produce a language-to-language conversion: from Nim to C++, same semantics. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Transform the following Nim implementation into C++, maintaining the same output and logic. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Convert this Nim snippet to Java and keep its semantics consistent. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Port the provided Nim code into Java while preserving the original functionality. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Preserve the algorithm and functionality while converting the code from Nim to Python. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Please provide an equivalent version of this Nim code in Python. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Write the same code in VB as shown below in Nim. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Convert this Nim snippet to VB and keep its semantics consistent. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Generate an equivalent Go version of this Nim code. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Change the following Nim code into Go without altering its purpose. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Rewrite this program in C while keeping its functionality equivalent to the OCaml version. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Rewrite the snippet below in C so it works the same as the original OCaml code. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Change the programming language of this snippet from OCaml to C# without modifying what it does. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Convert this OCaml snippet to C# and keep its semantics consistent. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Rewrite the snippet below in C++ so it works the same as the original OCaml code. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Write a version of this OCaml function in C++ with identical behavior. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Please provide an equivalent version of this OCaml code in Java. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Translate the given OCaml code snippet into Java without altering its behavior. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Convert the following code from OCaml to Python, ensuring the logic remains intact. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Rewrite the snippet below in Python so it works the same as the original OCaml code. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Rewrite this program in VB while keeping its functionality equivalent to the OCaml version. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Write the same code in VB as shown below in OCaml. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Translate this program into Go but keep the logic exactly as in OCaml. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Produce a language-to-language conversion: from OCaml to Go, same semantics. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Translate this program into C but keep the logic exactly as in Pascal. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Write a version of this Pascal function in C with identical behavior. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Convert this Pascal snippet to C# and keep its semantics consistent. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Convert this Pascal snippet to C# and keep its semantics consistent. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Port the following code from Pascal to C++ with equivalent syntax and logic. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Maintain the same structure and functionality when rewriting this code in C++. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Write the same algorithm in Java as shown in this Pascal implementation. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write the same code in Java as shown below in Pascal. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Rewrite the snippet below in Python so it works the same as the original Pascal code. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Write the same algorithm in VB as shown in this Pascal implementation. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Rewrite the snippet below in VB so it works the same as the original Pascal code. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Translate this program into Go but keep the logic exactly as in Pascal. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.