Results 1 to 4 of 4
- 08-26-2011, 09:34 PM #1
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?
- 08-26-2011, 09:47 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 08-26-2011, 10:53 PM #3
BigDecimal (Java Platform SE 6)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?
What Every Computer Scientist Should Know About Floating-Point Arithmetic
db
- 08-27-2011, 12:55 AM #4
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:
I just found another rounding method that works fine. So, the problem is solved. Thanks.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; }
Similar Threads
-
Alternating Transparency Based on Double Variable
By YellowPainting in forum Java 2DReplies: 2Last Post: 11-05-2010, 08:59 PM -
Double Variable Class
By hypes057 in forum New To JavaReplies: 9Last Post: 09-06-2009, 02:06 PM -
variable double problem
By DarkoDrljaca in forum New To JavaReplies: 5Last Post: 03-02-2009, 08:00 AM -
Rediculous problem?! Assigning a fraction to a double variable
By Tzoshirzup in forum New To JavaReplies: 3Last Post: 11-24-2008, 07:01 PM -
why does a variable be bracketted in double @val@!Thank you
By ibmzz in forum Advanced JavaReplies: 3Last Post: 01-19-2008, 08:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks