Grasshopper

algorithmic modeling for Rhino

Is there a way to find if a number is ODD or EVEN?

I mean if I have an output number and its 6, is there a function that could say: Yes its even then TRUE...

Thanks
Maurice

Views: 7179

Replies to This Discussion

Try this expression: x % 2 = 0

The "%" is "modulus" (there's also a modulus component...) which returns any remainder when x is divided by 2.

You'll have to watch out for zero which is neither even or odd, but with the above expression it comes out as an even number.

Thanks a lot TAZ
Thanks Taz, very helpful !

added to my favourite expressions!

 

Thank you!

 

DeDackel

Hi all,

I want to dig up this thread as I've got a question to expand on it. In the situation that Taz demonstrated below, you can get a True/False result for whether or not a number is even. I've been able to get this to work. Now I want to add +1 to all the odd (false) options. I'm having difficulty separating the data so that it is only adding to the odd numbers, then recombining the data to be in the same order, however with the relevant +1 so that all numbers are even values in the final list/tree?

Ive got a good handle of GH, but never had much use for If/Then statements - might this be a good time to figure them out? Something along the lines of "If number is odd, add 1. If number is even, add 0." (?)

Thanks,

-BC

Seems like I figured it out - I didn't realize there was actually a "modulus" component itself. Divide by 2, if there is a remainder, add it to the value! Comes out even every time.

RSS

About

Translate

Search

Photos

  • Add Photos
  • View All

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service