Reputation: 53037
JavaScript represents numbers as IEEE 754 doubles, which are deterministic. Nether less, I've seen arguments that some compiler optimizations can change the order of floating point operations, bringing non-determinism across different runs. So, the question is: using no other source of non-determinism (Math.random
, etc.), will a Number -> Number
JavaScript function always produce the same result independent of platform and engine?
Upvotes: 3
Views: 1101
Reputation: 665256
some compiler optimizations can change the order of floating point operations, bringing non-determinism across different runs
The ECMAScript specification does not discuss such optimizations. In general it is however expected (like it's explicitly noted for some TypedArray
algorithms) that "optimization must not introduce any observable changes in the specified behaviour of the algorithm." And evaluation order for operators is quite strictly specified in ECMAScript.
So unless proven wrong by an implementation that does such stuff (and whose standard-compliance would yet need to be determined), we can assume that the answer is Yes.
Upvotes: 4
Reputation: 23863
Taking a guess you are referring to language-specific non-determinisms, rather than anything IEEE-754 specific.
Many other languages, such as C
have undefined
or implementation-specific
behavior which allows the compiler to emit very tight code for that processor, but at the cost of a good number of gotchas.
For instance, in the C
language this expression:
(a++) + (++a)
can be evaluated in two different orders and you can get two valid answers.
EcmaScript 3 (and 5) however, specify the order of operations for expressions so any JavaScript platform should do things in exactly the same way.
There is a longer discussion here:
Javascript evaluation order for operators
Upvotes: 1