question

Jonathan avatar image
1 Like"
Jonathan asked Joerg Vogel edited

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

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

FlexSim 20.2.3
flexscriptflexsim 20.2.3mathfmod
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Matthew Gillespie avatar image
3 Likes"
Matthew Gillespie answered

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
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jonathan avatar image Jonathan commented ·

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

0 Likes 0 ·
Jason Lightfoot avatar image
1 Like"
Jason Lightfoot answered Jason Lightfoot commented

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?

· 6
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Jonathan avatar image Jonathan commented ·

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 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ Jonathan commented ·

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.

1 Like 1 ·
Jonathan avatar image Jonathan Jason Lightfoot ♦ commented ·

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 0 ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.