Skip to content

Commit d54f97e

Browse files
committed
#107 sqrt of large number
1 parent 373beed commit d54f97e

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

big.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,17 +766,18 @@
766766
if (s < 0) throw Error(NAME + 'No square root');
767767

768768
// Estimate.
769-
s = Math.sqrt(x.toString());
769+
s = Math.sqrt(x + '');
770770

771771
// Math.sqrt underflow/overflow?
772-
// Re-estimate: pass x to Math.sqrt as integer, then adjust the result exponent.
772+
// Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
773773
if (s === 0 || s === 1 / 0) {
774774
c = x.c.join('');
775775
if (!(c.length + e & 1)) c += '0';
776-
r = new Big(Math.sqrt(c).toString());
777-
r.e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
776+
s = Math.sqrt(c);
777+
e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
778+
r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
778779
} else {
779-
r = new Big(s.toString());
780+
r = new Big(s);
780781
}
781782

782783
e = r.e + (Big.DP += 4);

big.mjs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,17 +763,18 @@ P.sqrt = function () {
763763
if (s < 0) throw Error(NAME + 'No square root');
764764

765765
// Estimate.
766-
s = Math.sqrt(x.toString());
766+
s = Math.sqrt(x + '');
767767

768768
// Math.sqrt underflow/overflow?
769-
// Re-estimate: pass x to Math.sqrt as integer, then adjust the result exponent.
769+
// Re-estimate: pass x coefficient to Math.sqrt as integer, then adjust the result exponent.
770770
if (s === 0 || s === 1 / 0) {
771771
c = x.c.join('');
772772
if (!(c.length + e & 1)) c += '0';
773-
r = new Big(Math.sqrt(c).toString());
774-
r.e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
773+
s = Math.sqrt(c);
774+
e = ((e + 1) / 2 | 0) - (e < 0 || e & 1);
775+
r = new Big((s == 1 / 0 ? '1e' : (s = s.toExponential()).slice(0, s.indexOf('e') + 1)) + e);
775776
} else {
776-
r = new Big(s.toString());
777+
r = new Big(s);
777778
}
778779

779780
e = r.e + (Big.DP += 4);

test/sqrt.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,11 @@ var count = (function sqrt(Big) {
781781
Big.DP = 2;
782782
T('0.04', 0.001);
783783

784+
Big.DP = 20;
785+
Big.RM = 0;
786+
T('9.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999e+179', new Big('1e360').minus(1));
787+
//T('9.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999e+499', new Big('1e1000').minus(1));
788+
784789
log('\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \n');
785790
return [passed, total];;
786791
})(this.Big);

0 commit comments

Comments
 (0)