Instruction
stringlengths
45
106
input_code
stringlengths
1
13.7k
output_code
stringlengths
1
13.7k
Convert the following code from Ada to Go, ensuring the logic remains intact.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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)) }
Port the provided Ada code into Go while preserving the original functionality.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 a Java translation of this Ada snippet without changing its computational steps.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 Ada to Java with equivalent syntax and logic.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 this program in Python while keeping its functionality equivalent to the Ada version.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 Ada code into Python without altering its purpose.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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))
Translate this program into VB but keep the logic exactly as in Ada.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 Ada snippet to VB and keep its semantics consistent.
with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions; procedure Arith_Geom_Mean is type Num is digits 18; package N_IO is new Ada.Text_IO.Float_IO(Num); package Math is new Ada.Numerics.Generic_Elementary_Functions(Num); function AGM(A, G: Num) return Num is Old_G: Num; New_G: Num := G; New_A: Num := A; begin loop Old_G := New_G; New_G := Math.Sqrt(New_A*New_G); New_A := (Old_G + New_A) * 0.5; exit when (New_A - New_G) <= Num'Epsilon; end loop; return New_G; end AGM; begin N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0); end Arith_Geom_Mean;
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 AWK block to C, preserving its control flow and logic.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
#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; }
Generate a C translation of this AWK snippet without changing its computational steps.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
#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#.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK version.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK code.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
#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 AWK to C++ without modifying what it does.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
#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 Java but keep the logic exactly as in AWK.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 functionally identical Java code for the snippet given in AWK.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK to Python, same semantics.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK to VB without modifying what it does.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK block to VB, preserving its control flow and logic.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 AWK code in Go.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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 the snippet below in Go so it works the same as the original AWK code.
BEGIN { printf "%.16g\n", agm(1.0,sqrt(0.5)) } function agm(a,g) { while (1) { a0=a a=(a0+g)/2 g=sqrt(a0*g) if (abs(a0-a) < abs(a)*1e-15) break } return a } function abs(x) { return (x<0 ? -x : x) }
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)) }
Port the provided BBC_Basic code into C while preserving the original functionality.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
#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 the following code from BBC_Basic to C, ensuring the logic remains intact.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
#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 BBC_Basic code into C# without altering its purpose.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic snippet to C# and keep its semantics consistent.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
#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 BBC_Basic to C++.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
#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 code in Java as shown below in BBC_Basic.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic to Java.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic code.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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))
Translate the given BBC_Basic code snippet into Python without altering its behavior.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 a version of this BBC_Basic function in VB with identical behavior.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic to VB, same semantics.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 BBC_Basic version.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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)) }
Port the following code from BBC_Basic to Go with equivalent syntax and logic.
*FLOAT 64 @% = &1010 PRINT FNagm(1, 1/SQR(2)) END DEF FNagm(a,g) LOCAL ta REPEAT ta = a a = (a+g)/2 g = SQR(ta*g) UNTIL a = ta = a
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 the given Common_Lisp code snippet into C without altering its behavior.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
#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 the following code from Common_Lisp to C, ensuring the logic remains intact.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
#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; }
Port the provided Common_Lisp code into C# while preserving the original functionality.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Common_Lisp to C#, same semantics.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Common_Lisp to C++.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
#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 this program in C++ while keeping its functionality equivalent to the Common_Lisp version.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
#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 Common_Lisp block to Java, preserving its control flow and logic.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Common_Lisp code.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Python while keeping its functionality equivalent to the Common_Lisp version.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 VB code for the snippet given in Common_Lisp.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Common_Lisp to VB, ensuring the logic remains intact.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 Common_Lisp code into Go while preserving the original functionality.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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 a Go translation of this Common_Lisp snippet without changing its computational steps.
(ns agmcompute (:gen-class)) (import '(org.apfloat Apfloat ApfloatMath)) (def precision 70) (def one (Apfloat. 1M precision)) (def two (Apfloat. 2M precision)) (def half (Apfloat. 0.5M precision)) (def isqrt2 (.divide one (ApfloatMath/pow two half))) (def TOLERANCE (Apfloat. 0.000000M precision)) (defn agm [a g] " Simple AGM Loop calculation " (let [THRESH 1e-65 MAX-LOOPS 1000000] (loop [[an gn] [a g], cnt 0] (if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH) (> cnt MAX-LOOPS)) an (recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)] (inc cnt)))))) (println (agm one isqrt2))
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.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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; }
Please provide an equivalent version of this D code in C.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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; }
Preserve the algorithm and functionality while converting the code from D to C#.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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)); } } } }
Convert this D block to C#, preserving its control flow and logic.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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)); } } } }
Maintain the same structure and functionality when rewriting this code in C++.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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; }
Port the provided D code into C++ while preserving the original functionality.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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; }
Generate a Java translation of this D snippet without changing its computational steps.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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))); } }
Convert this D snippet to Java and keep its semantics consistent.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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))); } }
Write a version of this D function in Python with identical behavior.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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))
Port the following code from D to Python with equivalent syntax and logic.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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))
Transform the following D implementation into VB, maintaining the same output and logic.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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
Maintain the same structure and functionality when rewriting this code in VB.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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
Maintain the same structure and functionality when rewriting this code in Go.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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)) }
Port the following code from D to Go with equivalent syntax and logic.
import std.stdio, std.math, std.meta, std.typecons; real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe { do { AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g)); } while (feqrel(a, g) < bitPrecision); return a; } void main() @safe { writefln("%0.19f", agm(1, 1 / 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 programming language of this snippet from Delphi to C without modifying what it does.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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; }
Please provide an equivalent version of this Delphi code in C.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 Delphi snippet to C# and keep its semantics consistent.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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)); } } } }
Translate the given Delphi code snippet into C# without altering its behavior.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 provided Delphi code into C++ while preserving the original functionality.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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; }
Can you help me rewrite this code in C++ instead of Delphi, keeping it the same logically?
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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; }
Keep all operations the same but rewrite the snippet in Java.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 Java version of this Delphi code.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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))); } }
Keep all operations the same but rewrite the snippet in Python.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 following Delphi code into Python without altering its purpose.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 following code from Delphi to VB with equivalent syntax and logic.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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
Produce a language-to-language conversion: from Delphi to VB, same semantics.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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
Keep all operations the same but rewrite the snippet in Go.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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 Go code for the snippet given in Delphi.
program geometric_mean; uses System.SysUtils; function Fabs(value: Double): Double; begin Result := value; if Result < 0 then Result := -Result; end; function agm(a, g: Double):Double; var iota, a1, g1: Double; begin iota := 1.0E-16; if a * g < 0.0 then begin Writeln('arithmetic-geometric mean undefined when x*y<0'); exit(1); end; while Fabs(a - g) > iota do begin a1 := (a + g) / 2.0; g1 := sqrt(a * g); a := a1; g := g1; end; Exit(a); end; var x, y: Double; begin Write('Enter two numbers:'); Readln(x, y); writeln(format('The arithmetic-geometric mean is  %.6f', [agm(x, y)])); readln; 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)) }
Change the following Elixir code into C without altering its purpose.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
#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.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
#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 Elixir code into C# without altering its purpose.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir version.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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)); } } } }
Change the following Elixir code into C++ without altering its purpose.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
#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 Elixir implementation into C++, maintaining the same output and logic.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
#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 Elixir code.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir, keeping it the same logically?
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir code in Python.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir version.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir to VB without modifying what it does.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir code into Go while preserving the original functionality.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 Elixir code.
defmodule ArithhGeom do def mean(a,g,tol) when abs(a-g) <= tol, do: a def mean(a,g,tol) do mean((a+g)/2,:math.pow(a*g, 0.5),tol) end end IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
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 the following code from Erlang to C, 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).
#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; }
Generate a C translation of this Erlang snippet without changing its computational steps.
-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).
#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; }
Produce a functionally identical C# code for the snippet given in Erlang.
-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).
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 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).
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 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).
#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 Erlang snippet to C++ and keep its semantics consistent.
-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).
#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 Erlang code in Java.
-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).
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 Erlang, keeping it the same logically?
-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).
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))); } }