Odd or even numbers
                
                    pavpav                
                
                     ✭                
            
                        
                
                                    
                                  in RobotStudio             
            
                    Hi
                I would like to check if a value is odd or even, does anyone have a good solution for this in RAPID?
I know its possible to divide a value by 2 and count the amount of decimals, but i cannot find a function that can count decimals.
Thank you
                0  
            Comments
- 
            
FUNC num OddEven(num x)
VAR num nAux;
nAux := Trunc(x) MOD 2;
TEST nAux
CASE 0:
! Value is even
RETURN 2;
CASE 1:
! Value is odd
RETURN 1;
ENDTEST
ENDFUNC
Something like this?
Benteler Maschinenbau CZ1 - 
            ThxI read about MOD and the way i see it is that it counts the amount of numbers a value consists of?11/5 is 2,2 so MOD will be 2 because there is two numbers?0
 - 
            You clearly don't understand MOD yet, but you know it's function.This page contains an explanation of the mathematics behind the MOD (A.K.A. Modulo) instruction:Hope this helps1
 - 
            Ah thx the remainder after a quotient0
 - 
            One way is to check first bit of the number. If the bit is 1 than the number is odd, if it's 0, the number is even. Example code:
!I converted the num to dnum, than it's possible to check higher value number than 0...255.FUNC bool checkIsNumberOddOrNot(num inputNum)VAR dnum inputDnum;inputDnum:=numToDnum(inputNum);IF BitCheckDnum(inputDnum, 1) = TRUE THEN!first bit is true, it's an odd numberRETURN TRUE;ELSE!first bit is false, it's not an odd numberRETURN FALSE;ENDIFENDFUNC
Br,
Kristo1 
Categories
- All Categories
 - 5.6K RobotStudio
 - 401 UpFeed
 - 21 Tutorials
 - 15 RobotApps
 - 306 PowerPacs
 - 407 RobotStudio S4
 - 1.8K Developer Tools
 - 250 ScreenMaker
 - 2.9K Robot Controller
 - 363 IRC5
 - 81 OmniCore
 - 8 RCS (Realistic Controller Simulation)
 - 853 RAPID Programming
 - 31 AppStudio
 - 4 RobotStudio AR Viewer
 - 19 Wizard Easy Programming
 - 110 Collaborative Robots
 - 5 Job listings
 
