-2

I have to create a vending machine that adjusts its price according to the temperature for my AP computer science class. The only problem that I have right now is that the machine isn't supposed to accept pennies, so I tried to take the user's input (amount of money) and modulo it by .05 and if that results in anything other than 0.0 it re-runs the program and prompts the user for an input that doesn't contain pennies; however, even if I enter a number that, if modded by .05, does result in 0.0, it still tells the user "No pennies please." The user input is a double and not an int and I don't know whether or not that affects it or not but I'll attach something similar to what the code looks like:

double userMoney = scan.nextDouble 

double penny = userMoeny % .05
if (penny != 0.0)
    says something that says no pennys
else
    executes the rest of the code

I understand that wasn't properly formatted but I don't have access to the raw code as of right now.

As I said before, no matter what I enter, when I mod it by .05 it always seems to come out to be anything but 0.0, even if it should be.

Jerrybibo
  • 1,315
  • 1
  • 21
  • 27
  • 1
    I'm not sure you can do a modulo like this. Modulo is the left over of a division. But dividing by a fraction (0.05 is equivalent to 1/20) is like doing a multiplication (x/(y/z) = (x*z)/y). So I don't believe you'll get the result you're looking for. – AntonH Oct 29 '17 at 21:58
  • There is a chance that the operation resulted in an imprecise answer. See this post: https://stackoverflow.com/questions/3227342/modulus-with-doubles-in-java – Jerrybibo Oct 29 '17 at 22:01
  • 1
    This does not appear to be a JavaScript question but the answer is the same. Double precision floats are binary numbers and do not represent decimal fractions exactly. Results you expect to be zero are likely to be non zero by a very small amount. See https://stackoverflow.com/q/1458633/5217142 and related questions for discussion (JavaScript uses double precision floating point to represent numbers). – traktor Oct 29 '17 at 22:03
  • Is this java or JavaScript? – user1803551 Oct 29 '17 at 22:03
  • @AntonH FWIW modulo is not division and could be defined as a difference between an integer multiple of the modulus and a number. In JavaScript `0.75 % 0.5` evaluates to the expected result of `0.25`. (Exact binary fractions chosen deliberately). – traktor Oct 29 '17 at 22:09
  • @Traktor53 Good to know. I've not played around much with modulo, and even less modulo with decimal values. Do you know if it's the same in Java (since OP still hasn't specified which language he's asking for)? – AntonH Oct 29 '17 at 22:17

1 Answers1

2

You could store the amount of money as integer in cents, and divide it by 100 when displaying to user.

This way you will also overcome floating point issues like 0.1 + 0.2 != 0.3.

Martin Adámek
  • 16,771
  • 5
  • 45
  • 64
  • You could also convert input **simulated** by a string value into a valid integer number of cents, accept values that are a multiple of 5c and reject left overs. Real world machines that don't accept pennies reject them without ever putting them into the coin hopper. If the amount remaining after rejecting pennies is not enough it's an "insert more coin" response. Note the integer range of a double precision floating point is amply sufficient for a vending machine, but integer data types may be clearer if available in the language used. – traktor Oct 29 '17 at 23:44