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.5K RobotStudio
- 395 UpFeed
- 18 Tutorials
- 13 RobotApps
- 297 PowerPacs
- 405 RobotStudio S4
- 1.8K Developer Tools
- 249 ScreenMaker
- 2.7K Robot Controller
- 310 IRC5
- 59 OmniCore
- 7 RCS (Realistic Controller Simulation)
- 785 RAPID Programming
- AppStudio
- 3 RobotStudio AR Viewer
- 18 Wizard Easy Programming
- 105 Collaborative Robots
- 4 Job listings