2

I am right now playing with some code to try & get more familiar with Python & I made a change calculator in which you input a certain amount of money & it gives you the change you have to return (In Euros). However, I saw it would sometimes give a faulty result for the €0.01 change.

After doing some research, I saw the results for (x mod 20) would give a very strange number. For example, 20.03 mod 20 gives 0.030000000000001137. This problem also occurs for modulo 0.02 (in which the result becomes 0.030000000000001136). My code is as follows:

money = 20.33   # Input here the amount of money you would like to change
moneydivs = [500, 200, 100, 50, 20, 10,
             5, 2, 1, 0.50, 0.20, 0.10, 0.05, 0.02, 0.01]
list = []
i = 0
countpos = 0
moneyoriginal = money


def printlines(pos):
    print(f'{list[pos]} pieces of €{str(moneydivs[pos])}')


print('-----------[Steps in between (For bug hunting)]-----------')
for num in moneydivs:
    if money == 0:
        break
    list.append(int(money // num))
    print(f'num = {num} & '
          f'floor devision = {money // num} and modulus = {money % num}')
    money = money % num
    countpos += 1

print('-----------[Casual output itself]-----------')
print(f'To change {moneyoriginal}, you have to give:')
while i < countpos:
    if list[i] == 0:
        i += 1
        continue
    if i > 8 and i < 12:
        moneydivs[i] = str(moneydivs[i]) + "0"
        # Adds a 0 behind the decimal cents
    printlines(i)
    i += 1

And the output is as following:

-----------[Steps in between (For bug hunting)]-----------
num = 500 & floor devision = 0.0 and modulus = 20.33
num = 200 & floor devision = 0.0 and modulus = 20.33
num = 100 & floor devision = 0.0 and modulus = 20.33
num = 50 & floor devision = 0.0 and modulus = 20.33
num = 20 & floor devision = 1.0 and modulus = 0.3299999999999983
num = 10 & floor devision = 0.0 and modulus = 0.3299999999999983
num = 5 & floor devision = 0.0 and modulus = 0.3299999999999983
num = 2 & floor devision = 0.0 and modulus = 0.3299999999999983
num = 1 & floor devision = 0.0 and modulus = 0.3299999999999983
num = 0.5 & floor devision = 0.0 and modulus = 0.3299999999999983
num = 0.2 & floor devision = 1.0 and modulus = 0.12999999999999828
num = 0.1 & floor devision = 1.0 and modulus = 0.029999999999998278
num = 0.05 & floor devision = 0.0 and modulus = 0.029999999999998278
num = 0.02 & floor devision = 1.0 and modulus = 0.009999999999998278
num = 0.01 & floor devision = 0.0 and modulus = 0.009999999999998278
-----------[Casual output itself]-----------
To change 20.33, you have to give:
1 pieces of €20
1 pieces of €0.20
1 pieces of €0.10
1 pieces of €0.02

I don't understand where this offset comes from, or in case of my current code: Why 20.33 mod 20 = 0.33 - 1.7E-15. Where does the -1.7E-15 come from?

I should add that I'm using the Hydrogen package (in Atom) to give this output. Is there a possibility the issue may lay here?

Sorry if the question is a little bit messy. I get that I should not add my complete code, but I believe in the current case it is necessary to pull a conclusion & since my code is pretty short

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Ruben J.
  • 21
  • 1

0 Answers0