I'm rewriting the amount class in beem for using fixed-point arithmetic

beem-logo

I’m currently rewriting the Amount class of beem, my python library for steem. The amount class represents STEEM, SBD and VESTS amounts:

1
2
3
4
5
6
7
from beem.amount import Amount
a = Amount(6.104, "STEEM")
print(a)
print(float(a))
print(a.amount)
print(a.symbol)
print(int(a))

will results in

1
2
3
4
5
6.104 STEEM
6.104
6.104
STEEM
6104

Currently, a float number is internally used to
represent STEEM and SBD amounts. I’m trying to improve this by using the decimal class.

This changes may lead to different results in your code.

What will change

STEEM and SBD are fix point numbers, which means that they are basically integers.

6.104 STEEM can be represent as 6104. When I now divide or multiply, I could round the amount to an integer.

When I now divide the amount by 6, and multiply it again by 6, I will have the following operations:

1
int(int(6104 / 6) * 6)

which will be

1
int(1017 * 6) = 6102

Which has the meaning, we have 6.104 STEEM and want it divide by 6, so that 6 accounts can receive the same amount. As this is not possible, we need to send 0.002 STEEM first to @null and then it can be done.

In the same way, when I try to add 0.0001 to 6.104 STEEM, the amount will not change, no matter who often I add 0.0001 to 6.104 STEEM. As 0.0001 STEEM are not valid, it means basically that I try to add 0 STEEM to 6.104 STEEM. Thus, the amount will not change, no matter how often I try to add 0.0001.

1
int(6104 + 0.1) = 6104

What will change

The results of multiplication and addition may be different.

1
(6.104 STEEM + 0.0009) + 0.0009) = 6.104 STEEM

Before, the output of this equation was 6.105 STEEM

1
(6.104 STEEM / 6) * 6 = 6.102 STEEM

Before, the output was 6.104 STEEM

What do you think?

Are my planed changes to the Amount class usefull? As the amount is a fixed point number, fixed point arithmetic should be applied, or?