Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Generate an equivalent Python version of this Erlang code. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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 Erlang to Python, ensuring the logic remains intact. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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 Erlang code into VB without altering its purpose. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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 Erlang version. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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 Erlang to Go, ensuring the logic remains intact. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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 Erlang snippet. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| 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))
}
|
Maintain the same structure and functionality when rewriting this code in C. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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;
}
|
Convert this F# block to C, preserving its control flow and logic. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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;
}
|
Ensure the translated C# code behaves exactly like the original F# snippet. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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));
}
}
}
}
|
Maintain the same structure and functionality when rewriting this code in C#. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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 F# block to C++, preserving its control flow and logic. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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;
}
|
Preserve the algorithm and functionality while converting the code from F# to C++. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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;
}
|
Change the following F# code into Java without altering its purpose. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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 this program into Java but keep the logic exactly as in F#. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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)));
}
}
|
Produce a language-to-language conversion: from F# to Python, same semantics. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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))
|
Port the provided F# code into Python while preserving the original functionality. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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))
|
Write the same algorithm in VB as shown in this F# implementation. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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
|
Produce a language-to-language conversion: from F# to VB, same semantics. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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 algorithm in Go as shown in this F# implementation. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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))
}
|
Convert this F# snippet to Go and keep its semantics consistent. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (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))
}
|
Can you help me rewrite this code in C instead of Factor, keeping it the same logically? | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| #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 Factor code. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| #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;
}
|
Translate the given Factor code snippet into C# without altering its behavior. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 a C# translation of this Factor snippet without changing its computational steps. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Factor code snippet into C++ without altering its behavior. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
|
#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;
}
|
Translate the given Factor code snippet into C++ without altering its behavior. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
|
#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;
}
|
Change the programming language of this snippet from Factor to Java without modifying what it does. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
|
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 Java so it works the same as the original Factor code. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
|
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 Factor code into Python without altering its purpose. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Factor code in Python. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Factor code into VB without altering its purpose. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Factor code. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Go while keeping its functionality equivalent to the Factor version. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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))
}
|
Generate an equivalent Go version of this Factor code. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| 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 Forth. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| #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 Forth block to C, preserving its control flow and logic. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| #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 Forth to C# without modifying what it does. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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 Forth. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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 Forth version. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
|
#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 Forth code. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
|
#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 Forth snippet to Java and keep its semantics consistent. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
|
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 Forth, keeping it the same logically? | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
|
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 Python. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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))
|
Produce a functionally identical Python code for the snippet given in Forth. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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 Forth to VB without modifying what it does. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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
|
Port the provided Forth code into VB while preserving the original functionality. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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
|
Produce a language-to-language conversion: from Forth to Go, same semantics. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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 Forth. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| 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 C#. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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));
}
}
}
}
|
Preserve the algorithm and functionality while converting the code from Fortran to C#. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 Fortran block to C++, preserving its control flow and logic. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 Fortran block to C++, preserving its control flow and logic. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 Fortran function in C with identical behavior. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 the same algorithm in C as shown in this Fortran implementation. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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;
}
|
Rewrite this program in Java while keeping its functionality equivalent to the Fortran version. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 Fortran function in Java with identical behavior. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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)));
}
}
|
Generate an equivalent Python version of this Fortran code. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 a Python translation of this Fortran snippet without changing its computational steps. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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))
|
Change the programming language of this snippet from Fortran to VB without modifying what it does. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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
|
Generate a VB translation of this Fortran snippet without changing its computational steps. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
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 this Fortran block to PHP, preserving its control flow and logic. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the following code from Fortran to PHP with equivalent syntax and logic. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the following Groovy code into C without altering its purpose. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| #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;
}
|
Transform the following Groovy implementation into C, maintaining the same output and logic. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| #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 Groovy to C# without modifying what it does. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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));
}
}
}
}
|
Please provide an equivalent version of this Groovy code in C#. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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 Groovy code. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
|
#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;
}
|
Generate an equivalent C++ version of this Groovy code. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
|
#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;
}
|
Generate a Java translation of this Groovy snippet without changing its computational steps. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
|
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 Groovy. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
|
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 Groovy function in Python with identical behavior. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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 Groovy block to Python, preserving its control flow and logic. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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))
|
Keep all operations the same but rewrite the snippet in VB. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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
|
Port the provided Groovy code into VB while preserving the original functionality. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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 Go translation of this Groovy snippet without changing its computational steps. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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))
}
|
Can you help me rewrite this code in Go instead of Groovy, keeping it the same logically? | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| 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 C. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| #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 Haskell block to C, preserving its control flow and logic. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| #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 the same algorithm in C# as shown in this Haskell implementation. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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 the following code from Haskell to C#, ensuring the logic remains intact. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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));
}
}
}
}
|
Preserve the algorithm and functionality while converting the code from Haskell to C++. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
|
#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;
}
|
Translate this program into C++ but keep the logic exactly as in Haskell. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
|
#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;
}
|
Translate the given Haskell code snippet into Java without altering its behavior. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
|
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)));
}
}
|
Transform the following Haskell implementation into Java, maintaining the same output and logic. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
|
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 Python. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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))
|
Preserve the algorithm and functionality while converting the code from Haskell to Python. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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 VB version of this Haskell code. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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
|
Maintain the same structure and functionality when rewriting this code in VB. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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
|
Produce a language-to-language conversion: from Haskell to Go, same semantics. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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))
}
|
Can you help me rewrite this code in Go instead of Haskell, keeping it the same logically? |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| 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 J version. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| #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;
}
|
Maintain the same structure and functionality when rewriting this code in C. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| #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 J snippet to C# and keep its semantics consistent. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| 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));
}
}
}
}
|
Preserve the algorithm and functionality while converting the code from J to C#. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| 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 code in C++ as shown below in J. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
|
#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;
}
|
Translate this program into C++ but keep the logic exactly as in J. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
|
#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 J to Java. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
|
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 J, keeping it the same logically? | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
|
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)));
}
}
|
Transform the following J implementation into Python, maintaining the same output and logic. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| 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 Python as shown in this J implementation. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| 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))
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.