-
Notifications
You must be signed in to change notification settings - Fork 9
/
test_fixed_point.py
92 lines (64 loc) · 2.49 KB
/
test_fixed_point.py
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import algosdk.abi as sdkabi
from algosdk.abi.ufixed_type import UfixedType
from pyteal import *
from tests.helpers import assert_close_enough
from .ufixed import *
tests_per_contract = 15
def generate_valid_operands():
"""generate_valid_operands generates operands and"""
# random.seed()
# N = random.randrange(8, 64, 8)
# M = random.randrange(1, 2, 1)
N = 64
M = 3
# TODO
A = 3.3
B = 2.3
return A, B, UFixed(N, M)
def sdk_encode(t: UfixedType, v: float):
return Bytes(t.encode(int(v * (10 ** t.precision))))
def test_fixedpoint_add():
exprs, outputs, precisions = [], [], []
for _ in range(1):
a, b, fp = generate_valid_operands()
t = sdkabi.UfixedType(fp.bits, fp.precision)
exprs.append(Log(fp.encode(fp(sdk_encode(t, a)) + fp(sdk_encode(t, b)))))
outputs.append(a + b)
precisions.append(t)
assert_close_enough(Seq(*exprs), outputs, precisions, pad_budget=15)
def test_fixedpoint_sub():
exprs, outputs, precisions = [], [], []
for _ in range(1):
a, b, fp = generate_valid_operands()
t = sdkabi.UfixedType(fp.bits, fp.precision)
exprs.append(Log(fp.encode(fp(sdk_encode(t, a)) - fp(sdk_encode(t, b)))))
outputs.append((a - b))
precisions.append(t)
assert_close_enough(Seq(*exprs), outputs, precisions, pad_budget=15)
def test_fixedpoint_div():
exprs, outputs, precisions = [], [], []
for _ in range(1):
a, b, fp = generate_valid_operands()
t = sdkabi.UfixedType(fp.bits, fp.precision)
exprs.append(Log(fp.encode(fp(sdk_encode(t, a)) / fp(sdk_encode(t, b)))))
outputs.append(a / b)
precisions.append(t)
assert_close_enough(Seq(*exprs), outputs, precisions, pad_budget=15)
def test_fixedpoint_mul():
exprs, outputs, precisions = [], [], []
for _ in range(1):
a, b, fp = generate_valid_operands()
t = sdkabi.UfixedType(fp.bits, fp.precision)
exprs.append(Log(fp.encode(fp(sdk_encode(t, a)) * fp(sdk_encode(t, b)))))
outputs.append(a * b)
precisions.append(t)
assert_close_enough(Seq(*exprs), outputs, precisions, pad_budget=15)
def test_fixedpoint_rescale():
exprs, outputs, precisions = [], [], []
fp = UFixed(64, 2)
t = sdkabi.UfixedType(fp.bits, fp.precision)
val = 123123.12
exprs.append(Log(fp.encode(fp(sdk_encode(t, val)))))
outputs.append(val)
precisions.append(t)
assert_close_enough(Seq(*exprs), outputs, precisions, pad_budget=15)