Results 1 to 4 of 4
  1. #1
    jeffrey12 is offline Member
    Join Date
    Apr 2017
    Rep Power

    Default My program is working as expected but the results are .0000001 short?

    Hi, I made a program where it would calculate sales tax and then add it onto the total price. My program would ask for an input (double Price), and after putting something like 100.0, it would calculate sales tax whether it would be at 10%, 15%, etc.

    My output consists of showing the sales tax, as well as the total price afterwards. The first part shows Price * .1 for the 10% sales tax. Then the second part being Price * 1.1 for the total price. However, if I were to input something like 100.0, it would give me 10 as expected for the sales tax, but 109.999999999999 for the total price. How do I correct this issue so that it shows 110 instead of the repeating 9s? It happens to other percentages as well.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Eastern Florida
    Rep Power

    Default Re: My program is working as expected but the results are .0000001 short?

    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    New Zealand
    Rep Power

    Default Re: My program is working as expected but the results are .0000001 short?

    The Goldberg article is a classic and you should read it.

    That said, it can be a little daunting! The practical upshot for anyone doing arithmetic with a computer is that there is going to be some imprecision (not *every* one of the infinite number of values between zero and one can be distinctly represented without using an infinite amount of time, energy, and memory.)

    Doing real world arithmetic involves thinking about a couple of things (at least)

    Java Code:
    double beforeTax = 100.0;
    double afterTax = beforeTax * 1.1;
    System.out.println("Please pay $" + afterTax);
    How can I print values without the user becoming confused or laughing at me because they think I can't program properly? The answer is to use printf() and friends. It can be used to produce a message like "Please pay $110.00" even though the value of afterTax is not altered in any way. In particular, we do not round the value of afterTax. This is important because we may want to do more calculations with it. Rounding makes things less accurate. It's bad enough that reality imposes some imprecision on us: we don't want to make matters worse by adding inaccuracy as well.

    (precision == pointing to a place on the number line distinct from its neighbours, we can't do that beyond a certain limit. accuracy == pointing to the correct place on the number line with whatever precision we can muster. Those 9's might look frightening, but the amount of imprecision they represent is really, really small unless you are working at LIGO or somewhere.)

    I suspect printf() and friends are the answer to your problem. But maybe not. Or maybe the link is unclear.


    The other problem occurs in situations were we do want an absolutely precise result

    Java Code:
    if(afterTax >= 110.0) {
      // do this
    } else {
      // do that
    There can be no imprecision here. One path or the other will be taken. Other examples might be interest payments (or sales tax!) where contractual or other legal definitions leave (or should leave) no ambiguity. In this case you have no recourse but to find out the legal definition of "10% tax" - or ask your teacher if it's a homework problem - you specifically want to know what the tax on something like $13.37 would be. If it is a homework problem your teacher should be impressed (rather than annoyed) that you'd ask.

    As an aside, notice that ints don't suffer from any imprecision. One is different from two is different from three, all the way up: only a small amount of time/energy/memory is needed to keep them all distinct. This doesn't help directly in your problem because when you multiply an int by 1.1 the result will be a double as discussed by Goldberg. However doing calculations in cents (or some other basic unit like a millicent, dollar etc) can be one way of attacking financial calculations.

    For the if statement above you sometimes see "epsilon" used. The idea is you include some really small value:

    Java Code:
    double epsilon = 0.00000001;
    // ...
    if(afterTax + epsilon >= 110.0) {
      // here for sure
    } else {
      // not here
    In some circumstances (and the value of epsilon depends on those circumstances) this will do what you want.


    Sorry, I started this post intending a tldr for Goldberg's magisterial paper. Fail!

    Almost certainly printf() or similar is what you want. But I couldn't help rambling on. To reiterate one thing: if you search around on the internet you will find people advising you to round (or cast). Just say "no"!
    Last edited by pbrockway2; 01-21-2018 at 12:39 AM.

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    The Netherlands
    Rep Power

    Default Re: My program is working as expected but the results are .0000001 short?

    Welcome back pbrockway2 :)
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

Similar Threads

  1. Assignment not working as expected
    By steveby63 in forum New To Java
    Replies: 9
    Last Post: 04-14-2014, 10:58 PM
  2. .Jar not working as expected
    By iFrozen94 in forum New To Java
    Replies: 1
    Last Post: 02-02-2014, 09:18 PM
  3. Parse int not working as expected
    By codyjava in forum New To Java
    Replies: 7
    Last Post: 11-03-2013, 08:16 PM
  4. Gridbaglayout not working as expected
    By berkeleybross in forum AWT / Swing
    Replies: 7
    Last Post: 07-06-2010, 01:11 PM
  5. Replies: 0
    Last Post: 09-04-2009, 08:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts