Right, I’ve been tinkering more…
Firstly it turns out we’ve been chasing a red herring and actually the rounding is working properly in the HTML5 compile after all, both using math.pow and ^. Back to the drawing board in finding the source of these glitches then!
Secondly, benchmarking has shown ^ to be considerably faster than math.pow, and the if() check to be faster than the ^ maths + also faster than the local d = d or 0. Again the ^ was already in production but we’ll push the if() check to production now too and also see where else we can integrate it for similar optimisations. Thanks for that Ed!
Thirdly though, the benchmarking has shown some serious slugging from the HTML compile. The following completed in approx 0.0001 seconds in the simulator, but takes 0.2 seconds when built to HTML5 and opened in Chrome.
function testing(n, d) if(d) then return math.floor((n \* (10 ^ d)) + 0.5) / (10 ^ d) else return math.floor(n + 0.5) end end local s = os.clock() for i = 1, 100, 0.01 do testing(i, 1) end local e = os.clock() - s print(e)
Javascript is known to underperform compared with other languages, but this is a considerable jump. I doubt much can be done other than benchmarking the different loop variants in Javascript and then using the appropriate type of loop in Lua that translates to that variant in the compile rather than the most appropriate loop for Lua, which would defeat the purpose of writing platform independent code and in my view therefore not worth the effort, but hopefully this finding is at least useful to somebody else.