Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Odd remainder for a subtraction  (Read 3755 times)

0 Members and 1 Guest are viewing this topic.

Dilbert

    Topic Starter
  • Moderator


  • Egghead

  • Welcome to ComputerHope!
  • Thanked: 44
    Odd remainder for a subtraction
    « on: July 20, 2006, 08:30:13 PM »
    Hello, I'm on Visual Basic .NET 2003.

    Before I had to format my PC a while back, I had this VB.NET program that was simple enough: Input a USD amount into a text box, click the button, and watch it divide the total into $20's, $10's, etc. all the way down to the pennies. It was never all that accurate, so today I decided to re-do the program, and shoot for 100% accuracy.

    It took me a grand total of an hour and a half to write code to error-check the input textbox, then Do Until loop the total until I had the figures listed. (Basically, the actual code to sort out dollar amounts was nothing but Do loops the whole way down. I can't remember how I did it before, but it wasn't this, nor did it have any accuracy as I said.)

    After that, I wanted to extrapolate: What if someone didn't want, for example, $20 bills listed? So I wrote a not-so-elaborate method of allowing the user to choose each denomintion to Include it or Exclude it.

    Of course, this meant that there might be a remainder, due to the small change being excluded, or if the user put in three decimal places. So, I put in a system to display this remainder. Easy, right? Yes.

    BUT...

    If the user inputs a number with three decimal places, and that last decimal happens to be a 1, I get kooky remainders.

    Here is a thumbnail (click for larger version) of the program in progress with no remainders. Everything works properly. (The progress bar was for debugging purposes, to see just where in the program I'd have a crash. I left it in as a "feature", but it's really instantaneous unless I'm dealing with large figures.)



    Here's the same program, run with a three-digit decimal which caused a remainder. Perfectly normal.



    Next, a three-digit decimal with 1 as the third digit. The remainder should be 0.009, but...



    Attached is a text file with the source code, sans the Form Generated code. I'm hoping it's obvious where the flaw is, but I can't find it. :-/
    "The geek shall inherit the Earth."

    Rob Pomeroy



      Prodigy

    • Systems Architect
    • Thanked: 124
      • Me
    • Experience: Expert
    • OS: Other
    Re: Odd remainder for a subtraction
    « Reply #1 on: July 21, 2006, 04:14:21 AM »
    I doubt that there's anything particularly wrong with your programming; just your approach.  Remember that all numbers must be capable of representation in binary on a computer.  It is very common to have these kinds of rounding problems due to the conversion between decimal fractions and binary "fractions".  Does VB.NET have a specific precision arithmetic library/functions?  You would need to use those instead.

    Alternatively, cast your intial input to a string, and extract everything after the second decimal place.  E.g. input = 192.5678; remainder string = 0.00 concatenated to '78'.

    Make sense?
    Only able to visit the forums sporadically, sorry.

    Geek & Dummy - honest news, reviews and howtos

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Odd remainder for a subtraction
    « Reply #2 on: July 21, 2006, 05:38:57 AM »
    On the dollar side everything appears correct. The problem is that the program handles cents as a fractional value of dollars instead of integer values of a different unit.

    Example $234.78:

    You do you calculations for 234 with a loop using integer paper denominations (20, 10, 5, 1)

    For the .78 consider that you have 78 (integer) cents (not a fractional portion of $1) and do the same loop with coin denominations (quarter=25, dime=10, nickel=5, penny=1)

    Just another approach. 8-)

    When did we start using thousandths for USD?

    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Rob Pomeroy



      Prodigy

    • Systems Architect
    • Thanked: 124
      • Me
    • Experience: Expert
    • OS: Other
    Re: Odd remainder for a subtraction
    « Reply #3 on: July 21, 2006, 05:49:01 AM »
    Quote
    When did we start using thousandths for USD?
    Ask any stockbroker.  ;)
    Only able to visit the forums sporadically, sorry.

    Geek & Dummy - honest news, reviews and howtos

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: Odd remainder for a subtraction
    « Reply #4 on: July 21, 2006, 06:01:30 AM »
    The program title is "ChangeMaker". If you find the odd .005 piece please mail in care of CH.

     ;D
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dilbert

      Topic Starter
    • Moderator


    • Egghead

    • Welcome to ComputerHope!
    • Thanked: 44
      Re: Odd remainder for a subtraction
      « Reply #5 on: July 21, 2006, 10:22:49 AM »
      Sidewinder, your solution worked perfectly. Now, when the third decimal is a 1, I get the proper 0.001 remainer (my assumption that the result should be 0.009 was based on a lack of proper sleep; I came to CH in haste to correct this error in my thinking -- too late!).

      I actually don't use more than two decimal points, and thanks to this advice I have an easy way to restrict it. Two textboxes, so I don't have to multiply the total by 100 to get an integer subtraction... I can limit that decimal box to two characters...

      But I can still have remainders, if the end user chooses to, for example, exclude pennies with a total that would contain them. No big deal, though; I can still show it as normal. :)

      Oh, and I don't think my software can actually make change as in create it, but I'll be happy to ship in that 0.00001 cent leftover. :D
      "The geek shall inherit the Earth."