Reputation: 3350
My goal is to accurantly add or remove 0.05% from values with 18 decimals in Python without converting them to floats. I made the two following solutions and they seem correct for me, but I am very unfamiliar with numbers in Python, therefore I would like to know if there's a better (in terms of accurancy) solution.
price_in_wei = 1000000000000000000 # = 1
# -0.05%
price_with_fee = (price_in_wei/1000)*995
# +0.05%
price_with_fee = (price_in_wei/1000)*1005
# -0.05%
price_with_fee = (price_in_wei*995)/1000
# +0.05%
price_with_fee = (price_in_wei*1005)/1000
Upvotes: 2
Views: 523
Reputation: 811
Using decimal
module would be a way to go because, as per the official documentation, "the decimal
module has a user alterable precision (defaulting to 28 places) which can be as large as needed for a given problem."
I think, these two links will help:
https://docs.python.org/3/tutorial/floatingpoint.html
https://docs.python.org/3/library/decimal.html
Upvotes: 1
Reputation: 44108
Let me suggest that you use decimal arithmetic using class decimal.Decimal
. Even if you need the final result to be an integer value, using decimal arithmetic for intermediate calculations will provide greater accuracy. For the example you provided, what you are doing in the second set of calculations work well enough but only because of the specific values used. What if price_in_wei
were instead 1000000000000000001? Your calculation would yield 9.95e+17 or, if converted to an int, 99500000000000000:
>>> price_in_wei
1000000000000000001
>>> price_with_fee = price_in_wei*995/1000
>>> price_with_fee
9.95e+17
>>> int(price_with_fee)
995000000000000000
But decimal arithmetic provides greater precision:
>>> from decimal import Decimal
>>> price_with_fee = Decimal(price_in_wei) * 995 / 1000
>>> price_with_fee
Decimal('995000000000000000.995')
>>> price_with_fee = int(price_with_fee.quantize(Decimal(1))) # round to an integer and convert to int
>>> price_with_fee
995000000000000001
But let's say your currency were US Dollars, which supports two places of precision following the decimal point (cents). If you want that precision, you should work exclusively with decimal arithmetic. For example:
>>> from decimal import Decimal, ROUND_HALF_UP
>>> price_in_wei = Decimal('1000000000000000003')
>>> price_with_fee = (price_in_wei * 995 / 1000).quantize(Decimal('1.00'), decimal.ROUND_HALF_UP) # round to two places
>>> price_with_fee
Decimal('995000000000000002.99')
Upvotes: 4
Reputation: 5805
For accuracy, your second set of calculations will be more accurate. That is, you should multiply first, then divide. Python seems to accept very large integers. On my computer, I'm not sure if I can go above 1E308.
Also, as @RyanZhang has suggested you should use integer/floored division with the //
operator. According to the docs, the result from the //
operator is not guaranteed to be of type 'int', so you may need to always explicitly cast to int:
# -0.05%
price_with_fee = int((price_in_wei*995)//1000)
# +0.05%
price_with_fee = int((price_in_wei*1005)//1000)
Upvotes: 1
Reputation: 1920
If you want to avoid the conversion to floats, be sure to use integer division. This ensures the values stay as integers.
Logically, your code looks fine.
Upvotes: 0