Page 1 of 2 12 LastLast
Results 1 to 20 of 22
  1. #1
    bmbishop is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default Double subtraction

    In subtracting one double from another, namely 45.32 - 45.31, I get a result of 0.00999999999999801. According to all the math I've ever learned, the answer should be .01.

    I asked my professor about this, and his response was that "45.31 may be a repeating decimal." To which I responded, "I have no idea what that means."

    Help please!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

  3. #3
    bmbishop is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Thanks, but I'm in intro the programming and could use a little simpler explanation.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    2 / 3

    Write the answer to that EXACTLY in decimal.

  5. #5
    bmbishop is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    OK, I'm not trying to be difficult here, but I still don't get it!

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    the answer is 0.666666666666666666666666666666666666666666666666 66666666666666666666666666666666666666666666666666 66666666666666666666666666666666666666666666666666 6666666666666666666666666666666666666666666

    You cannot represent that value accurately in decimal. Guess what happens to some values in binary?

  7. #7
    bmbishop is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    I understand that analogy. But 4.11 - 4.10 is .01. The damn calculator says it's so. Can you explain it to me like I'm a complete idiot, which I'm not, but I am just not following you.

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    The damn calculator cheats. Since not all decimal values can be represented exactly in binary some inaccuracy creeps in. It is a known artifact of floating point arithmetic. All of which is explained in the article linked above if you had bothered to read it. If you did read it and didn't understand then read it again and again and again until you do understand because quite frankly anything I say here is just repeating what that article says. So why should I waste my time?

  9. #9
    bmbishop is offline Member
    Join Date
    Apr 2011
    Posts
    5
    Rep Power
    0

    Default

    Wow. You are an asshole. I am a new programmer seeking an answer that I can understand. Would anyone else like to help?

  10. #10
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Good luck with getting anyone else to explain it.

    You have been provided a link
    I have given you an explanation
    You used offensive language against me which violates the conditions you agreed to when joining this forum.

    I doubt very much anyone will waste their time helping an ungrateful idiot like you!

  11. #11
    Join Date
    Apr 2011
    Posts
    17
    Rep Power
    0

    Default

    Short answer: Since all numbers are represented in binary, some rounding errors occur when you use floating point arithmetic.

    Long answer: Read the article!

  12. #12
    RichersooN is offline Senior Member
    Join Date
    Jan 2011
    Location
    Rizal Province, Philippiines
    Posts
    167
    Rep Power
    0

    Default

    Your noob programmer cause you easily get angry, many problems still comes along the way if your going to continue to be as programmer.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default

    Quote Originally Posted by bmbishop View Post
    Wow. You are an asshole. I am a new programmer seeking an answer that I can understand. Would anyone else like to help?
    Abuse reported.

    db

  14. #14
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    *sigh*
    Come on people, let's help him out here. He's still new and probably doesn't even know what binary means or how it works. Why turn him off to programming now?

    @bmbishop
    As you probably know, computers use a binary system (1's and 0's) to store data, such as values and instructions. Humans use a decimal system. So like 2/3 in decimal is an irrational number, 45.31 is an irrational number in binary, aka, there is no exact representation of 45.31 as 1's and 0's.

    Hope that helps :)

    (but really, please do not use offensive language, even if you are angry)

  15. #15
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Don't waste your time. OP has moved on to pollute another forum.

    Addition of doubles (newbie question)

  16. #16
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Well he did get mad that you were treating him as if he understood this.

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    No I treated him as a lazy person who expected us to spoonfeed him the answer instead of reading the article. I also didn't apreciate his attitude that because he didn't understand then somehow that was my fault. If I attended a lecture on Quantum Mechanics by Stephen Hawking it would all go over my head. That is my fault not Stephen's.

  18. #18
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    But most likely he didn't understand the article because of some potentially confusing words.
    Thanks, but I'm in intro the programming and could use a little simpler explanation.

  19. #19
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    I did provide a simpler explanation and the idiot still not understand nor appreciate it.

    I'm also done arguing with you!

  20. #20
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Location
    Atlanta, Georgia, US
    Posts
    396
    Rep Power
    4

    Default

    Grrrrrr...

    I just felt a little pity for the guy, as he is a n00b, ignoring the offensive language part.
    Last edited by ra4king; 04-21-2011 at 04:56 AM.

Page 1 of 2 12 LastLast

Similar Threads

  1. unexpected type on a subtraction sign?
    By JanieRae in forum New To Java
    Replies: 5
    Last Post: 04-22-2010, 04:09 AM
  2. double a * double b = weird output
    By GPB in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 10:40 AM
  3. date subtraction
    By silversurfer2in in forum New To Java
    Replies: 6
    Last Post: 02-12-2010, 02:51 PM
  4. Replies: 5
    Last Post: 03-21-2009, 09:32 PM
  5. Double.valueOf() vs Double.parseDouble()
    By greenbean in forum New To Java
    Replies: 10
    Last Post: 01-12-2009, 08:39 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
  •