JavaScript semantic change
Posted: Sun Dec 12, 2010 5:42 pm
Boo.
For a while now – call it a year and a half – I’ve been putting off upgrading Oolite’s JavaScript engine to the current version. The amount of work has of course grown constantly during that time, and there are now some compatibility issues.
In particular, it is no longer possible for Oolite to customize the behaviour of the
I’m loathe to add
For quaternions, the dot product of two normalized quaternions will be near 1 if they are nearly the same.
As the functionality is no longer present in SpiderMonkey, it’s not possible to provide a transition period.
For a while now – call it a year and a half – I’ve been putting off upgrading Oolite’s JavaScript engine to the current version. The amount of work has of course grown constantly during that time, and there are now some compatibility issues.
In particular, it is no longer possible for Oolite to customize the behaviour of the
==
/!=
operators to usefully compare different objects with the same values. Doing this in the first place was an abuse of the engine which didn’t comply with JavaScript language semantics. This primarily affects vectors and quaternions.I’m loathe to add
equal()
methods to vectors and quaternions, though, because they’re less meaningful than you’d expect. Because of the limited precision of floating-point numbers, calculations that are algebraically equivalent can produce slightly different results (or in odd cases, very different results). The correct way to compare vectors is to check whether the distance between them is below some threshold value:
Code: Select all
var equal = v.squaredDistanceTo(u) < thresholdSquared;
Code: Select all
// Skip normalize() if they are known to be normalized already –
// entity orientations are always normalized.
var equal = q.normalize().dot(p.normalize()) > 0.9999;
==
/!=
will not work for distinct Oolite objects in the next release, and will stop working in nightlies soon (probably tonight) even before the upgrade is complete.