Results 1 to 4 of 4
  1. #1
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default My double variable adds itself 0.032/33 and 0.064/65

    Hello there. I'm having such an strange problem with a double variable which increases 0.032 and 0.064 by itself.
    Each time I click on a JButton, the variable has to increase in 0.01, starting on 0. The thing is that after some clicks, it increases itself 0.032 or 0.064. It's ever at the same number of clicks; for example, when the number is 0.499 it goes to 0.564. Ever the same clicks. Of course, there's nothing in the (long)code that says it to add that numbers. What's happening?
    Another problem that I have is that the same variable usually adds 0.011, or 0.0101, even if I say to it to add 0.010000000000000. How can I solve it?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default

    Try to create a small example that compiles and shows the behaviour you just described; without it there's not much we can do.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default

    Another problem that I have is that the same variable usually adds 0.011, or 0.0101, even if I say to it to add 0.010000000000000. How can I solve it?
    BigDecimal (Java Platform SE 6)
    What Every Computer Scientist Should Know About Floating-Point Arithmetic

    db

  4. #4
    Josep_16's Avatar
    Josep_16 is offline Member
    Join Date
    Mar 2011
    Posts
    40
    Rep Power
    0

    Default

    Ok I found where the problem came from. It was from the method that rounded the doubles. I don't know why it added 0.32 and 0.64 to the double until it arrived to 1.

    It was this one:

    Java Code:
    public double round(double number, int decimals)
      {
      	double result;
      	BigDecimal res;
    
      	res = new BigDecimal(number).setScale(decimals, BigDecimal.ROUND_UP);
      	result = res.doubleValue();
      	return result;
      }
    I just found another rounding method that works fine. So, the problem is solved. Thanks.

Similar Threads

  1. Alternating Transparency Based on Double Variable
    By YellowPainting in forum Java 2D
    Replies: 2
    Last Post: 11-05-2010, 08:59 PM
  2. Double Variable Class
    By hypes057 in forum New To Java
    Replies: 9
    Last Post: 09-06-2009, 02:06 PM
  3. variable double problem
    By DarkoDrljaca in forum New To Java
    Replies: 5
    Last Post: 03-02-2009, 08:00 AM
  4. Replies: 3
    Last Post: 11-24-2008, 07:01 PM
  5. Replies: 3
    Last Post: 01-19-2008, 08:38 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
  •