Is there a clean way to determine if a number is even?
I can think of some really easy ways that don't invlove 100 ORs in my logic, but they all use modulus.
Any really simple ways?
Is there a clean way to determine if a number is even?
I can think of some really easy ways that don't invlove 100 ORs in my logic, but they all use modulus.
Any really simple ways?
One option would be to setup a group output and set it equal to the number you want to check. Then just check the status of the first output in the group. If it is off then the number is even if it is on then the number is odd.
I actually have a program module for this. It uses a quirk of how KRL handles divsion remainders between Integer and Real variables.
GLOBAL DEFFCT _EvenOrOdd TestEvenOrOdd (_Integer :IN)
;FOLD HEADER
; checks whether an integer variable is Odd or Even
;ENDFOLD HEADER
;FOLD EXECUTABLE
DECL INT _Integer
IF ((_Integer / 2) <> (_Integer / 2.0)) THEN
RETURN #Odd
ELSE
RETURN #Even
ENDIF
;ENDFOLD EXECUTABLE
ENDFCT
Display More
I have the ENUM type variable EvenOrOdd declared globally in the .DAT file of the module:
I like it a lot. Thanks! ALMOST as clean as (_Integer % 2 == 0)