Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are JavaScript numbers deterministic?

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?

like image 723
MaiaVictor Avatar asked Apr 23 '26 02:04

MaiaVictor


1 Answers

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.

like image 82
Bergi Avatar answered Apr 25 '26 16:04

Bergi