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 › divide lesser number by greater
Page Index Toggle Pages: 1
divide lesser number by greater (Read 667 times)
divide lesser number by greater
Jan 19th, 2010, 3:39pm
 
I'm probably being an idiot here but in hacky old actionscript I can do..

var t : Number = 1/2;

which equals 0.5.

I can't seem to see an easy way of doing this in processing.  It rounds down to 0.

Sure there must be a way.

Cheers

Ben


Re: divide lesser number by greater
Reply #1 - Jan 19th, 2010, 4:00pm
 
Code:

// Results in 0.0
float f = 1/2;

//Results in 0.5
float f = 1.0/2;

In the first case Processingattempts to evaluate "1/2" it will consider this integer division (divide and discard fractional part) so the result will be an integer.

In the second evaluation of "1.0/2" is a mixed type calculation so Processiong will use the precision of the more 'accurate' type found in this case float, so performs a floating point division thus resulting in 0.5
Re: divide lesser number by greater
Reply #2 - Jan 19th, 2010, 4:27pm
 
Ahh, yes I remember now. Thanks so much.

Page Index Toggle Pages: 1