Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Transform the following Pascal implementation into Go, maintaining the same output and logic. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| 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 Perl code into C without altering its purpose. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Keep all operations the same but rewrite the snippet in C. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Transform the following Perl implementation into C#, maintaining the same output and logic. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| 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 Perl. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Generate a C++ translation of this Perl snippet without changing its computational steps. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Produce a functionally identical C++ code for the snippet given in Perl. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Change the following Perl code into Java without altering its purpose. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Port the provided Perl code into Java while preserving the original functionality. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
|
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 Python code for the snippet given in Perl. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Convert this Perl block to Python, preserving its control flow and logic. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| 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 Perl. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Translate the given Perl code snippet into VB without altering its behavior. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Generate an equivalent Go version of this Perl code. |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Can you help me rewrite this code in Go instead of Perl, keeping it the same logically? |
my ($a0, $g0, $a1, $g1);
sub agm($$) {
$a0 = shift;
$g0 = shift;
do {
$a1 = ($a0 + $g0)/2;
$g1 = sqrt($a0 * $g0);
$a0 = ($a1 + $g1)/2;
$g0 = sqrt($a1 * $g1);
} while ($a0 != $a1);
return $a0;
}
print agm(1, 1/sqrt(2))."\n";
| 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))
}
|
Preserve the algorithm and functionality while converting the code from PowerShell to C. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Can you help me rewrite this code in C instead of PowerShell, keeping it the same logically? | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Transform the following PowerShell implementation into C#, maintaining the same output and logic. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Generate a C# translation of this PowerShell snippet without changing its computational steps. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Convert this PowerShell block to C++, preserving its control flow and logic. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Rewrite the snippet below in C++ so it works the same as the original PowerShell code. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Write a version of this PowerShell function in Java with identical behavior. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Change the programming language of this snippet from PowerShell to Java without modifying what it does. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Convert the following code from PowerShell to Python, ensuring the logic remains intact. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Preserve the algorithm and functionality while converting the code from PowerShell to Python. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Can you help me rewrite this code in VB instead of PowerShell, keeping it the same logically? | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Convert the following code from PowerShell to VB, ensuring the logic remains intact. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Change the following PowerShell code into Go without altering its purpose. | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Can you help me rewrite this code in Go instead of PowerShell, keeping it the same logically? | function agm ([Double]$a, [Double]$g) {
[Double]$eps = 1E-15
[Double]$a1 = [Double]$g1 = 0
while([Math]::Abs($a - $g) -gt $eps) {
$a1, $g1 = $a, $g
$a = ($a1 + $g1)/2
$g = [Math]::Sqrt($a1*$g1)
}
[pscustomobject]@{
a = "$a"
g = "$g"
}
}
agm 1 (1/[Math]::Sqrt(2))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Rewrite the snippet below in C so it works the same as the original R code. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Keep all operations the same but rewrite the snippet in C. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Write the same algorithm in C# as shown in this R implementation. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R code. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R code into C++ while preserving the original functionality. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
|
#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 the following code from R to C++, ensuring the logic remains intact. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
|
#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 following code from R to Java with equivalent syntax and logic. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
|
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 R to Java, same semantics. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write the same code in Python as shown below in R. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R version. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R to VB with equivalent syntax and logic. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R to VB without modifying what it does. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Rewrite the snippet below in Go so it works the same as the original R code. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 R to Go with equivalent syntax and logic. | arithmeticMean <- function(a, b) { (a + b)/2 }
geometricMean <- function(a, b) { sqrt(a * b) }
arithmeticGeometricMean <- function(a, b) {
rel_error <- abs(a - b) / pmax(a, b)
if (all(rel_error < .Machine$double.eps, na.rm=TRUE)) {
agm <- a
return(data.frame(agm, rel_error));
}
Recall(arithmeticMean(a, b), geometricMean(a, b))
}
agm <- arithmeticGeometricMean(1, 1/sqrt(2))
print(format(agm, digits=16))
| 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 Racket to C without modifying what it does. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Write a version of this Racket function in C with identical behavior. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Translate the given Racket code snippet into C# without altering its behavior. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Convert this Racket block to C#, preserving its control flow and logic. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Rewrite the snippet below in C++ so it works the same as the original Racket code. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Transform the following Racket implementation into C++, maintaining the same output and logic. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Please provide an equivalent version of this Racket code in Java. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Rewrite this program in Java while keeping its functionality equivalent to the Racket version. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Generate a Python translation of this Racket snippet without changing its computational steps. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Ensure the translated Python code behaves exactly like the original Racket snippet. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Keep all operations the same but rewrite the snippet in VB. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Preserve the algorithm and functionality while converting the code from Racket to VB. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Convert this Racket block to Go, preserving its control flow and logic. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Please provide an equivalent version of this Racket code in Go. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Can you help me rewrite this code in C instead of COBOL, keeping it the same logically? | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| #include<math.h>
#include<stdio.h>
#include<stdlib.h>
double agm( double a, double g ) {
double iota = 1.0E-16;
double a1, g1;
if( a*g < 0.0 ) {
printf( "arithmetic-geometric mean undefined when x*y<0\n" );
exit(1);
}
while( fabs(a-g)>iota ) {
a1 = (a + g) / 2.0;
g1 = sqrt(a * g);
a = a1;
g = g1;
}
return a;
}
int main( void ) {
double x, y;
printf( "Enter two numbers: " );
scanf( "%lf%lf", &x, &y );
printf( "The arithmetic-geometric mean is %lf\n", agm(x, y) );
return 0;
}
|
Change the programming language of this snippet from COBOL to C without modifying what it does. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| #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 COBOL. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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 COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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 a version of this COBOL function in C++ with identical behavior. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
|
#include "gmp.h"
void agm (const mpf_t in1, const mpf_t in2, mpf_t out1, mpf_t out2) {
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
}
int main (void) {
mpf_set_default_prec (65568);
mpf_t x0, y0, resA, resB;
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for(int i=0; i<7; i++){
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
}
gmp_printf ("%.20000Ff\n", x0);
gmp_printf ("%.20000Ff\n\n", y0);
return 0;
}
|
Rewrite the snippet below in C++ so it works the same as the original COBOL code. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
|
#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 COBOL. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write the same algorithm in Java as shown in this COBOL implementation. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
|
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 Python but keep the logic exactly as in COBOL. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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))
|
Ensure the translated Python code behaves exactly like the original COBOL snippet. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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))
|
Ensure the translated VB code behaves exactly like the original COBOL snippet. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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
|
Preserve the algorithm and functionality while converting the code from COBOL to VB. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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 COBOL block to Go, preserving its control flow and logic. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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 COBOL to Go, ensuring the logic remains intact. | IDENTIFICATION DIVISION.
PROGRAM-ID. ARITHMETIC-GEOMETRIC-MEAN-PROG.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 AGM-VARS.
05 A PIC 9V9(16).
05 A-ZERO PIC 9V9(16).
05 G PIC 9V9(16).
05 DIFF PIC 9V9(16) VALUE 1.
* Initialize DIFF with a non-zero value, otherwise AGM-PARAGRAPH
* is never performed at all.
PROCEDURE DIVISION.
TEST-PARAGRAPH.
MOVE 1 TO A.
COMPUTE G = 1 / FUNCTION SQRT(2).
* The program will run with the test values. If you would rather
* calculate the AGM of numbers input at the console, comment out
* TEST-PARAGRAPH and un-comment-out INPUT-A-AND-G-PARAGRAPH.
* INPUT-A-AND-G-PARAGRAPH.
* DISPLAY 'Enter two numbers.'
* ACCEPT A.
* ACCEPT G.
CONTROL-PARAGRAPH.
PERFORM AGM-PARAGRAPH UNTIL DIFF IS LESS THAN 0.000000000000001.
DISPLAY A.
STOP RUN.
AGM-PARAGRAPH.
MOVE A TO A-ZERO.
COMPUTE A = (A-ZERO + G) / 2.
MULTIPLY A-ZERO BY G GIVING G.
COMPUTE G = FUNCTION SQRT(G).
SUBTRACT A FROM G GIVING DIFF.
COMPUTE DIFF = FUNCTION ABS(DIFF).
| 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))
}
|
Please provide an equivalent version of this REXX code in C. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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 REXX to C. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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;
}
|
Convert the following code from REXX to C#, ensuring the logic remains intact. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 0
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Produce a language-to-language conversion: from REXX to C#, same semantics. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 0
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Produce a language-to-language conversion: from REXX to C++, same semantics. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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;
}
|
Translate this program into C++ but keep the logic exactly as in REXX. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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;
}
|
Keep all operations the same but rewrite the snippet in Java. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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)));
}
}
|
Please provide an equivalent version of this REXX code in Java. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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)));
}
}
|
Change the programming language of this snippet from REXX to Python without modifying what it does. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 0
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Write the same code in Python as shown below in REXX. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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))
|
Maintain the same structure and functionality when rewriting this code in VB. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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
|
Produce a functionally identical VB code for the snippet given in REXX. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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
|
Ensure the translated Go code behaves exactly like the original REXX snippet. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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 REXX to Go with equivalent syntax and logic. |
options replace format comments java crossref symbols nobinary
numeric digits 18
parse arg a_ g_ .
if a_ = '' | a_ = '.' then a0 = 1
else a0 = a_
if g_ = '' | g_ = '.' then g0 = 1 / Math.sqrt(2)
else g0 = g_
say agm(a0, g0)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method agm(a0, g0) public static returns Rexx
a1 = a0
g1 = g0
loop while (a1 - g1).abs() >= Math.pow(10, -14)
temp = (a1 + g1) / 2
g1 = Math.sqrt(a1 * g1)
a1 = temp
end
return a1 + 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))
}
|
Convert the following code from Ruby to C, ensuring the logic remains intact. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| #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. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| #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;
}
|
Can you help me rewrite this code in C# instead of Ruby, keeping it the same logically? |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| 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 Ruby to C#, same semantics. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| 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 Ruby to C++. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
|
#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 Ruby to C++. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
|
#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. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Convert the following code from Ruby to Java, ensuring the logic remains intact. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
|
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 Ruby code. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Produce a functionally identical Python code for the snippet given in Ruby. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Write the same algorithm in VB as shown in this Ruby implementation. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| 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
|
Transform the following Ruby implementation into VB, maintaining the same output and logic. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| 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. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Produce a language-to-language conversion: from Ruby to Go, same semantics. |
require 'flt'
include Flt
BinNum.Context.precision = 512
def agm(a,g)
new_a = BinNum a
new_g = BinNum g
while new_a - new_g > new_a.class.Context.epsilon do
old_g = new_g
new_g = (new_a * new_g).sqrt
new_a = (old_g + new_a) * 0.5
end
new_g
end
puts agm(1, 1 / BinNum(2).sqrt)
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Convert this Scala snippet to C and keep its semantics consistent. |
fun agm(a: Double, g: Double): Double {
var aa = a
var gg = g
var ta: Double
val epsilon = 1.0e-16
while (true) {
ta = (aa + gg) / 2.0
if (Math.abs(aa - ta) <= epsilon) return ta
gg = Math.sqrt(aa * gg)
aa = ta
}
}
fun main(args: Array<String>) {
println(agm(1.0, 1.0 / Math.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;
}
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.