Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Addition of doubles (newbie question)

  1. #1
    Junior Member
    Join Date
    Apr 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Addition of doubles (newbie question)

    Hi. 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." I tried another forum, where someone treated me like I was a complete moron.

    I just don't understand what's happening, and if someone could explain it to me very simply, I'd greatly appreciate it.

    Thank you.


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Addition of doubles (newbie question)

    OP has been given a link and an explanation already and became abusive. Do not waste your time with this idiot.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/42781-double-subtraction.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting


  3. #3
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Addition of doubles (newbie question)

    Hello archyb,

    It would be worth doing some reading about repeating decimals. There is lot's of information online.

    Heres the gist of it though:

    A decimal representation of a real number is called a repeating decimal (or recurring decimal) if at some point it becomes periodic: there is some finite sequence of digits that is repeated indefinitely. For example, the decimal representation of 1/3 = 0.3333333... (spoken as "0.3 repeating", or "0.3 recurring") becomes periodic just after the decimal point, repeating the single-digit sequence "3" infinitely. A somewhat more complicated example is 3227/555 = 5.8144144144..., where the decimal representation becomes periodic at the second digit after the decimal point, repeating the sequence of digits "144" infinitely.
    The link provided in your other forum post is also useful:

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    I know it's an awful lot of reading and to a beginner it can be very daunting but even just a quick read will help you pick up some basic information that helps lay the foundation for the knowledge you need to acquire.

    I also suggest reading - Java theory and practice: Where's your point?

    You can round your answer to get the expected output:

    public class DoubleTest {
     
    	/**
    	 * JavaProgrammingForums.com
    	 */
    	public static void main(String[] args) {
     
    		double x = 45.32;
    		double y = 45.31;
    		double answer = (x - y);
     
    		answer = Math.round(answer*100)/100.0d;		
    		System.out.println(answer);
     
    	}
     
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Newbie Question on a Code - I don't know if this is Java
    By fresca in forum Java Theory & Questions
    Replies: 4
    Last Post: April 7th, 2011, 08:39 PM
  2. newbie question about Abstract methods
    By FailMouse in forum Java Theory & Questions
    Replies: 3
    Last Post: August 10th, 2010, 11:51 PM
  3. Looping Question (newbie)
    By xdrechsler in forum Loops & Control Statements
    Replies: 13
    Last Post: July 19th, 2010, 09:12 PM
  4. Reading doubles from file
    By fawx in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: October 24th, 2009, 11:57 PM