Can someone explain Math.fmod() vs using a % operator?

Can someone explain Math.fmod() vs using a % operator?

jonathan_s3
Not applicable
1,486 Views
9 Replies
Message 1 of 10

Can someone explain Math.fmod() vs using a % operator?

jonathan_s3
Not applicable

[ FlexSim 20.2.3 ]

Working on a decision point in Flexscript I wanted to use a modulo math operator like I can within Excel. I started tinkering with this:

if (current.labels.assert("roundRobinVal", 1).value % 2) == 0) {...then do stuff...}

But no matter what I did the % did not seem to behave like I anticipated.

I then found Math.fmod() and made this:

if (Math.fmod(current.labels.assert("roundRobinVal", 1).value, 2) == 0) {...then do stuff...}

and it worked perfectly.

I am incrementing the roundRobinVal after the if().

I also don't understand the ", 1" in the latter portion of the .assert().

Accepted solutions (1)
1,487 Views
9 Replies
Replies (9)
Message 2 of 10

jason_lightfoot_adsk
Autodesk
Autodesk

The 1 is the initial value of the label in the assert -so the value it will take if it didn't exist.

The % operator should work exactly as fmod - can you post an example?

Message 3 of 10

jonathan_s3
Not applicable

This works like I intended:

if (Math.fmod(current.labels.assert("roundRobinVal", 1).value, 2) == 0)

{newDest = current.outObjects[1];}

else

{newDest = current.outObjects[2];}

current.labels.assert("roundRobinVal", 1).value++;


This did not:

if ((current.labels.assert("roundRobinVal", 1).value % 2) == 0)

{newDest = current.outObjects[1];}

else

{newDest = current.outObjects[2];}

current.labels.assert("roundRobinVal", 1).value++;

0 Likes
Message 4 of 10

jason_lightfoot_adsk
Autodesk
Autodesk

I meant in a model where we can step through and see if something funky is happening, since the description you gave is perfect and should work as far as I can tell.

You could try stepping through with the debugger and put expressions in the watch list to see what everything is evaluating to.

Message 5 of 10

jonathan_s3
Not applicable

I've already implemented and saved over using Math.fmod() and it's working now.

What it seemed to be doing is that the even values never evaluated to a 0 remainder with "% 2".

As far as the debugger I haven't used that yet. I'm not sure how to put in breakpoints and watch specific variables in flexsim.

0 Likes
Message 6 of 10

jason_lightfoot_adsk
Autodesk
Autodesk

Understood. I really recommend the debugger - it takes out the mystery of many situatons and is pretty simple to use. If you want help on that there's the documentation here or you can reach out for support.

Message 7 of 10

jonathan_s3
Not applicable

While I'm thinking about it, can you step me through the logic of:

current.outObjects[(current.labels.assert("roundRobinVal", 1).value++ - 1) % current.outObjects.length + 1];

as in does it evaluate the .value++ and -1 before or after the %.

The way it reads left to right it would read the roundRobinVal (asserting it to 1 if it did not exist already), then increment it by 1, then -1, then divide it by "current.outObjects.length + 1" to get the final value. But that would mean the .value++ -1 would be self defeating so that's no right. I assume the ++ is done after it evaluates the %.

0 Likes
Message 8 of 10

jason_lightfoot_adsk
Autodesk
Autodesk

It's incremented after the expression is evaluated. There's no preincrement yet in flexscript afaik.

Message 9 of 10

Matthew_Gillespie
Autodesk
Autodesk
Accepted solution

The F in fmod stands for float and doesn't round to the nearest integer like % does, for example:

3.4 % 2                  //    1
Math.fmod(3.4, 2)        //    1.4


Matthew Gillespie
FlexSim Software Developer

Message 10 of 10

jonathan_s3
Not applicable

That makes sense with some of the behavior I was seeing. I had been wondering if it was something like that.

0 Likes