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: How Java handles float ?

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

    Question How Java handles float ?

    Hi,

    I have been with Java for 6 months, yet I got this doubt from a friend... I am posting on behalf of her...

    Going directly to the code:

    System.out.println(1e-12==Math.pow(10,-12));
    System.out.println(1e-5==Math.pow(10,-5));

    O/P for the lines of code is:
    true
    false

    I tried:
    System.out.println(1e-12);
    System.out.println(1e-5);
    System.out.println(Math.pow(10,-5));
    System.out.println((float)Math.pow(10,-5));

    O/P
    1.0E-12
    1.0E-5
    9.999999999999999E-6
    1.0E-5


    Question is,
    I don't know why the O/P of "Math.pow(10,-5)" was "9.999999999999999E-6" but when it was cast into float, it became to "1.0E-5"?

    Moreover, it's stranger that, the result of
    System.out.println(Math.pow(10,-4)); and System.out.println(Math.pow(10,-6)); are natural, only "Math.pow(10,-5)" has an approximate result? Why?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,140
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: How Java handles float ?

    One of the problems with floating point math in a binary world.
    I've seen a nice description of the problem but don't have a link to it.
    Ask a search engine about problems programming with floating point
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    939
    Thanks
    6
    Thanked 202 Times in 178 Posts

    Default Re: How Java handles float ?

    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    Not only numeric quantities expressed in a binary format, but any formal system trying to name (distinguish) an infinite number of things is going to strike a problem sooner or later. Java tries to implement a common standard across its various platforms - successfully as far as I know.

  4. The Following User Says Thank You to pbrockway2 For This Useful Post:

    Norm (April 27th, 2013)

Similar Threads

  1. Problems getting floating point number to show in GUI...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 18th, 2012, 11:48 PM
  2. regarding floating point
    By deependeroracle in forum Java Theory & Questions
    Replies: 3
    Last Post: January 10th, 2012, 10:18 AM
  3. Replies: 2
    Last Post: November 30th, 2011, 06:35 PM
  4. Declaring floating point number depending on an if statement
    By Keys767 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 11th, 2011, 01:50 PM
  5. update query is firing first then insert query
    By salmondavid88 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 8th, 2011, 10:15 AM

Tags for this Thread