Skip to main content
8-Gravel
Oct 25, 2016 8:42 PM UTC
Solved

Relations statement to identify even or odd numbers.

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.

    KenFarley
    KenFarleyAnswer
    21-Topaz II
    Oct 26, 2016 10:59 AM UTC

    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

    5 replies

    13-Aquamarine
    Oct 26, 2016 8:16 AM UTC

    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?

    Translate
    KenFarley
    KenFarley21-Topaz IIAnswer
    21-Topaz II
    Oct 26, 2016 10:59 AM UTC

    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

    Translate
    13-Aquamarine
    Oct 26, 2016 11:03 AM UTC

    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').

    Translate
    KenFarley
    21-Topaz II
    Oct 26, 2016 12:31 PM UTC

    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.

    Translate