Is there a way to input a number and get two separate outputs if it’s even or odd?
You can divide the value by 2 and then multiply by 2. If the number is the same, it’s even. If it’s not the same, it’s odd. Why does this works? Because odd numbers divided by 2 will be rounded up.
For example: 15
15/2=8 (7.5)
8*2=16
16=/=15 so it’s odd
Thanks
Another approach is to use the modulus operator (’%’) in an expression to divide by 2 and return the remainder, like this: A % 2
If the result is 0 then A is even, otherwise A is odd.
@grazer Oh wow does that works too?! So it’s just like in real programming languages. I was looking for that in the behavior documentation, but I couldn’t find it. Would be nice if you add it though.
It’s there: http://flowlab.io/behavior_handbook/#logic-math-expression but you may have overlooked it
Oh, magic
How do I put this into my code?
@THECAM
Its best to make a new discussion then to comment on these old discussions.
Those go into the Expression block behavior, and you can see all the different mathematical expressions in the link Grazer provided a few comments above.
Another nice one is the ‘ternary operator’, basically a one line ‘If…Then…Else’ function.
The syntax is:
` ( condition )? value for true : value for false`
Let’s say you want 2 different speed outputs for your Even/Odd scenario, for example 5 and -5, it would look like this:
(A is the connected input)
This can save you sometimes a few blocks
P.S. is there a better place to collect Flowlab Tips&Tricks? Like a wiki or so? Such a hassle to search through the forums, so my apology if this has been posted somewhere else already
Other than hoping to see if theres a tutorial or info in the forum for it, only thing else is the Handbook Grazer tries to be descriptive about. http://flowlab.io/behavior_handbook/#logic-math-expression
Flowlab isnt too publicly known for a wiki or sources outside of Flowlab.
Other than maybe finding a tutorial on Youtube, the Forums is your best bet.
hi (chews loudly)