Home AMX User Forum NetLinx Studio

Modulo operation

Dear All,
I found an issue on code compiler when tried to know if a value is integer or decimal. For example:

Define_Variable

Float Value


Define_Event

Button_Event[dvTP,1]
{
Push :
{
Stack_Var float nDecimalCheck

Value = Value + 0.5 //raise the value by 0.5 step
nDecimalCheck = Value mod 1 //modulo operation

if (nDecimalCheck = 0 ) // means integer
// do something
if (nDecimalCheck = 0.5 ) // means decimal
// do something else
}
}

The above code does not work. In every "value", the nDecimalCheck equals to 0 ( integer ) even if the "value" is decimal ( e.g. 13.5, 13.5 mod 1 = 0.5 )
I tried with a fixed "value" number:

nDecimalCheck = 13.5 mod 1

but the compiler replied with the below error :

C10524 : Cannot perform constant modulus on type [7]

Do you know if float numbers can work with modulo operation ? I'm afraid that since "Value mod 1" always return 0, the compiler considers the "value" as integer even if it is not ( e.g. 65,5 ).
If so, how can we check if a number is integer or decimal ?
It is very disappointing if a master cannot make such a math operation.

Thanks,
George

Comments

  • zack.boydzack.boyd Posts: 94
    There's a few things wrong here.

    1 - I think you have a misunderstanding of how a modulo works. Modulo gives a remainder after division. So anything (modulo) 1 will return zero. There is never a remainder when dividing by 1. It's also only intended to be used on whole numbers.

    2 - Your IF statement is wrong. You should be evaluating with two equal signs.

    3 - Here's a way to accomplish what you'd like:
    FLOAT fDecimal
    SINTEGER nWholeNumber
    
    fDecimal = 4.5
    nWholeNumber = TYPE_CAST(fDecimal)
    
    IF(fDecimal == nWholeNumber){
        //Whole Number
    }
    ELSE{
        //Decimal
    }
    
  • richardhermanrichardherman Posts: 387
    zack.boyd wrote: »
    There's a few things wrong here.

    2 - Your IF statement is wrong. You should be evaluating with two equal signs.

    That's not true for NetLinx. It is probably best practice to use two equal signs for a comparison, but it's optional. both 1 and 2 equal signs will work here.
  • viningvining Posts: 4,368
    You could also multiply fDecimal by 10 and then modulo by 5 but the method mention earlier is probably more efficient.
  • zack.boydzack.boyd Posts: 94

    That's not true for NetLinx. It is probably best practice to use two equal signs for a comparison, but it's optional. both 1 and 2 equal signs will work here.

    Huh. Guess I told myself that at some point and just kept believing it. I had to test it just now to prove it to myself and it does work. For some reason I thought it was an assignment and evaluated to 1. Maybe I have languages confused(again)...

    Thanks for the schoolin'
Sign In or Register to comment.