Monday, January 02, 2006

float num = 2/10;
println (num);

why the hell does that not work out to 0.2 in processing? (it tells me the answer is 0.0)

now try the same with...

float num = 2.0/10;
println (num);

and the damn thing works, 0.2! things like this are really annoying!

4 Comments:

Blogger Tom Carden said...

It's because 2 and 10 are integers, and all of the right hand side of the expression is evaluated before it gets assigned to thing on the right. So it doesn't know you wanted a float until it's already thrown away the non-whole bit of 2/10. Futhermore, it has to convert the answer into a float before assigning it.

When you use 2.0/10.0 it knows you're bothered about accuracy and precision, and that you're expecting a float as an answer, so it doesn't throw anything away and it won't have to convert things afterwards.

Ben Fry's explanation of the same thing might also help:
http://processing.org/faq/bugs.html#integers

Keep going!

2:49 pm  
Blogger Tom Carden said...

I mean, before it gets assigned to the thing on the left, of course. Oops :)

2:50 pm  
Anonymous Anonymous said...

Yeah.

What Tom said.

This kind of thing was frustrating for me at first - particularly coming from a sloppy language like ActionScript.

It gets easier...

2:13 am  
Blogger Alison said...

thanks, Im glad these things have explanations!

I just wish I already knew these things!

2:15 pm  

Post a Comment

<< Home