Skip to content

Commit 4418329

Browse files
committed
#177 Add neg method to negate a Big number.
1 parent 55bd68c commit 4418329

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

big.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,16 @@
629629

630630
return this.minus(x.times(y));
631631
};
632+
633+
634+
/*
635+
* Return a new Big whose value is the value of this Big negated.
636+
*/
637+
P.neg = function () {
638+
var x = new this.constructor(this);
639+
x.s = -x.s;
640+
return x;
641+
};
632642

633643

634644
/*

big.mjs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,16 @@ P.mod = function (y) {
628628
};
629629

630630

631+
/*
632+
* Return a new Big whose value is the value of this Big negated.
633+
*/
634+
P.neg = function () {
635+
var x = new this.constructor(this);
636+
x.s = -x.s;
637+
return x;
638+
};
639+
640+
631641
/*
632642
* Return a new Big whose value is the value of this Big plus the value of Big y.
633643
*/

docs/index.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<li><a href="#lte" >lte</a></li>
104104
<li><a href="#minus" >minus</a></li>
105105
<li><a href="#mod" >mod</a></li>
106+
<li><a href="#neg" >neg</a></li>
106107
<li><a href="#plus" >plus</a></li>
107108
<li><a href="#pow" >pow</a></li>
108109
<li><a href="#prec" >prec</a></li>
@@ -207,7 +208,7 @@ <h5 id="big">
207208
new Big('-735.0918e-430') // '-7.350918e-428'
208209
Big(435.345) // '435.345'
209210
new Big() // 'Error: [big.js] Invalid value'
210-
Big() // No error, and a new Big constructor is returned
211+
Big2 = Big() // No error, and a new Big constructor is returned
211212
</pre>
212213

213214

@@ -580,6 +581,17 @@ <h5 id="mod">mod<code class='inset'>.mod(n) <i>&rArr; Big</i></code></h5>
580581

581582

582583

584+
<h5 id="neg">neg<code class='inset'>.neg() <i>&rArr; Big</i></code></h5>
585+
<p>
586+
Returns a Big number whose value is the value of this Big number negated.
587+
</p>
588+
<pre>
589+
x = new Big(0.3)
590+
x.neg() // '-0.3'
591+
x.neg().neg() // '0.3'</pre>
592+
593+
594+
583595
<h5 id="plus">
584596
plus<code class='inset'>.plus(n) <i>&rArr; Big</i></code>
585597
</h5>

test/browser/test.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<!-- <script src='../methods/div.js'></script> -->
1414
<!-- <script src='../methods/minus.js'></script> -->
1515
<!-- <script src='../methods/mod.js'></script> -->
16+
<!-- <script src='../methods/neg.js'></script> -->
1617
<!-- <script src='../methods/plus.js'></script> -->
1718
<!-- <script src='../methods/pow.js'></script> -->
1819
<!-- <script src='../methods/prec.js'></script> -->

test/runner.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ console.log( '\n Testing big.js\n' );
1010
'div',
1111
'minus',
1212
'mod',
13+
'neg',
1314
'plus',
1415
'pow',
1516
'prec',

0 commit comments

Comments
 (0)