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 & HelpPrograms › 1st Array demo has curious overflow
Page Index Toggle Pages: 1
1st Array demo has curious overflow (Read 283 times)
1st Array demo has curious overflow
Jan 19th, 2009, 7:12pm
 
"Examples/Basics/Arrays/Array/Array.pde" has an overflow? at the middle index. For example, run this snippet and look at index 200 (because the width of the window is 400).

float[] coswave;

void setup()
{
 size(400, 300);
 coswave = new float[width];

 for(int i = 0; i < width; ++i)
 {
   float amount = map(i,        // number to map
                      0, width, // map from this range
                      0, PI);   // to this range

   coswave[i] = abs(cos(amount));

   if(i == width/2)
   {    
          println("**********************************");
          println("amount = " + amount);
          println("cos(" + amount + ") = " + cos(amount));
          println("coswave[" + i + "] = " + coswave[i]);
          println("**********************************");    
   }
   else
     println("coswave[" + i + "] = " + coswave[i]);
 }
}

Now change the width to 399, no overflow. I'm curious as to why...

Regards.
Re: 1st Array demo has curious overflow
Reply #1 - Jan 19th, 2009, 10:20pm
 
I have no overflow, neither with the example nor the given code. And I see no reason to have one.
Re: 1st Array demo has curious overflow
Reply #2 - Jan 19th, 2009, 10:48pm
 
perhaps overflow is a poor choice of wording.

basically, on index 200 i get:

amount = 1.5707964 // 90 degrees
cos(amount) = -4.371139E-8

and in google calc it's:
cos(1.5707964) = -7.32051035 × 10-8
Re: 1st Array demo has curious overflow
Reply #3 - Jan 19th, 2009, 10:50pm
 
That's not an overflow, what you're getting is an answer that's almost (but not quite) 0, since 1.5707964 is almost, but not quite 90' HALF_PI will give you the closest approximation to 90' in radians.
Re: 1st Array demo has curious overflow
Reply #4 - Jan 19th, 2009, 11:24pm
 
sorry, all confused. i was thinking of an int overflow wrapping and i forgot about exponents, shift the decimal over 8 places and u've got a really big 0

thx
Page Index Toggle Pages: 1