Instruction stringlengths 45 106 | input_code stringlengths 1 13.7k | output_code stringlengths 1 13.7k |
|---|---|---|
Rewrite this program in C while keeping its functionality equivalent to the Scala version. |
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;
}
|
Port the following code from Scala to C# with equivalent syntax and logic. |
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)))
}
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Change the programming language of this snippet from Scala to C# without modifying what it does. |
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)))
}
| namespace RosettaCode.ArithmeticGeometricMean
{
using System;
using System.Collections.Generic;
using System.Globalization;
internal static class Program
{
private static double ArithmeticGeometricMean(double number,
double otherNumber,
IEqualityComparer<double>
comparer)
{
return comparer.Equals(number, otherNumber)
? number
: ArithmeticGeometricMean(
ArithmeticMean(number, otherNumber),
GeometricMean(number, otherNumber), comparer);
}
private static double ArithmeticMean(double number, double otherNumber)
{
return 0.5 * (number + otherNumber);
}
private static double GeometricMean(double number, double otherNumber)
{
return Math.Sqrt(number * otherNumber);
}
private static void Main()
{
Console.WriteLine(
ArithmeticGeometricMean(1, 0.5 * Math.Sqrt(2),
new RelativeDifferenceComparer(1e-5)).
ToString(CultureInfo.InvariantCulture));
}
private class RelativeDifferenceComparer : IEqualityComparer<double>
{
private readonly double _maximumRelativeDifference;
internal RelativeDifferenceComparer(double maximumRelativeDifference)
{
_maximumRelativeDifference = maximumRelativeDifference;
}
public bool Equals(double number, double otherNumber)
{
return RelativeDifference(number, otherNumber) <=
_maximumRelativeDifference;
}
public int GetHashCode(double number)
{
return number.GetHashCode();
}
private static double RelativeDifference(double number,
double otherNumber)
{
return AbsoluteDifference(number, otherNumber) /
Norm(number, otherNumber);
}
private static double AbsoluteDifference(double number,
double otherNumber)
{
return Math.Abs(number - otherNumber);
}
private static double Norm(double number, double otherNumber)
{
return 0.5 * (Math.Abs(number) + Math.Abs(otherNumber));
}
}
}
}
|
Translate the given Scala code snippet into C++ without altering its behavior. |
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 "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 Scala code in C++. |
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 "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 Scala. |
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)))
}
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write the same code in Java as shown below in Scala. |
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)))
}
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Port the following code from Scala to Python with equivalent syntax and logic. |
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)))
}
| 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 Scala. |
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)))
}
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Change the programming language of this snippet from Scala to VB without modifying what it does. |
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)))
}
| Private Function agm(a As Double, g As Double, Optional tolerance As Double = 0.000000000000001) As Double
Do While Abs(a - g) > tolerance
tmp = a
a = (a + g) / 2
g = Sqr(tmp * g)
Debug.Print a
Loop
agm = a
End Function
Public Sub main()
Debug.Print agm(1, 1 / Sqr(2))
End Sub
|
Translate this program into VB but keep the logic exactly as in Scala. |
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)))
}
| 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 Scala snippet to Go 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)))
}
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Port the provided Scala code into Go while preserving the original functionality. |
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)))
}
| 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 Swift, keeping it the same logically? | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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 this Swift snippet to C and keep its semantics consistent. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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;
}
|
Port the following code from Swift to C# with equivalent syntax and logic. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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 Swift to C#, same semantics. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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));
}
}
}
}
|
Change the following Swift code into C++ without altering its purpose. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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;
}
|
Write the same algorithm in C++ as shown in this Swift implementation. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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;
}
|
Write the same algorithm in Java as shown in this Swift implementation. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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)));
}
}
|
Translate this program into Java but keep the logic exactly as in Swift. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 0")
}
|
public class ArithmeticGeometricMean {
public static double agm(double a, double g) {
double a1 = a;
double g1 = g;
while (Math.abs(a1 - g1) >= 1.0e-14) {
double arith = (a1 + g1) / 2.0;
double geom = Math.sqrt(a1 * g1);
a1 = arith;
g1 = geom;
}
return a1;
}
public static void main(String[] args) {
System.out.println(agm(1.0, 1.0 / Math.sqrt(2.0)));
}
}
|
Write the same algorithm in Python as shown in this Swift implementation. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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))
|
Change the following Swift code into Python without altering its purpose. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 0")
}
| from math import sqrt
def agm(a0, g0, tolerance=1e-10):
an, gn = (a0 + g0) / 2.0, sqrt(a0 * g0)
while abs(an - gn) > tolerance:
an, gn = (an + gn) / 2.0, sqrt(an * gn)
return an
print agm(1, 1 / sqrt(2))
|
Transform the following Swift implementation into VB, maintaining the same output and logic. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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
|
Port the provided Swift code into VB while preserving the original functionality. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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
|
Translate this program into Go but keep the logic exactly as in Swift. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 0")
}
| package main
import (
"fmt"
"math"
)
const ε = 1e-14
func agm(a, g float64) float64 {
for math.Abs(a-g) > math.Abs(a)*ε {
a, g = (a+g)*.5, math.Sqrt(a*g)
}
return a
}
func main() {
fmt.Println(agm(1, 1/math.Sqrt2))
}
|
Rewrite this program in Go while keeping its functionality equivalent to the Swift version. | import Darwin
enum AGRError : Error {
case undefined
}
func agm(_ a: Double, _ g: Double, _ iota: Double = 1e-8) throws -> Double {
var a = a
var g = g
var a1: Double = 0
var g1: Double = 0
guard a * g >= 0 else {
throw AGRError.undefined
}
while abs(a - g) > iota {
a1 = (a + g) / 2
g1 = sqrt(a * g)
a = a1
g = g1
}
return a
}
do {
try print(agm(1, 1 / sqrt(2)))
} catch {
print("agr is undefined when a * g < 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))
}
|
Preserve the algorithm and functionality while converting the code from Tcl to C. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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 the same code in C as shown below in Tcl. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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;
}
|
Generate a C# translation of this Tcl snippet without changing its computational steps. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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 the following code from Tcl to C#, ensuring the logic remains intact. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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));
}
}
}
}
|
Port the provided Tcl code into C++ while preserving the original functionality. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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;
}
|
Ensure the translated C++ code behaves exactly like the original Tcl snippet. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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;
}
|
Produce a language-to-language conversion: from Tcl to Java, same semantics. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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 Tcl version. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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)));
}
}
|
Transform the following Tcl implementation into Python, maintaining the same output and logic. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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 Tcl snippet. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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))
|
Produce a functionally identical VB code for the snippet given in Tcl. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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
|
Produce a functionally identical VB code for the snippet given in Tcl. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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
|
Produce a functionally identical Go code for the snippet given in Tcl. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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))
}
|
Convert the following code from Tcl to Go, ensuring the logic remains intact. | proc agm {a b} {
set old_b [expr {$b<0?inf:-inf}]
while {$a != $b && $b != $old_b} {
set old_b $b
lassign [list [expr {0.5*($a+$b)}] [expr {sqrt($a*$b)}]] a b
}
return $a
}
puts [agm 1 [expr 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))
}
|
Write the same algorithm in PHP as shown in this Rust implementation. |
fn main () {
let mut args = std::env::args();
let x = args.nth(1).expect("First argument not specified.").parse::<f32>().unwrap();
let y = args.next().expect("Second argument not specified.").parse::<f32>().unwrap();
let result = agm(x,y);
println!("The arithmetic-geometric mean is {}", result);
}
fn agm (x: f32, y: f32) -> f32 {
let e: f32 = 0.000001;
let mut a = x;
let mut g = y;
let mut a1: f32;
let mut g1: f32;
if a * g < 0f32 { panic!("The arithmetric-geometric mean is undefined for numbers less than zero!"); }
else {
loop {
a1 = (a + g) / 2.;
g1 = (a * g).sqrt();
a = a1;
g = g1;
if (a - g).abs() < e { return a; }
}
}
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Transform the following Rust implementation into PHP, maintaining the same output and logic. |
fn main () {
let mut args = std::env::args();
let x = args.nth(1).expect("First argument not specified.").parse::<f32>().unwrap();
let y = args.next().expect("Second argument not specified.").parse::<f32>().unwrap();
let result = agm(x,y);
println!("The arithmetic-geometric mean is {}", result);
}
fn agm (x: f32, y: f32) -> f32 {
let e: f32 = 0.000001;
let mut a = x;
let mut g = y;
let mut a1: f32;
let mut g1: f32;
if a * g < 0f32 { panic!("The arithmetric-geometric mean is undefined for numbers less than zero!"); }
else {
loop {
a1 = (a + g) / 2.;
g1 = (a * g).sqrt();
a = a1;
g = g1;
if (a - g).abs() < e { return a; }
}
}
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Transform the following Ada implementation into PHP, maintaining the same output and logic. | with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
procedure Arith_Geom_Mean is
type Num is digits 18;
package N_IO is new Ada.Text_IO.Float_IO(Num);
package Math is new Ada.Numerics.Generic_Elementary_Functions(Num);
function AGM(A, G: Num) return Num is
Old_G: Num;
New_G: Num := G;
New_A: Num := A;
begin
loop
Old_G := New_G;
New_G := Math.Sqrt(New_A*New_G);
New_A := (Old_G + New_A) * 0.5;
exit when (New_A - New_G) <= Num'Epsilon;
end loop;
return New_G;
end AGM;
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Translate this program into PHP but keep the logic exactly as in Ada. | with Ada.Text_IO, Ada.Numerics.Generic_Elementary_Functions;
procedure Arith_Geom_Mean is
type Num is digits 18;
package N_IO is new Ada.Text_IO.Float_IO(Num);
package Math is new Ada.Numerics.Generic_Elementary_Functions(Num);
function AGM(A, G: Num) return Num is
Old_G: Num;
New_G: Num := G;
New_A: Num := A;
begin
loop
Old_G := New_G;
New_G := Math.Sqrt(New_A*New_G);
New_A := (Old_G + New_A) * 0.5;
exit when (New_A - New_G) <= Num'Epsilon;
end loop;
return New_G;
end AGM;
begin
N_IO.Put(AGM(1.0, 1.0/Math.Sqrt(2.0)), Fore => 1, Aft => 17, Exp => 0);
end Arith_Geom_Mean;
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the programming language of this snippet from AWK to PHP without modifying what it does. |
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
}
function agm(a,g) {
while (1) {
a0=a
a=(a0+g)/2
g=sqrt(a0*g)
if (abs(a0-a) < abs(a)*1e-15) break
}
return a
}
function abs(x) {
return (x<0 ? -x : x)
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the following code from AWK to PHP with equivalent syntax and logic. |
BEGIN {
printf "%.16g\n", agm(1.0,sqrt(0.5))
}
function agm(a,g) {
while (1) {
a0=a
a=(a0+g)/2
g=sqrt(a0*g)
if (abs(a0-a) < abs(a)*1e-15) break
}
return a
}
function abs(x) {
return (x<0 ? -x : x)
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the programming language of this snippet from BBC_Basic to PHP without modifying what it does. | *FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
END
DEF FNagm(a,g)
LOCAL ta
REPEAT
ta = a
a = (a+g)/2
g = SQR(ta*g)
UNTIL a = ta
= a
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the following BBC_Basic code into PHP without altering its purpose. | *FLOAT 64
@% = &1010
PRINT FNagm(1, 1/SQR(2))
END
DEF FNagm(a,g)
LOCAL ta
REPEAT
ta = a
a = (a+g)/2
g = SQR(ta*g)
UNTIL a = ta
= a
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Can you help me rewrite this code in PHP instead of Common_Lisp, keeping it the same logically? | (ns agmcompute
(:gen-class))
(import '(org.apfloat Apfloat ApfloatMath))
(def precision 70)
(def one (Apfloat. 1M precision))
(def two (Apfloat. 2M precision))
(def half (Apfloat. 0.5M precision))
(def isqrt2 (.divide one (ApfloatMath/pow two half)))
(def TOLERANCE (Apfloat. 0.000000M precision))
(defn agm [a g]
" Simple AGM Loop calculation "
(let [THRESH 1e-65
MAX-LOOPS 1000000]
(loop [[an gn] [a g], cnt 0]
(if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH)
(> cnt MAX-LOOPS))
an
(recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)]
(inc cnt))))))
(println (agm one isqrt2))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Keep all operations the same but rewrite the snippet in PHP. | (ns agmcompute
(:gen-class))
(import '(org.apfloat Apfloat ApfloatMath))
(def precision 70)
(def one (Apfloat. 1M precision))
(def two (Apfloat. 2M precision))
(def half (Apfloat. 0.5M precision))
(def isqrt2 (.divide one (ApfloatMath/pow two half)))
(def TOLERANCE (Apfloat. 0.000000M precision))
(defn agm [a g]
" Simple AGM Loop calculation "
(let [THRESH 1e-65
MAX-LOOPS 1000000]
(loop [[an gn] [a g], cnt 0]
(if (or (< (ApfloatMath/abs (.subtract an gn)) THRESH)
(> cnt MAX-LOOPS))
an
(recur [(.multiply (.add an gn) half) (ApfloatMath/pow (.multiply an gn) half)]
(inc cnt))))))
(println (agm one isqrt2))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this D block to PHP, preserving its control flow and logic. | import std.stdio, std.math, std.meta, std.typecons;
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
do {
AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g));
} while (feqrel(a, g) < bitPrecision);
return a;
}
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Rewrite the snippet below in PHP so it works the same as the original D code. | import std.stdio, std.math, std.meta, std.typecons;
real agm(real a, real g, in int bitPrecision=60) pure nothrow @nogc @safe {
do {
AliasSeq!(a, g) = tuple((a + g) / 2.0, sqrt(a * g));
} while (feqrel(a, g) < bitPrecision);
return a;
}
void main() @safe {
writefln("%0.19f", agm(1, 1 / sqrt(2.0)));
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert the following code from Delphi to PHP, ensuring the logic remains intact. | program geometric_mean;
uses
System.SysUtils;
function Fabs(value: Double): Double;
begin
Result := value;
if Result < 0 then
Result := -Result;
end;
function agm(a, g: Double):Double;
var
iota, a1, g1: Double;
begin
iota := 1.0E-16;
if a * g < 0.0 then
begin
Writeln('arithmetic-geometric mean undefined when x*y<0');
exit(1);
end;
while Fabs(a - g) > iota do
begin
a1 := (a + g) / 2.0;
g1 := sqrt(a * g);
a := a1;
g := g1;
end;
Exit(a);
end;
var
x, y: Double;
begin
Write('Enter two numbers:');
Readln(x, y);
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
end.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Produce a functionally identical PHP code for the snippet given in Delphi. | program geometric_mean;
uses
System.SysUtils;
function Fabs(value: Double): Double;
begin
Result := value;
if Result < 0 then
Result := -Result;
end;
function agm(a, g: Double):Double;
var
iota, a1, g1: Double;
begin
iota := 1.0E-16;
if a * g < 0.0 then
begin
Writeln('arithmetic-geometric mean undefined when x*y<0');
exit(1);
end;
while Fabs(a - g) > iota do
begin
a1 := (a + g) / 2.0;
g1 := sqrt(a * g);
a := a1;
g := g1;
end;
Exit(a);
end;
var
x, y: Double;
begin
Write('Enter two numbers:');
Readln(x, y);
writeln(format('The arithmetic-geometric mean is %.6f', [agm(x, y)]));
readln;
end.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this Elixir snippet to PHP and keep its semantics consistent. | defmodule ArithhGeom do
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
mean((a+g)/2,:math.pow(a*g, 0.5),tol)
end
end
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Can you help me rewrite this code in PHP instead of Elixir, keeping it the same logically? | defmodule ArithhGeom do
def mean(a,g,tol) when abs(a-g) <= tol, do: a
def mean(a,g,tol) do
mean((a+g)/2,:math.pow(a*g, 0.5),tol)
end
end
IO.puts ArithhGeom.mean(1,1/:math.sqrt(2),0.0000000001)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Can you help me rewrite this code in PHP instead of Erlang, keeping it the same logically? |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Please provide an equivalent version of this Erlang code in PHP. |
-module(agm_calculator).
-export([find_agm/0]).
-define(TOLERANCE, 0.0000000001).
find_agm() ->
A = 1,
B = 1 / (math:pow(2, 0.5)),
AGM = agm(A, B),
io:format("AGM = ~p", [AGM]).
agm (A, B) when abs(A-B) =< ?TOLERANCE ->
A;
agm (A, B) ->
A1 = (A+B) / 2,
B1 = math:pow(A*B, 0.5),
agm(A1, B1).
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Can you help me rewrite this code in PHP instead of F#, keeping it the same logically? | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Rewrite this program in PHP while keeping its functionality equivalent to the F# version. | let rec agm a g precision =
if precision > abs(a - g) then a else
agm (0.5 * (a + g)) (sqrt (a * g)) precision
printfn "%g" (agm 1. (sqrt(0.5)) 1e-15)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Rewrite the snippet below in PHP so it works the same as the original Factor code. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Generate a PHP translation of this Factor snippet without changing its computational steps. | USING: kernel math math.functions prettyprint ;
IN: rosetta-code.arithmetic-geometric-mean
: agm ( a g -- a' g' ) 2dup [ + 0.5 * ] 2dip * sqrt ;
1 1 2 sqrt / [ 2dup - 1e-15 > ] [ agm ] while drop .
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Write a version of this Forth function in PHP with identical behavior. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Maintain the same structure and functionality when rewriting this code in PHP. | : agm
begin
fover fover f+ 2e f/
frot frot f* fsqrt
fover fover 1e-15 f~
until
fdrop ;
1e 2e -0.5e f** agm f.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Generate an equivalent PHP version of this Fortran code. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Keep all operations the same but rewrite the snippet in PHP. | function agm(a,b)
implicit none
double precision agm,a,b,eps,c
parameter(eps=1.0d-15)
10 c=0.5d0*(a+b)
b=sqrt(a*b)
a=c
if(a-b.gt.eps*a) go to 10
agm=0.5d0*(a+b)
end
program test
implicit none
double precision agm
print*,agm(1.0d0,1.0d0/sqrt(2.0d0))
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Produce a language-to-language conversion: from Groovy to PHP, same semantics. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this Groovy snippet to PHP and keep its semantics consistent. | double agm (double a, double g) {
double an = a, gn = g
while ((an-gn).abs() >= 10.0**-14) { (an, gn) = [(an+gn)*0.5, (an*gn)**0.5] }
an
}
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Produce a functionally identical PHP code for the snippet given in Haskell. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Preserve the algorithm and functionality while converting the code from Haskell to PHP. |
agm :: (Floating a) => a -> a -> ((a, a) -> Bool) -> a
agm a g eq = snd $ until eq step (a, g)
where
step (a, g) = ((a + g) / 2, sqrt (a * g))
relDiff :: (Fractional a) => (a, a) -> a
relDiff (x, y) =
let n = abs (x - y)
d = (abs x + abs y) / 2
in n / d
main :: IO ()
main = do
let equal = (< 0.000000001) . relDiff
print $ agm 1 (1 / sqrt 2) equal
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Generate an equivalent PHP version of this J code. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Write a version of this J function in PHP with identical behavior. | mean=: +/ % #
(mean , */ %:~ #)^:_] 1,%%:2
0.8472130847939792 0.8472130847939791
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Keep all operations the same but rewrite the snippet in PHP. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the provided Julia code into PHP while preserving the original functionality. | function agm(x, y, e::Real = 5)
(x ≤ 0 || y ≤ 0 || e ≤ 0) && throw(DomainError("x, y must be strictly positive"))
g, a = minmax(x, y)
while e * eps(x) < a - g
a, g = (a + g) / 2, sqrt(a * g)
end
a
end
x, y = 1.0, 1 / √2
println("
@show agm(x, y)
println("
x, y = Float32(x), Float32(y)
@show agm(x, y)
println("
x, y = big(1.0), 1 / √big(2.0)
@show agm(x, y)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the following code from Lua to PHP with equivalent syntax and logic. | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Can you help me rewrite this code in PHP instead of Lua, keeping it the same logically? | function agm(a, b, tolerance)
if not tolerance or tolerance < 1e-15 then
tolerance = 1e-15
end
repeat
a, b = (a + b) / 2, math.sqrt(a * b)
until math.abs(a-b) < tolerance
return a
end
print(string.format("%.15f", agm(1, 1 / math.sqrt(2))))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the following Mathematica code into PHP without altering its purpose. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this Mathematica snippet to PHP and keep its semantics consistent. | PrecisionDigits = 85;
AGMean[a_, b_] := FixedPoint[{ Tr@#/2, Sqrt[Times@@#] }&, N[{a,b}, PrecisionDigits]]〚1〛
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this MATLAB snippet to PHP and keep its semantics consistent. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Generate a PHP translation of this MATLAB snippet without changing its computational steps. | function [a,g]=agm(a,g)
while (1)
a0=a;
a=(a0+g)/2;
g=sqrt(a0*g);
if (abs(a0-a) < a*eps) break; end;
end;
end
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the provided Nim code into PHP while preserving the original functionality. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert the following code from Nim to PHP, ensuring the logic remains intact. | import math
proc agm(a, g: float,delta: float = 1.0e-15): float =
var
aNew: float = 0
aOld: float = a
gOld: float = g
while (abs(aOld - gOld) > delta):
aNew = 0.5 * (aOld + gOld)
gOld = sqrt(aOld * gOld)
aOld = aNew
result = aOld
echo agm(1.0,1.0/sqrt(2.0))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Transform the following OCaml implementation into PHP, maintaining the same output and logic. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Rewrite this program in PHP while keeping its functionality equivalent to the OCaml version. | let rec agm a g tol =
if tol > abs_float (a -. g) then a else
agm (0.5*.(a+.g)) (sqrt (a*.g)) tol
let _ = Printf.printf "%.16f\n" (agm 1.0 (sqrt 0.5) 1e-15)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this Pascal snippet to PHP and keep its semantics consistent. | Program ArithmeticGeometricMean;
uses
gmp;
procedure agm (in1, in2: mpf_t; var out1, out2: mpf_t);
begin
mpf_add (out1, in1, in2);
mpf_div_ui (out1, out1, 2);
mpf_mul (out2, in1, in2);
mpf_sqrt (out2, out2);
end;
const
nl = chr(13)+chr(10);
var
x0, y0, resA, resB: mpf_t;
i: integer;
begin
mpf_set_default_prec (65568);
mpf_init_set_ui (y0, 1);
mpf_init_set_d (x0, 0.5);
mpf_sqrt (x0, x0);
mpf_init (resA);
mpf_init (resB);
for i := 0 to 6 do
begin
agm(x0, y0, resA, resB);
agm(resA, resB, x0, y0);
end;
mp_printf ('%.20000Ff'+nl, @x0);
mp_printf ('%.20000Ff'+nl+nl, @y0);
end.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Change the following Pascal code into PHP without altering its purpose. | 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.
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Ensure the translated PHP code behaves exactly like the original Perl snippet. |
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";
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert the following code from Perl to PHP, ensuring the logic remains intact. |
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";
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Port the following code from PowerShell to PHP with equivalent syntax 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))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Preserve the algorithm and functionality while converting the code from PowerShell to PHP. | 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))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this R block to PHP, preserving its control flow 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))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Write a version of this R function in PHP with identical behavior. | 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))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Please provide an equivalent version of this Racket code in PHP. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Maintain the same structure and functionality when rewriting this code in PHP. | #lang racket
(define (agm a g [ε 1e-15])
(if (<= (- a g) ε)
a
(agm (/ (+ a g) 2) (sqrt (* a g)) ε)))
(agm 1 (/ 1 (sqrt 2)))
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Generate a PHP translation of this COBOL snippet without changing its computational steps. | 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).
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Transform the following COBOL implementation into PHP, maintaining the same output 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).
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Rewrite the snippet below in PHP so it works the same as the original REXX code. |
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
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert this REXX snippet to PHP and keep its semantics consistent. |
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
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Convert the following code from Ruby to PHP, 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)
| define('PRECISION', 13);
function agm($a0, $g0, $tolerance = 1e-10)
{
$limit = number_format($tolerance, PRECISION, '.', '');
$an = $a0;
$gn = $g0;
do {
list($an, $gn) = array(
bcdiv(bcadd($an, $gn), 2),
bcsqrt(bcmul($an, $gn)),
);
} while (bccomp(bcsub($an, $gn), $limit) > 0);
return $an;
}
bcscale(PRECISION);
echo agm(1, 1 / bcsqrt(2));
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.