NetLogo / Tortoise

Compiler and runtime engine for NetLogo models that runs in JavaScript 🐢
https://netlogoweb.org
Other
56 stars 27 forks source link

Different behaviors between NetLogo and NetLogo Web on "sum" primitive #213

Closed CIVITAS-John closed 4 years ago

CIVITAS-John commented 4 years ago

Several weeks ago, I encountered a different behavior between NetLogo and NetLogo Web. To produce it minimally, try:

sum ["go-home" 5 "go-grocery" 1 "go-park" 1 "go-bar" 1 "wander" 1]

Expected result: 9 (NetLogo) Actual result: "0go-home5go-grocery1go-park1go-bar1wander1" (NetLogo Web)

The unexpected result then caused an undefined error (ERROR: ) in the following procedure:

; get-decision: Randomly report a decision from the given weighted list. to-report get-decision [ chances ] ; let maximum reduce [ [current next] -> ifelse-value (is-number? next) [ current + next ] [ current ] ] but-first chances let maximum sum chances let dice random maximum let current 0 let index 0 loop [ set current current + item (index + 1) chances if (current > dice) [ report item index chances ] set index (index + 2) if (index >= length chances) [ report item (index - 2) chances ] ] end

Interestingly, the commented "reduce" implementation works well in both platforms. I assume it is due to the fact that NetLogo Web doesn't perform type checking in its Javascript implementation of "sum" primitive.

LaCuneta commented 4 years ago

I did a check of the other number crunching prims and found that min and max were similarly affected. mean is also affected, but surprisingly to me mean throws an error with any non-number items in its list argument, which is different behavior than sum, median, and variance. I'm trying to figure out why that's the case, and might update both desktop and web to change the behavior if there isn't a good reason.

I'll also be adding test cases for all scenarios to desktop so we can avoid regressions in the future.