Closed jwzimmer-zz closed 5 years ago
tan(x)
where x
is 90 technically should have the same result as x/0
, i.e. NaN
. However, values of x
approaching 90 should get progressively larger (e.g. tan(89) ~= 57.3
but tan(89.99) ~=5730
).
I've come across this numerous times, and it's probably worth noting that it has nothing to do with tan, specifically – it's all to do with the large numbers.
I've always suspected this issue comes down to rounding errors when performing the calculations on such large values (i.e. maths operations on x & y in function keepOnStage() in ScratchSprite.as) – the final values still end up outside the expected range (i.e. not within screen limits).
I think a fairly quick-n-easy fix might be to assume that the dimensions of the sprite will not exceed some suitably large value (a million pixels?), and then check if the x and/or y positions do exceed this by somewhat more (positively or negatively). If so, then bound the co-ordinate without any reference to the actual supplied x and/or y (i.e. using only the sprite's bounding box, along with the sign(s) of the relevant x/y co-ord(s) to tell which screen edge to bound it on).
Discussed with @cwillisf, reported by user -Handle-. If you use tan(90) for sprite position, the value will be a very large number and the sprite will not appear within the visible part of the stage.
Repro: https://scratch.mit.edu/projects/212387052/#editor
@cwillisf said:
Math.pi
in Flash (and in JavaScript, and a number of other places) is represented the same way, with a 64-bit floating point number (standard IEEE 754 format), which is imprecise enough that the common way of implementingtan
gives a result that's really big but not actually infinity (inf
). It could also be related to converting from degrees to radians.