Summary
test/modules/powSqrt.js cannot execute. It throws before its first assertion,
on a clean checkout of master, on any Node version.
Reproduction
git clone https://github.com/MikeMcl/decimal.js
cd decimal.js
node -e "require('./test/modules/powSqrt.js')"
Testing pow against sqrt...
ReferenceError: total is not defined
at .../test/modules/powSqrt.js:12
Cause
Line 12 loops on a free variable:
for (var e, n, p, r, s; total < 10000; ) {
Nothing defines total. test/setup.js keeps its counters as closure variables
inside T — passed and testNumber — and exposes them only afterwards, as
T.result. There is no global of that name, so the comparison throws on the
first evaluation of the loop condition.
test/test.js computes a local total while summing results, which looks like
where the name came from, but that variable is not in scope here and the module
is required in its own right.
Why it went unnoticed
test/test.js lists 60 modules to require, and powSqrt is not among them —
test/modules/ holds 61 files. So npm test never loads it, and the failure
never surfaces.
Why it matters
The module is a genuinely valuable cross-check that is currently doing nothing.
It compares r.pow(0.5) against r.sqrt() for random values, at a random
rounding mode and a random precision in [1, 40], ten thousand times — which
exercises naturalExponential and naturalLogarithm against the independent
Newton-Raphson path in squareRoot. Nothing else in the suite pits those two
implementations against each other.
Suggested fixes
Either would do:
-
Use the harness's own counter. T.result is only set once the module
finishes, so this needs a live counter — e.g. exposing testNumber from
setup.js, and looping on that.
-
Loop a fixed number of times, which is what the code appears to intend:
for (var e, n, p, r, s, i = 0; i < 10000; i++) {
Then add 'powSqrt' to the module list in test/test.js so it actually runs.
Be aware it is slow — ten thousand pow(0.5) calls at up to 40 significant
digits.
Summary
test/modules/powSqrt.jscannot execute. It throws before its first assertion,on a clean checkout of
master, on any Node version.Reproduction
Cause
Line 12 loops on a free variable:
Nothing defines
total.test/setup.jskeeps its counters as closure variablesinside
T—passedandtestNumber— and exposes them only afterwards, asT.result. There is no global of that name, so the comparison throws on thefirst evaluation of the loop condition.
test/test.jscomputes a localtotalwhile summing results, which looks likewhere the name came from, but that variable is not in scope here and the module
is required in its own right.
Why it went unnoticed
test/test.jslists 60 modules to require, andpowSqrtis not among them —test/modules/holds 61 files. Sonpm testnever loads it, and the failurenever surfaces.
Why it matters
The module is a genuinely valuable cross-check that is currently doing nothing.
It compares
r.pow(0.5)againstr.sqrt()for random values, at a randomrounding mode and a random precision in [1, 40], ten thousand times — which
exercises
naturalExponentialandnaturalLogarithmagainst the independentNewton-Raphson path in
squareRoot. Nothing else in the suite pits those twoimplementations against each other.
Suggested fixes
Either would do:
Use the harness's own counter.
T.resultis only set once the modulefinishes, so this needs a live counter — e.g. exposing
testNumberfromsetup.js, and looping on that.Loop a fixed number of times, which is what the code appears to intend:
Then add
'powSqrt'to the module list intest/test.jsso it actually runs.Be aware it is slow — ten thousand
pow(0.5)calls at up to 40 significantdigits.