Skip to main content
8-Gravel
October 25, 2016
Solved

Relations statement to identify even or odd numbers.

  • October 25, 2016
  • 2 replies
  • 3780 views

I am trying to write a relation that goes something like this...

IF

Parameter = even number

dimension_x = .05

ELSE

dimension_x=0

ENDIF

I cannot figure out how to get Creo to tell if the Parameter (which is a integer type) is an odd or even number.

Any thoughts would be greatly appreciated.

    Best answer by KenFarley

    To do "int" you either have to use "floor" or "ceil".

    The easier thing to do is use the "MOD(numerator, divisor)" function. To find out if a number is odd or even, it's something like

    IF MOD ( number, 2 ) == 0

      /* Do this stuff if even.

    ELSE

      /* Do this stuff if odd.

    ENDIF

    2 replies

    13-Aquamarine
    October 26, 2016

    Without checking exactly what the syntax would be in Relations, the usual approach is:

    if( x/2 = int(x/2) ) then it's even

    Is there an int() in Relations?

    KenFarley21-Topaz IIAnswer
    21-Topaz II
    October 26, 2016

    To do "int" you either have to use "floor" or "ceil".

    The easier thing to do is use the "MOD(numerator, divisor)" function. To find out if a number is odd or even, it's something like

    IF MOD ( number, 2 ) == 0

      /* Do this stuff if even.

    ELSE

      /* Do this stuff if odd.

    ENDIF

    13-Aquamarine
    October 26, 2016

    Ah yes, that's a little neater.

    I believe INT is usually equivalent to FLOOR (i.e. 'take the INTeger part', not 'round to nearest integer').

    21-Topaz II
    October 26, 2016

    Yeah, to round things I usually "cheat" by doing something like floor ( number + 0.5 ). It's kind of weird the functions that are included and more importantly not included. I'd love to see a "real-to-string" function, so I wouldn't have to use multiple lines of code to do the same thing. It would also be kind of cool if there were some sort of ability to define our own functions. That would really make things simpler for me.