File size: 2,111 Bytes
cb4d9ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import random
import json
import operator

num_samples = [20000, 50000, 230000, 500000]
min_values = [-9.99, -19.99, -99.99, -99.99]
max_values = [9.99, 19.99, 99.99, 99.99]
vals_floats = ["%.2f", "%.3f", "%.3f", "%.4f"]
result_floats = ["%.2f", "%.3f", "%.4f", "%.4f"]
operations = {
    '+': operator.add,
    '-': operator.sub,
    '*': operator.mul,
    '/': operator.truediv
}

def safe_division(numerator, denominator):
    return numerator if denominator == 0 else numerator / denominator

for idx, _ in enumerate(num_samples):
    data = []
    for _ in range(num_samples[idx]):
        num1, num2, num3 = None, None, None
        while num1 in [None, 0., -0.] or num2 in [None, 0., -0.] or num3 in [None, 0., -0.]:
            num1 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
            num2 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))
            num3 = float(vals_floats[idx] % random.uniform(min_values[idx], max_values[idx]))

        operation1 = random.choice(list(operations.keys()))
        operation2 = random.choice(list(operations.keys()))

        if random.choice([True, False]):
            expression = f"({num1} {operation1} {num2}) {operation2} {num3}"
            intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
            result = safe_division(intermediate_result, num3) if operation2 == '/' and intermediate_result != 0 else operations[operation2](intermediate_result, num3)
        else:
            expression = f"{num1} {operation1} {num2} {operation2} {num3}"
            intermediate_result = safe_division(num1, num2) if operation1 == '/' else operations[operation1](num1, num2)
            result = safe_division(intermediate_result, num3) if operation2 == '/' else operations[operation2](intermediate_result, num3)

        output = result_floats[idx] % result
        data.append({'instruction': expression, 'output': output})

    out_file = f'arithmetic-float-complex_{idx}.json'
    with open(out_file, 'w') as f:
        json.dump(data, f)