WeixiangYan/CodeTransOcean huggingface dataset:
Task: Approximate equality







Python: from numpy import sqrt
from math import isclose

testvalues = [[100000000000000.01,           100000000000000.011],
              [100.01,                       100.011],
              [10000000000000.001 / 10000.0, 1000000000.0000001000],
              [0.001,                        0.0010000001],
              [0.000000000000000000000101,   0.0],
              [sqrt(2) * sqrt(2),            2.0],
              [-sqrt(2) * sqrt(2),          -2.0],
              [3.14159265358979323846,       3.14159265358979324]]

for (x, y) in testvalues:
    maybenot = "is" if isclose(x, y) else "is NOT"
    print(x, maybenot, "approximately equal to ", y)


VB: Imports System.Runtime.CompilerServices

Module Module1

    <Extension()>
    Function ApproxEquals(ByVal value As Double, other As Double, epsilon As Double)
        Return Math.Abs(value - other) < epsilon
    End Function

    Sub Test(a As Double, b As Double)
        Dim epsilon = 1.0E-18
        Console.WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}")
    End Sub

    Sub Main()
        Test(100000000000000.02, 100000000000000.02)
        Test(100.01, 100.011)
        Test(10000000000000.002 / 10000.0, 1000000000.0000001)
        Test(0.001, 0.0010000001)
        Test(1.01E-22, 0.0)
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0)
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0)
        Test(3.1415926535897931, 3.1415926535897931)
    End Sub

End Module
Task: Approximate equality


C++: #include <iomanip>
#include <iostream>
#include <cmath>

bool approxEquals(double a, double b, double e) {
    return fabs(a - b) < e;
}

void test(double a, double b) {
    constexpr double epsilon = 1e-18;
    std::cout << std::setprecision(21) << a;
    std::cout << ", ";
    std::cout << std::setprecision(21) << b;
    std::cout << " => ";
    std::cout << approxEquals(a, b, epsilon) << '\n';
}

int main() {
    test(100000000000000.01, 100000000000000.011);
    test(100.01, 100.011);
    test(10000000000000.001 / 10000.0, 1000000000.0000001000);
    test(0.001, 0.0010000001);
    test(0.000000000000000000000101, 0.0);
    test(sqrt(2.0) * sqrt(2.0), 2.0);
    test(-sqrt(2.0) * sqrt(2.0), -2.0);
    test(3.14159265358979323846, 3.14159265358979324);
    return 0;
}



Go: package main

import (
    "fmt"
    "log"
    "math/big"
)

func max(a, b *big.Float) *big.Float {
    if a.Cmp(b) > 0 {
        return a
    }
    return b
}

func isClose(a, b *big.Float) bool {
    relTol := big.NewFloat(1e-9) 
    t := new(big.Float)
    t.Sub(a, b)
    t.Abs(t)
    u, v, w := new(big.Float), new(big.Float), new(big.Float)
    u.Mul(relTol, max(v.Abs(a), w.Abs(b)))
    return t.Cmp(u) <= 0
}

func nbf(s string) *big.Float {
    n, ok := new(big.Float).SetString(s)
    if !ok {
        log.Fatal("invalid floating point number")
    }
    return n
}

func main() {
    root2 := big.NewFloat(2.0)
    root2.Sqrt(root2)
    pairs := [][2]*big.Float{
        {nbf("100000000000000.01"), nbf("100000000000000.011")},
        {nbf("100.01"), nbf("100.011")},
        {nbf("0").Quo(nbf("10000000000000.001"), nbf("10000.0")), nbf("1000000000.0000001000")},
        {nbf("0.001"), nbf("0.0010000001")},
        {nbf("0.000000000000000000000101"), nbf("0.0")},
        {nbf("0").Mul(root2, root2), nbf("2.0")},
        {nbf("0").Mul(nbf("0").Neg(root2), root2), nbf("-2.0")},
        {nbf("100000000000000003.0"), nbf("100000000000000004.0")},
        {nbf("3.14159265358979323846"), nbf("3.14159265358979324")},
    }
    for _, pair := range pairs {
        s := "≉"
        if isClose(pair[0], pair[1]) {
            s = "≈"
        }
        fmt.Printf("% 21.19g %s %- 21.19g\n", pair[0], s, pair[1])
    }
}
Task: Approximate equality



C#: using System;

public static class Program
{
    public static void Main() {
        Test(100000000000000.01, 100000000000000.011);
        Test(100.01, 100.011);
        Test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        Test(0.001, 0.0010000001);
        Test(0.000000000000000000000101, 0.0);
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0);
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0);
        Test(3.14159265358979323846, 3.14159265358979324);

        void Test(double a, double b) {
            const double epsilon = 1e-18;
            WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}");
        }
    }

    public static bool ApproxEquals(this double value, double other, double epsilon) => Math.Abs(value - other) < epsilon;
}



Java: public class Approximate {
    private static boolean approxEquals(double value, double other, double epsilon) {
        return Math.abs(value - other) < epsilon;
    }

    private static void test(double a, double b) {
        double epsilon = 1e-18;
        System.out.printf("%f, %f => %s\n", a, b, approxEquals(a, b, epsilon));
    }

    public static void main(String[] args) {
        test(100000000000000.01, 100000000000000.011);
        test(100.01, 100.011);
        test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        test(0.001, 0.0010000001);
        test(0.000000000000000000000101, 0.0);
        test(Math.sqrt(2.0) * Math.sqrt(2.0), 2.0);
        test(-Math.sqrt(2.0) * Math.sqrt(2.0), -2.0);
        test(3.14159265358979323846, 3.14159265358979324);
    }
}
Task: Approximate equality



C#: using System;

public static class Program
{
    public static void Main() {
        Test(100000000000000.01, 100000000000000.011);
        Test(100.01, 100.011);
        Test(10000000000000.001 / 10000.0, 1000000000.0000001000);
        Test(0.001, 0.0010000001);
        Test(0.000000000000000000000101, 0.0);
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0);
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0);
        Test(3.14159265358979323846, 3.14159265358979324);

        void Test(double a, double b) {
            const double epsilon = 1e-18;
            WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}");
        }
    }

    public static bool ApproxEquals(this double value, double other, double epsilon) => Math.Abs(value - other) < epsilon;
}






VB: Imports System.Runtime.CompilerServices

Module Module1

    <Extension()>
    Function ApproxEquals(ByVal value As Double, other As Double, epsilon As Double)
        Return Math.Abs(value - other) < epsilon
    End Function

    Sub Test(a As Double, b As Double)
        Dim epsilon = 1.0E-18
        Console.WriteLine($"{a}, {b} => {a.ApproxEquals(b, epsilon)}")
    End Sub

    Sub Main()
        Test(100000000000000.02, 100000000000000.02)
        Test(100.01, 100.011)
        Test(10000000000000.002 / 10000.0, 1000000000.0000001)
        Test(0.001, 0.0010000001)
        Test(1.01E-22, 0.0)
        Test(Math.Sqrt(2) * Math.Sqrt(2), 2.0)
        Test(-Math.Sqrt(2) * Math.Sqrt(2), -2.0)
        Test(3.1415926535897931, 3.1415926535897931)
    End Sub

End Module
Task: Jewels and stones

C: #include <stdio.h>
#include <string.h>

int count_jewels(const char *s, const char *j) {
    int count = 0;
    for ( ; *s; ++s) if (strchr(j, *s)) ++count;
    return count;
}

int main() {
    printf("%d\n", count_jewels("aAAbbbb", "aA"));
    printf("%d\n", count_jewels("ZZ", "z"));
    return 0;
}




Go: package main
  
import (
    "fmt"
    "strings"
)

func js(stones, jewels string) (n int) {
    for _, b := range []byte(stones) {
        if strings.IndexByte(jewels, b) >= 0 {
            n++
        }
    }
    return
}

func main() {
    fmt.Println(js("aAAbbbb", "aA"))
}
Task: Jewels and stones

C: #include <stdio.h>
#include <string.h>

int count_jewels(const char *s, const char *j) {
    int count = 0;
    for ( ; *s; ++s) if (strchr(j, *s)) ++count;
    return count;
}

int main() {
    printf("%d\n", count_jewels("aAAbbbb", "aA"));
    printf("%d\n", count_jewels("ZZ", "z"));
    return 0;
}





Java: import java.util.HashSet;
import java.util.Set;

public class App {
    private static int countJewels(String stones, String jewels) {
        Set<Character> bag = new HashSet<>();
        for (char c : jewels.toCharArray()) {
            bag.add(c);
        }

        int count = 0;
        for (char c : stones.toCharArray()) {
            if (bag.contains(c)) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(countJewels("aAAbbbb", "aA"));
        System.out.println(countJewels("ZZ", "z"));
    }
}
Task: Jewels and stones

C: #include <stdio.h>
#include <string.h>

int count_jewels(const char *s, const char *j) {
    int count = 0;
    for ( ; *s; ++s) if (strchr(j, *s)) ++count;
    return count;
}

int main() {
    printf("%d\n", count_jewels("aAAbbbb", "aA"));
    printf("%d\n", count_jewels("ZZ", "z"));
    return 0;
}







Python: def countJewels(s, j):
    return sum(x in j for x in s)

print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")
Task: Jewels and stones

C: #include <stdio.h>
#include <string.h>

int count_jewels(const char *s, const char *j) {
    int count = 0;
    for ( ; *s; ++s) if (strchr(j, *s)) ++count;
    return count;
}

int main() {
    printf("%d\n", count_jewels("aAAbbbb", "aA"));
    printf("%d\n", count_jewels("ZZ", "z"));
    return 0;
}








VB: Function count_jewels(stones As String, jewels As String) As Integer
    Dim res As Integer: res = 0
    For i = 1 To Len(stones)
        res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
    Next i
    count_jewels = res
End Function
Public Sub main()
    Debug.Print count_jewels("aAAbbbb", "aA")
    Debug.Print count_jewels("ZZ", "z")
End Sub
Task: Jewels and stones




Go: package main
  
import (
    "fmt"
    "strings"
)

func js(stones, jewels string) (n int) {
    for _, b := range []byte(stones) {
        if strings.IndexByte(jewels, b) >= 0 {
            n++
        }
    }
    return
}

func main() {
    fmt.Println(js("aAAbbbb", "aA"))
}


Java: import java.util.HashSet;
import java.util.Set;

public class App {
    private static int countJewels(String stones, String jewels) {
        Set<Character> bag = new HashSet<>();
        for (char c : jewels.toCharArray()) {
            bag.add(c);
        }

        int count = 0;
        for (char c : stones.toCharArray()) {
            if (bag.contains(c)) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(countJewels("aAAbbbb", "aA"));
        System.out.println(countJewels("ZZ", "z"));
    }
}
Task: Jewels and stones





Java: import java.util.HashSet;
import java.util.Set;

public class App {
    private static int countJewels(String stones, String jewels) {
        Set<Character> bag = new HashSet<>();
        for (char c : jewels.toCharArray()) {
            bag.add(c);
        }

        int count = 0;
        for (char c : stones.toCharArray()) {
            if (bag.contains(c)) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(countJewels("aAAbbbb", "aA"));
        System.out.println(countJewels("ZZ", "z"));
    }
}



Python: def countJewels(s, j):
    return sum(x in j for x in s)

print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")
Task: Jewels and stones





Java: import java.util.HashSet;
import java.util.Set;

public class App {
    private static int countJewels(String stones, String jewels) {
        Set<Character> bag = new HashSet<>();
        for (char c : jewels.toCharArray()) {
            bag.add(c);
        }

        int count = 0;
        for (char c : stones.toCharArray()) {
            if (bag.contains(c)) {
                count++;
            }
        }

        return count;
    }

    public static void main(String[] args) {
        System.out.println(countJewels("aAAbbbb", "aA"));
        System.out.println(countJewels("ZZ", "z"));
    }
}




VB: Function count_jewels(stones As String, jewels As String) As Integer
    Dim res As Integer: res = 0
    For i = 1 To Len(stones)
        res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
    Next i
    count_jewels = res
End Function
Public Sub main()
    Debug.Print count_jewels("aAAbbbb", "aA")
    Debug.Print count_jewels("ZZ", "z")
End Sub
Task: Jewels and stones




Go: package main
  
import (
    "fmt"
    "strings"
)

func js(stones, jewels string) (n int) {
    for _, b := range []byte(stones) {
        if strings.IndexByte(jewels, b) >= 0 {
            n++
        }
    }
    return
}

func main() {
    fmt.Println(js("aAAbbbb", "aA"))
}




Python: def countJewels(s, j):
    return sum(x in j for x in s)

print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")
Task: Jewels and stones




Go: package main
  
import (
    "fmt"
    "strings"
)

func js(stones, jewels string) (n int) {
    for _, b := range []byte(stones) {
        if strings.IndexByte(jewels, b) >= 0 {
            n++
        }
    }
    return
}

func main() {
    fmt.Println(js("aAAbbbb", "aA"))
}





VB: Function count_jewels(stones As String, jewels As String) As Integer
    Dim res As Integer: res = 0
    For i = 1 To Len(stones)
        res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
    Next i
    count_jewels = res
End Function
Public Sub main()
    Debug.Print count_jewels("aAAbbbb", "aA")
    Debug.Print count_jewels("ZZ", "z")
End Sub
Task: Jewels and stones







Python: def countJewels(s, j):
    return sum(x in j for x in s)

print countJewels("aAAbbbb", "aA")
print countJewels("ZZ", "z")


VB: Function count_jewels(stones As String, jewels As String) As Integer
    Dim res As Integer: res = 0
    For i = 1 To Len(stones)
        res = res - (InStr(1, jewels, Mid(stones, i, 1), vbBinaryCompare) <> 0)
    Next i
    count_jewels = res
End Function
Public Sub main()
    Debug.Print count_jewels("aAAbbbb", "aA")
    Debug.Print count_jewels("ZZ", "z")
End Sub
Task: Van der Corput sequence

C: #include <stdio.h>

void vc(int n, int base, int *num, int *denom)
{
        int p = 0, q = 1;

        while (n) {
                p = p * base + (n % base);
                q *= base;
                n /= base;
        }

        *num = p;  
        *denom = q;

        while (p) { n = p; p = q % p; q = n; }
        *num /= q;
        *denom /= q;
}

int main()
{
        int d, n, i, b;
        for (b = 2; b < 6; b++) {
                printf("base %d:", b);
                for (i = 0; i < 10; i++) {
                        vc(i, b, &n, &d);
                        if (n) printf("  %d/%d", n, d);
                        else   printf("  0");
                }
                printf("\n");
        }

        return 0;
}


C++: #include <cmath>
#include <iostream>

double vdc(int n, double base = 2)
{
    double vdc = 0, denom = 1;
    while (n)
    {
        vdc += fmod(n, base) / (denom *= base);
        n /= base; 
    }
    return vdc;
}

int main() 
{
    for (double base = 2; base < 6; ++base)
    {
        std::cout << "Base " << base << "\n";
        for (int n = 0; n < 10; ++n)
        {
            std::cout << vdc(n, base) << " ";
        }
        std::cout << "\n\n";
    }
}
Task: Van der Corput sequence

C: #include <stdio.h>

void vc(int n, int base, int *num, int *denom)
{
        int p = 0, q = 1;

        while (n) {
                p = p * base + (n % base);
                q *= base;
                n /= base;
        }

        *num = p;  
        *denom = q;

        while (p) { n = p; p = q % p; q = n; }
        *num /= q;
        *denom /= q;
}

int main()
{
        int d, n, i, b;
        for (b = 2; b < 6; b++) {
                printf("base %d:", b);
                for (i = 0; i < 10; i++) {
                        vc(i, b, &n, &d);
                        if (n) printf("  %d/%d", n, d);
                        else   printf("  0");
                }
                printf("\n");
        }

        return 0;
}




Go: package main

import "fmt"

func v2(n uint) (r float64) {
    p := .5
    for n > 0 {
        if n&1 == 1 {
            r += p
        }
        p *= .5
        n >>= 1
    }
    return
}

func newV(base uint) func(uint) float64 {
    invb := 1 / float64(base)
    return func(n uint) (r float64) {
        p := invb
        for n > 0 {
            r += p * float64(n%base)
            p *= invb
            n /= base
        }
        return
    }
}

func main() {
    fmt.Println("Base 2:")
    for i := uint(0); i < 10; i++ {
        fmt.Println(i, v2(i))
    }
    fmt.Println("Base 3:")
    v3 := newV(3)
    for i := uint(0); i < 10; i++ {
        fmt.Println(i, v3(i))
    }
}
Task: Van der Corput sequence

C: #include <stdio.h>

void vc(int n, int base, int *num, int *denom)
{
        int p = 0, q = 1;

        while (n) {
                p = p * base + (n % base);
                q *= base;
                n /= base;
        }

        *num = p;  
        *denom = q;

        while (p) { n = p; p = q % p; q = n; }
        *num /= q;
        *denom /= q;
}

int main()
{
        int d, n, i, b;
        for (b = 2; b < 6; b++) {
                printf("base %d:", b);
                for (i = 0; i < 10; i++) {
                        vc(i, b, &n, &d);
                        if (n) printf("  %d/%d", n, d);
                        else   printf("  0");
                }
                printf("\n");
        }

        return 0;
}







Python: >>> def base10change(n, base):
	digits = []
	while n:
		n,remainder = divmod(n, base)
		digits.insert(0, remainder)
	return digits

>>> base10change(11, 2)
[1, 0, 1, 1]
Task: Van der Corput sequence

C: #include <stdio.h>

void vc(int n, int base, int *num, int *denom)
{
        int p = 0, q = 1;

        while (n) {
                p = p * base + (n % base);
                q *= base;
                n /= base;
        }

        *num = p;  
        *denom = q;

        while (p) { n = p; p = q % p; q = n; }
        *num /= q;
        *denom /= q;
}

int main()
{
        int d, n, i, b;
        for (b = 2; b < 6; b++) {
                printf("base %d:", b);
                for (i = 0; i < 10; i++) {
                        vc(i, b, &n, &d);
                        if (n) printf("  %d/%d", n, d);
                        else   printf("  0");
                }
                printf("\n");
        }

        return 0;
}








VB: Private Function vdc(ByVal n As Integer, BASE As Variant) As Variant
    Dim res As String
    Dim digit As Integer, g As Integer, denom As Integer
    denom = 1
    Do While n
        denom = denom * BASE
        digit = n Mod BASE
        n = n \ BASE
        res = res & CStr(digit) 
    Loop
    vdc = IIf(Len(res) = 0, "0", "0." & res)
End Function
 
Public Sub show_vdc()
    Dim v As Variant, j As Integer
    For i = 2 To 5
        Debug.Print "Base "; i; ": ";
        For j = 0 To 9
            v = vdc(j, i)
            Debug.Print v; " ";
        Next j
        Debug.Print
    Next i
End Sub
Task: Van der Corput sequence


C++: #include <cmath>
#include <iostream>

double vdc(int n, double base = 2)
{
    double vdc = 0, denom = 1;
    while (n)
    {
        vdc += fmod(n, base) / (denom *= base);
        n /= base; 
    }
    return vdc;
}

int main() 
{
    for (double base = 2; base < 6; ++base)
    {
        std::cout << "Base " << base << "\n";
        for (int n = 0; n < 10; ++n)
        {
            std::cout << vdc(n, base) << " ";
        }
        std::cout << "\n\n";
    }
}



Go: package main

import "fmt"

func v2(n uint) (r float64) {
    p := .5
    for n > 0 {
        if n&1 == 1 {
            r += p
        }
        p *= .5
        n >>= 1
    }
    return
}

func newV(base uint) func(uint) float64 {
    invb := 1 / float64(base)
    return func(n uint) (r float64) {
        p := invb
        for n > 0 {
            r += p * float64(n%base)
            p *= invb
            n /= base
        }
        return
    }
}

func main() {
    fmt.Println("Base 2:")
    for i := uint(0); i < 10; i++ {
        fmt.Println(i, v2(i))
    }
    fmt.Println("Base 3:")
    v3 := newV(3)
    for i := uint(0); i < 10; i++ {
        fmt.Println(i, v3(i))
    }
}
Task: Van der Corput sequence


C++: #include <cmath>
#include <iostream>

double vdc(int n, double base = 2)
{
    double vdc = 0, denom = 1;
    while (n)
    {
        vdc += fmod(n, base) / (denom *= base);
        n /= base; 
    }
    return vdc;
}

int main() 
{
    for (double base = 2; base < 6; ++base)
    {
        std::cout << "Base " << base << "\n";
        for (int n = 0; n < 10; ++n)
        {
            std::cout << vdc(n, base) << " ";
        }
        std::cout << "\n\n";
    }
}




Java: public class VanDerCorput{
	public static double vdc(int n){
		double vdc = 0;
		int denom = 1;
		while(n != 0){
			vdc += n % 2.0 / (denom *= 2);
			n /= 2;
		}
		return vdc;
	}
	
	public static void main(String[] args){
		for(int i = 0; i <= 10; i++){
			System.out.println(vdc(i));
		}
	}
}
Task: Van der Corput sequence


C++: #include <cmath>
#include <iostream>

double vdc(int n, double base = 2)
{
    double vdc = 0, denom = 1;
    while (n)
    {
        vdc += fmod(n, base) / (denom *= base);
        n /= base; 
    }
    return vdc;
}

int main() 
{
    for (double base = 2; base < 6; ++base)
    {
        std::cout << "Base " << base << "\n";
        for (int n = 0; n < 10; ++n)
        {
            std::cout << vdc(n, base) << " ";
        }
        std::cout << "\n\n";
    }
}






Python: >>> def base10change(n, base):
	digits = []
	while n:
		n,remainder = divmod(n, base)
		digits.insert(0, remainder)
	return digits

>>> base10change(11, 2)
[1, 0, 1, 1]
Task: Van der Corput sequence


C++: #include <cmath>
#include <iostream>

double vdc(int n, double base = 2)
{
    double vdc = 0, denom = 1;
    while (n)
    {
        vdc += fmod(n, base) / (denom *= base);
        n /= base; 
    }
    return vdc;
}

int main() 
{
    for (double base = 2; base < 6; ++base)
    {
        std::cout << "Base " << base << "\n";
        for (int n = 0; n < 10; ++n)
        {
            std::cout << vdc(n, base) << " ";
        }
        std::cout << "\n\n";
    }
}







VB: Private Function vdc(ByVal n As Integer, BASE As Variant) As Variant
    Dim res As String
    Dim digit As Integer, g As Integer, denom As Integer
    denom = 1
    Do While n
        denom = denom * BASE
        digit = n Mod BASE
        n = n \ BASE
        res = res & CStr(digit) 
    Loop
    vdc = IIf(Len(res) = 0, "0", "0." & res)
End Function
 
Public Sub show_vdc()
    Dim v As Variant, j As Integer
    For i = 2 To 5
        Debug.Print "Base "; i; ": ";
        For j = 0 To 9
            v = vdc(j, i)
            Debug.Print v; " ";
        Next j
        Debug.Print
    Next i
End Sub