0

So basically I'm trying to create an elementary python program that takes a float as input (managed by cs50 lib) and then subtracts quarters, dimes, nickels, and pennies and calculates the total amount of coins. Somewhere my float gets messed up and starts returning peculiar values like the input of 0,41 changes to 0.15999999999999998 after 0.25 has been subtracted. Why is this happening?

import cs50

def get_cents():
    cents = cs50.get_float("Amount of cents owed: ")
    return cents

def calculate_quarters(cents):
    quarters = 0
    while cents >= 0.25:
        cents = cents - 0.25
        quarters += 1
    return quarters

def calculate_dimes(cents):
    dimes = 0
    while cents >= 0.10:
        cents = cents - 0.10
        dimes += 1
    return dimes

def calculate_nickels(cents):
    nickels=0
    while cents >= 0.05:
        cents = cents - 0.05
        nickels += 1
    return nickels

def calculate_pennies(cents):
    pennies= 0
    while cents >= 0.01:
        cents = cents - 0.01
        pennies += 1
    return pennies

# Ask how many cents the customer is owed
cents = get_cents()
print(cents)

# Calculate the number of quarters (25 cent) to give the customer
quarters = calculate_quarters(cents)
cents = cents - (quarters * 0.25)
print(cents)

#Calculate the number of dimes (10 cent) to give the customer
dimes = calculate_dimes(cents)
cents = cents - (dimes * 0.10)
print(cents)

# Calculate the number of nickels (5 cent) to give the customer
nickels = calculate_nickels(cents)
cents = cents - (nickels * 0.05)
print(cents)

# Calculate the number of pennies (1 cent) to give the customer
pennies = calculate_pennies(cents)
cents = cents - (pennies * 0.01)
print(cents)

# Sum coins
coins = (quarters + dimes + nickels + pennies)
print(cents)

# Print total number of coins to give the customer
print(coins)
dariocodes
  • 133
  • 1
  • 1
  • 7
  • 1
    So don't use floats. Do all your calculations on pennies (integers). [Is floating point math broken?](https://stackoverflow.com/q/588004) – 001 Jan 06 '23 at 13:54
  • @JohnnyMopp well the point is that a user should be able to input a float and then keep subtracting the change of that to calculate the total. So how would you do that if you only worked with ints. – dariocodes Jan 06 '23 at 13:56
  • 1
    If the user enters ".83", change that to "83" and calculate using that. Ex: `quarters = 83 // 25`. Or, using your code: `while cents >= 25: cents = cents - 25` – 001 Jan 06 '23 at 13:58
  • A quarter is 25 cents, not 0.25 cents. Same problem with all of the coin denominations. – jasonharper Jan 06 '23 at 14:50
  • In systems that have them, a penny is usually one cent, not 0.01 cents (and similarly for the other coins). – molbdnilo Jan 06 '23 at 14:50

1 Answers1

1

This phenomenon is produced by the computer calculation which is afterwards transfered to numbers in the decimal system (the computer made its calculations in the binary number system). To resolve this issue have a look at the following post:

How to avoid floating point errors?