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 › println specific number
Page Index Toggle Pages: 1
println specific number (Read 702 times)
println specific number
Dec 7th, 2009, 3:26am
 
hi, i'm wondering how i can get to print only specific numbers, for example

if i = *.0 then println(i)

... meaning only print i, if i is a 1.0, 2.0, 3.0 etc. don't print i if 1.2, 3.5 etc. can't seem to figure out why the below code doesn't work

Code:

void draw() {
 for (float i = 0; i < 100.0; i += 0.1) {  
   if (i == round(i)) {
     println(i);
   }
 }
}


thanks in advance
Re: println specific number
Reply #1 - Dec 7th, 2009, 3:45am
 
Have you tried println(i) regardless (i.e. not in a condition).  This might answer your question:

Code:
void setup() {
 size(100,100);
 for (float i = 0; i < 100.0; i += 0.1) {  
   println(i);
   if (i%1 == 0) {
     println("round number: " + i);
   }
 }
}


Search for 'floating point inacuracy' to find the cause of the problem.  One solution is to work with whole numbers and then divide by 10/100/100 depending on how many decimal places you want...

Note also that using modulo is another way to do this test.
Re: println specific number
Reply #2 - Dec 7th, 2009, 3:50am
 
For example:

Code:
void setup() {
size(100,100);
for (float i = 0; i < 1000.0; i += 1) {
float number = i/10;
if (number%1 == 0) {
println("round number: ");
}
println(number);
}
}
Re: println specific number
Reply #3 - Dec 7th, 2009, 4:00am
 
thanks blindfish, using modulo works great. and it makes sense. exactly the solution i was looking for.

cheers!
Page Index Toggle Pages: 1