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 4 of 4

Thread: Java Math Curiosity

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Math Curiosity

    I'm currently learning lava and i have been playing around with some simple tasks.


    Code:
    public class Main {
    	public static void main(String args[]){
     
    		int counter = 0;
    		double number = 0;
     
    		while (counter <= 10){
     
    			number += .2;
    			System.out.println(number);
    			counter++;
     
    		}
     
    	}
    }

    Output:
    0.2
    0.4
    0.6000000000000001
    0.8
    1.0
    1.2
    1.4
    1.5999999999999999
    1.7999999999999998
    1.9999999999999998
    2.1999999999999997

    Why does the output have uneven values?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Math Curiosity

    Floating point numbers can not be represented exactly in a computer.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Math Curiosity

    What could i use to avoid this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Java Math Curiosity

    That is how floating point variables work.
    If you want to see different values displayed when printing floating point variables, use a formatting class like the DecimalFormat class or the printf() method with formatting Strings.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Question about math.pow and math.sqrt
    By ggx7 in forum Java Theory & Questions
    Replies: 7
    Last Post: March 7th, 2014, 12:51 PM
  2. Out of pure joy and curiosity...and whey ahead of myself?
    By einar123 in forum Java Theory & Questions
    Replies: 4
    Last Post: August 13th, 2013, 11:37 AM
  3. Simple question, curiosity, not really important
    By Azrhei in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 19th, 2012, 05:22 PM
  4. [SOLVED] Help with Math.tan and Math.atan
    By Dr.Code in forum Algorithms & Recursion
    Replies: 6
    Last Post: July 2nd, 2012, 05:54 AM
  5. Confusion with Math.toDegrees() and Math.toRadians(). Please help.
    By marksquall in forum Java Theory & Questions
    Replies: 3
    Last Post: June 23rd, 2011, 01:28 AM