We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Quick Counting Question
Page Index Toggle Pages: 1
Quick Counting Question (Read 772 times)
Quick Counting Question
May 3rd, 2008, 4:54am
 
This code does what I want but I don't why the numbers are so weird. Is there anyway to count:

-0
-0.01
-0.02
-0.03
-0.04
-0.05
-0.06
-0.07

instead of

-0
-0.01
-0.02
-0.03
-0.04
-0.04999997
-0.05999995
-0.06999999
-0.07999999


Quote:


float count = 0;
int hold = 0;
void draw(){


 if (hold==0){
   count-=.01;
 }

 if(count==-2.0099986){
   hold = 1;
 }
 if (hold==1){
   count+=.01;
 }
 if(count>=0){
   hold=0;
 }
 println(count);

}



Re: Quick Counting Question
Reply #1 - May 3rd, 2008, 10:04am
 
Unfortunately floating point numbers are inherently inaccurate due to how they're stored. You could use a "double" instead of a float for more accuracy, but there'll be a point even with those where you can't exactly store the number you want.
Re: Quick Counting Question
Reply #2 - May 3rd, 2008, 9:36pm
 
...which is not to say you're out of luck, though.  Depending on your needs, you could either use integers (just interpret them as being shifted by two decimal places - this requires care when doing multiplications or divisions) or use Java's BigDecimal class (see http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html for the details on that).

Neither method is ideal, as you can no longer just write out your math the natural way, but this is an inherent limitation of Java, caused by the fact that Sun thinks Java programmers en masse are too stupid and foolish to handle such a subtle language feature as operator overloading.  Then again, they might be right (see the many abuses of operator overloading in C++ if you doubt this), so I shouldn't complain too much...
Re: Quick Counting Question
Reply #3 - May 3rd, 2008, 11:05pm
 
Eric Jordan wrote on May 3rd, 2008, 9:36pm:
...which is not to say you're out of luck, though.  Depending on your needs, you could either use integers (just interpret them as being shifted by two decimal places - this requires care when doing multiplications or divisions) or use Java's BigDecimal class (see http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigDecimal.html for the details on that).

Neither method is ideal, as you can no longer just write out your math the natural way, but this is an inherent limitation of Java, caused by the fact that Sun thinks Java programmers en masse are too stupid and foolish to handle such a subtle language feature as operator overloading.  Then again, they might be right (see the many abuses of operator overloading in C++ if you doubt this), so I shouldn't complain too much...


Big Decimal sounds like fun.

I really have no idea how to use it. If you you could post a short example it would be much appreciated.
Re: Quick Counting Question
Reply #4 - May 4th, 2008, 3:24am
 
Here's a simple example, to count down by .1:

Code:

import java.math.BigDecimal;

BigDecimal bd = new BigDecimal(0.0);
// To get .1 exactly, pass it as a string to the BigDecimal constructor
BigDecimal oneTenth = new BigDecimal(".1");

for (int i=0; i<100; ++i) {
bd = bd.subtract(oneTenth);
println(bd);
}

// Divide result by 40 - we have to set the number of decimal places to 2 to
// allow for exact computation, which is the second argument to divide()
println(bd.divide(new BigDecimal("40"),2,BigDecimal.ROUND_UNNECESSARY));


To do more, I'd suggest you read the BigDecimal docs - there are a lot of subtleties when it comes to setting scale properly under division/multiplication, and handling rounding.  But this class is very useful, especially if you ever do any sort of financial programming, where floating point error means actual missing money.  The following snippet will indicate that even though floating point calculations are useful, for real world amounts of money (to a company or rich person, at least), they are totally insufficient:

Code:

float number = 0.0;

while( number != (++number) ){
//not as infinite a loop as you might think...
}

println("Floating point precision is less than 1 at "+number);
Re: Quick Counting Question
Reply #5 - May 4th, 2008, 7:45am
 
Thanks!

I finally got it to do what I wanted with compareTo.

Although I can use a normal if statement as long as they are both big decimals on both sides? right?

Is there really no way to convert the big decimal back into a normal processing float? I thought I saw something to get it back into an int but not a float.
Re: Quick Counting Question
Reply #6 - May 4th, 2008, 10:35am
 
triscuit wrote on May 4th, 2008, 7:45am:
Although I can use a normal if statement as long as they are both big decimals on both sides right

Is there really no way to convert the big decimal back into a normal processing float I thought I saw something to get it back into an int but not a float.


No, you can't use == even if they're both BigDecimals, in Java "==" on objects just compares to see if they're the same absolute object, not that the objects have the same value.

You can get a float value out with myBigDecimal.floatValue();
Page Index Toggle Pages: 1