Results 1 to 7 of 7
  1. #1
    arvindmer is offline Member
    Join Date
    Jan 2009
    Posts
    3
    Rep Power
    0

    Red face double and float value in java

    I am new to java and using it for numerical calculations............
    Earlier I thought JAVA is great because of OOPs and it is advance than C or C++ ...........

    But recently I got the problem regarding the double values . For example this code ..

    Java Code:
    double x = 5.02;
    double y = 0.01;
    double z = x + y;
    System.out.println(z);
    It should print 5.03 but it prints 5.029999999999999
    This is nonsense ........... :eek: :eek: :eek: :eek:

    I have already written few codes in JAVA and now i think all my effort is just a waste, because like C i declared every variable as double, and in my calculation 0.0 and 5.029999999999999 are two different things and I need the accurate value.

    What should I do ??????

    What is the easy and accurate way ?? Should i stop using java or there is any way out ............ :confused:

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Quote Originally Posted by arvindmer View Post
    This is nonsense ........... :eek: :eek: :eek: :eek:
    ...
    i think all my effort is just a waste
    ...
    What should I do ??????
    ...
    Should i stop using java
    ...
    :confused:
    Whether you stop using Java is entirely up to you.

    Perhaps you could decide whether you are "confused" as denoted by the icon at the end, or "dogmatic" as suggested by the body of your post. ("Dogmatic" is like Angry/Mad, but a deeper shade of red and with little exclamation marks above its head.)

    If I came across a technology that was nonsense, inaccurate, and wasting my time I would most definitely stop using that technology. Wait! That sentence should read, if I were using a technology that I knew was nonsense, inaccurate and wasting my time...

    On the other hand if I was confused about that technology I would

    (a) Not assert things about which I was confused
    (b) Accept basic, common, documented behaviour of the technology like how System.out.println() works as being quite sensible behaviour even though I am unaware of the sense.
    (c) Find out about whatever concepts I was unsure of, like What Every Computer Scientist Should Know About Floating-Point Arithmetic.

    Those loud, frowny red faced guys are no fun at parties - and neither is the broody blue faced one sitting in the corner clutching his question mark. Better company are those like "Learning": yellow faced and smiling, but with an equal mixture of question marks and lightbulbs floating about above his head.
    Last edited by pbrockway2; 05-27-2009 at 09:36 PM.

  3. #3
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    That's the same for any program lang using float points, because the problem is floating point arithmetics.

    you can use interger instead:
    Java Code:
        long x = 502 ;
        long y = 1;
        long z = x + y;
        System.out.println(z/100.0);
    OR java.math.BigDecimal(String val)
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  4. #4
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    AB is correct in his last statement; you should use BigDecimal if you want exact rounding.

    float and double cannot exactly hold the values 5.02 and .1, because of the limitations of binary numbers. Therefore, you get tiny rounding errors.

    Java deals with this by providing a class called BigDecimal. BigDecimal takes some time to fully understand, but it gives you absolute control over the number of decimals and significant digits (not precisely the same thing, it turns out).

    Therefore, you have three choices:

    1. live with rounding errors in float and double
    2. find ways to always use int or long
    3. use BigDecimal

    All the mainstream modern languages are going to give you (at most) those three options.

  5. #5
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    Floating point is necessarily inexact. Every language and every platform has similar problems.

  6. #6
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    To the OP: if making your calculations to the 15 or so significant decimal figures of accuracy that double provides is sufficient for your purposes, then what you're dealing with is simply a printing issue. Decide how many decimal places you want to print to, and do e.g.:

    Java Code:
      String numberStr = String.format("%.3f", myNumber);
      System.out.println(numberStr);
    for (in this case) 3 decimal places of output.

    You may also be interested in a summary I wrote not so long ago on performing various Java string/text manipulation and formatting operations.
    Last edited by neilcoffey; 05-28-2009 at 07:56 AM. Reason: added related link

  7. #7
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    7

    Default

    To OP: I want to second what Neil said about String.format(). It's brought over from C, so the formatting codes are really cryptic, but it is very powerful and easy to use, once you figure out the codes. I started using it recently, cursed the codes for about an hour, and then was very pleased with the results.

Similar Threads

  1. [SOLVED] float in java and in mysql
    By Ch4os in forum NetBeans
    Replies: 1
    Last Post: 12-08-2008, 10:22 PM
  2. Float vs. Double
    By javanub in forum New To Java
    Replies: 1
    Last Post: 11-23-2008, 12:11 PM
  3. Float and double to binary
    By rosh72851 in forum Advanced Java
    Replies: 3
    Last Post: 09-24-2008, 06:29 AM
  4. Put float value to double
    By chamalsl in forum New To Java
    Replies: 3
    Last Post: 09-20-2008, 04:52 AM
  5. Replies: 2
    Last Post: 11-16-2007, 01:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •