1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Created by GNU Texinfo 6.5, http://www.gnu.org/software/texinfo/ -->
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
6<title>Javascript Bignum Extensions</title>
7
8<meta name="description" content="Javascript Bignum Extensions">
9<meta name="keywords" content="Javascript Bignum Extensions">
10<meta name="resource-type" content="document">
11<meta name="distribution" content="global">
12<meta name="Generator" content="makeinfo">
13<link href="#SEC_Contents" rel="contents" title="Table of Contents">
14<style type="text/css">
15<!--
16a.summary-letter {text-decoration: none}
17blockquote.indentedblock {margin-right: 0em}
18blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
19blockquote.smallquotation {font-size: smaller}
20div.display {margin-left: 3.2em}
21div.example {margin-left: 3.2em}
22div.lisp {margin-left: 3.2em}
23div.smalldisplay {margin-left: 3.2em}
24div.smallexample {margin-left: 3.2em}
25div.smalllisp {margin-left: 3.2em}
26kbd {font-style: oblique}
27pre.display {font-family: inherit}
28pre.format {font-family: inherit}
29pre.menu-comment {font-family: serif}
30pre.menu-preformatted {font-family: serif}
31pre.smalldisplay {font-family: inherit; font-size: smaller}
32pre.smallexample {font-size: smaller}
33pre.smallformat {font-family: inherit; font-size: smaller}
34pre.smalllisp {font-size: smaller}
35span.nolinebreak {white-space: nowrap}
36span.roman {font-family: initial; font-weight: normal}
37span.sansserif {font-family: sans-serif; font-weight: normal}
38ul.no-bullet {list-style: none}
39-->
40</style>
41<meta name="viewport" content="width=device-width, initial-scale=1.0">
42
43
44</head>
45
46<body lang="en">
47<h1 class="settitle" align="center">Javascript Bignum Extensions</h1>
48
49<a name="SEC_Contents"></a>
50<h2 class="contents-heading">Table of Contents</h2>
51
52<div class="contents">
53<ul class="no-bullet">
54<li><a name="toc-Introduction" href="#Introduction">1 Introduction</a></li>
55<li><a name="toc-Operator-overloading" href="#Operator-overloading">2 Operator overloading</a></li>
56<li><a name="toc-BigInt-extensions" href="#BigInt-extensions">3 BigInt extensions</a></li>
57<li><a name="toc-BigFloat" href="#BigFloat">4 BigFloat</a>
58<ul class="no-bullet">
59  <li><a name="toc-Introduction-1" href="#Introduction-1">4.1 Introduction</a></li>
60  <li><a name="toc-Floating-point-rounding" href="#Floating-point-rounding">4.2 Floating point rounding</a></li>
61  <li><a name="toc-Operators" href="#Operators">4.3 Operators</a></li>
62  <li><a name="toc-BigFloat-literals" href="#BigFloat-literals">4.4 BigFloat literals</a></li>
63  <li><a name="toc-Builtin-Object-changes" href="#Builtin-Object-changes">4.5 Builtin Object changes</a>
64  <ul class="no-bullet">
65    <li><a name="toc-BigFloat-function" href="#BigFloat-function">4.5.1 <code>BigFloat</code> function</a></li>
66    <li><a name="toc-BigFloat_002eprototype" href="#BigFloat_002eprototype">4.5.2 <code>BigFloat.prototype</code></a></li>
67    <li><a name="toc-BigFloatEnv-constructor" href="#BigFloatEnv-constructor">4.5.3 <code>BigFloatEnv</code> constructor</a></li>
68  </ul></li>
69</ul></li>
70<li><a name="toc-BigDecimal" href="#BigDecimal">5 BigDecimal</a>
71<ul class="no-bullet">
72  <li><a name="toc-Operators-1" href="#Operators-1">5.1 Operators</a></li>
73  <li><a name="toc-BigDecimal-literals" href="#BigDecimal-literals">5.2 BigDecimal literals</a></li>
74  <li><a name="toc-Builtin-Object-changes-1" href="#Builtin-Object-changes-1">5.3 Builtin Object changes</a>
75  <ul class="no-bullet">
76    <li><a name="toc-The-BigDecimal-function_002e" href="#The-BigDecimal-function_002e">5.3.1 The <code>BigDecimal</code> function.</a></li>
77    <li><a name="toc-Properties-of-the-BigDecimal-object" href="#Properties-of-the-BigDecimal-object">5.3.2 Properties of the <code>BigDecimal</code> object</a></li>
78    <li><a name="toc-Properties-of-the-BigDecimal_002eprototype-object" href="#Properties-of-the-BigDecimal_002eprototype-object">5.3.3 Properties of the <code>BigDecimal.prototype</code> object</a></li>
79  </ul></li>
80</ul></li>
81<li><a name="toc-Math-mode" href="#Math-mode">6 Math mode</a></li>
82
83</ul>
84</div>
85
86
87<a name="Introduction"></a>
88<h2 class="chapter">1 Introduction</h2>
89
90<p>The Bignum extensions add the following features to the Javascript
91language while being 100% backward compatible:
92</p>
93<ul>
94<li> Operator overloading with a dispatch logic inspired from the proposal available at <a href="https://github.com/tc39/proposal-operator-overloading/">https://github.com/tc39/proposal-operator-overloading/</a>.
95
96</li><li> Arbitrarily large floating point numbers (<code>BigFloat</code>) in base 2 using the IEEE 754 semantics.
97
98</li><li> Arbitrarily large floating point numbers (<code>BigDecimal</code>) in base 10 based on the proposal available at
99<a href="https://github.com/littledan/proposal-bigdecimal">https://github.com/littledan/proposal-bigdecimal</a>.
100
101</li><li> <code>math</code> mode: arbitrarily large integers and floating point numbers are available by default. The integer division and power can be overloaded for example to return a fraction. The modulo operator (<code>%</code>) is defined as the Euclidian
102remainder. <code>^</code> is an alias to the power operator
103(<code>**</code>). <code>^^</code> is used as the exclusive or operator.
104
105</li></ul>
106
107<p>The extensions are independent from each other except the <code>math</code>
108mode which relies on BigFloat and operator overloading.
109</p>
110<a name="Operator-overloading"></a>
111<h2 class="chapter">2 Operator overloading</h2>
112
113<p>Operator overloading is inspired from the proposal available at
114<a href="https://github.com/tc39/proposal-operator-overloading/">https://github.com/tc39/proposal-operator-overloading/</a>. It
115implements the same dispatch logic but finds the operator sets by
116looking at the <code>Symbol.operatorSet</code> property in the objects. The
117changes were done in order to simplify the implementation.
118</p>
119<p>More precisely, the following modifications were made:
120</p>
121<ul>
122<li> <code>with operators from</code> is not supported. Operator overloading is always enabled.
123
124</li><li> The dispatch is not based on a static <code>[[OperatorSet]]</code> field in all instances. Instead, a dynamic lookup of the <code>Symbol.operatorSet</code> property is done. This property is typically added in the prototype of each object.
125
126</li><li> <code>Operators.create(...dictionaries)</code> is used to create a new OperatorSet object. The <code>Operators</code> function is supported as an helper to be closer to the TC39 proposal.
127
128</li><li> <code>[]</code> cannot be overloaded.
129
130</li><li> In math mode, the BigInt division and power operators can be overloaded with <code>Operators.updateBigIntOperators(dictionary)</code>.
131
132</li></ul>
133
134<a name="BigInt-extensions"></a>
135<h2 class="chapter">3 BigInt extensions</h2>
136
137<p>A few properties are added to the BigInt object:
138</p>
139<dl compact="compact">
140<dt><code>tdiv(a, b)</code></dt>
141<dd><p>Return <em>trunc(a/b)</em>. <code>b = 0</code> raises a RangeError
142exception.
143</p>
144</dd>
145<dt><code>fdiv(a, b)</code></dt>
146<dd><p>Return <em>\lfloor a/b \rfloor</em>. <code>b = 0</code> raises a RangeError
147exception.
148</p>
149</dd>
150<dt><code>cdiv(a, b)</code></dt>
151<dd><p>Return <em>\lceil a/b \rceil</em>. <code>b = 0</code> raises a RangeError
152exception.
153</p>
154</dd>
155<dt><code>ediv(a, b)</code></dt>
156<dd><p>Return <em>sgn(b) \lfloor a/{|b|} \rfloor</em> (Euclidian
157division). <code>b = 0</code> raises a RangeError exception.
158</p>
159</dd>
160<dt><code>tdivrem(a, b)</code></dt>
161<dt><code>fdivrem(a, b)</code></dt>
162<dt><code>cdivrem(a, b)</code></dt>
163<dt><code>edivrem(a, b)</code></dt>
164<dd><p>Return an array of two elements. The first element is the quotient,
165the second is the remainder. The same rounding is done as the
166corresponding division operation.
167</p>
168</dd>
169<dt><code>sqrt(a)</code></dt>
170<dd><p>Return <em>\lfloor \sqrt(a) \rfloor</em>. A RangeError exception is
171raised if <em>a &lt; 0</em>.
172</p>
173</dd>
174<dt><code>sqrtrem(a)</code></dt>
175<dd><p>Return an array of two elements. The first element is <em>\lfloor
176\sqrt{a} \rfloor</em>. The second element is <em>a-\lfloor \sqrt{a}
177\rfloor^2</em>. A RangeError exception is raised if <em>a &lt; 0</em>.
178</p>
179</dd>
180<dt><code>floorLog2(a)</code></dt>
181<dd><p>Return -1 if <em>a \leq 0</em> otherwise return <em>\lfloor \log2(a) \rfloor</em>.
182</p>
183</dd>
184<dt><code>ctz(a)</code></dt>
185<dd><p>Return the number of trailing zeros in the two&rsquo;s complement binary representation of a. Return -1 if <em>a=0</em>.
186</p>
187</dd>
188</dl>
189
190<a name="BigFloat"></a>
191<h2 class="chapter">4 BigFloat</h2>
192
193<a name="Introduction-1"></a>
194<h3 class="section">4.1 Introduction</h3>
195
196<p>This extension adds the <code>BigFloat</code> primitive type. The
197<code>BigFloat</code> type represents floating point numbers in base 2
198with the IEEE 754 semantics. A floating
199point number is represented as a sign, mantissa and exponent. The
200special values <code>NaN</code>, <code>+/-Infinity</code>, <code>+0</code> and <code>-0</code>
201are supported. The mantissa and exponent can have any bit length with
202an implementation specific minimum and maximum.
203</p>
204<a name="Floating-point-rounding"></a>
205<h3 class="section">4.2 Floating point rounding</h3>
206
207<p>Each floating point operation operates with infinite precision and
208then rounds the result according to the specified floating point
209environment (<code>BigFloatEnv</code> object). The status flags of the
210environment are also set according to the result of the operation.
211</p>
212<p>If no floating point environment is provided, the global floating
213point environment is used.
214</p>
215<p>The rounding mode of the global floating point environment is always
216<code>RNDN</code> (&ldquo;round to nearest with ties to even&rdquo;)<a name="DOCF1" href="#FOOT1"><sup>1</sup></a>. The status flags of the global environment cannot be
217read<a name="DOCF2" href="#FOOT2"><sup>2</sup></a>. The precision of the global environment is
218<code>BigFloatEnv.prec</code>. The number of exponent bits of the global
219environment is <code>BigFloatEnv.expBits</code>. The global environment
220subnormal flag is set to <code>true</code>.
221</p>
222<p>For example, <code>prec = 53</code> and <code> expBits = 11</code> exactly give
223the same precision as the IEEE 754 64 bit floating point format. The
224default precision is <code>prec = 113</code> and <code> expBits = 15</code> (IEEE
225754 128 bit floating point format).
226</p>
227<p>The global floating point environment can only be modified temporarily
228when calling a function (see <code>BigFloatEnv.setPrec</code>). Hence a
229function can change the global floating point environment for its
230callees but not for its caller.
231</p>
232<a name="Operators"></a>
233<h3 class="section">4.3 Operators</h3>
234
235<p>The builtin operators are extended so that a BigFloat is returned if
236at least one operand is a BigFloat. The computations are always done
237with infinite precision and rounded according to the global floating
238point environment.
239</p>
240<p><code>typeof</code> applied on a <code>BigFloat</code> returns <code>bigfloat</code>.
241</p>
242<p>BigFloat can be compared with all the other numeric types and the
243result follows the expected mathematical relations.
244</p>
245<p>However, since BigFloat and Number are different types they are never
246equal when using the strict comparison operators (e.g. <code>0.0 ===
2470.0l</code> is false).
248</p>
249<a name="BigFloat-literals"></a>
250<h3 class="section">4.4 BigFloat literals</h3>
251
252<p>BigFloat literals are floating point numbers with a trailing <code>l</code>
253suffix. BigFloat literals have an infinite precision. They are rounded
254according to the global floating point environment when they are
255evaluated.<a name="DOCF3" href="#FOOT3"><sup>3</sup></a>
256</p>
257<a name="Builtin-Object-changes"></a>
258<h3 class="section">4.5 Builtin Object changes</h3>
259
260<a name="BigFloat-function"></a>
261<h4 class="subsection">4.5.1 <code>BigFloat</code> function</h4>
262
263<p>The <code>BigFloat</code> function cannot be invoked as a constructor. When
264invoked as a function: the parameter is converted to a primitive
265type. If the result is a numeric type, it is converted to BigFloat
266without rounding. If the result is a string, it is converted to
267BigFloat using the precision of the global floating point environment.
268</p>
269<p><code>BigFloat</code> properties:
270</p>
271<dl compact="compact">
272<dt><code>LN2</code></dt>
273<dt><code>PI</code></dt>
274<dd><p>Getter. Return the value of the corresponding mathematical constant
275rounded to nearest, ties to even with the current global
276precision. The constant values are cached for small precisions.
277</p>
278</dd>
279<dt><code>MIN_VALUE</code></dt>
280<dt><code>MAX_VALUE</code></dt>
281<dt><code>EPSILON</code></dt>
282<dd><p>Getter. Return the minimum, maximum and epsilon <code>BigFloat</code> values
283(same definition as the corresponding <code>Number</code> constants).
284</p>
285</dd>
286<dt><code>fpRound(a[, e])</code></dt>
287<dd><p>Round the floating point number <code>a</code> according to the floating
288point environment <code>e</code> or the global environment if <code>e</code> is
289undefined.
290</p>
291</dd>
292<dt><code>parseFloat(a[, radix[, e]])</code></dt>
293<dd><p>Parse the string <code>a</code> as a floating point number in radix
294<code>radix</code>. The radix is 0 (default) or from 2 to 36. The radix 0
295means radix 10 unless there is a hexadecimal or binary prefix. The
296result is rounded according to the floating point environment <code>e</code>
297or the global environment if <code>e</code> is undefined.
298</p>
299</dd>
300<dt><code>isFinite(a)</code></dt>
301<dd><p>Return true if <code>a</code> is a finite bigfloat.
302</p>
303</dd>
304<dt><code>isNaN(a)</code></dt>
305<dd><p>Return true if <code>a</code> is a NaN bigfloat.
306</p>
307</dd>
308<dt><code>add(a, b[, e])</code></dt>
309<dt><code>sub(a, b[, e])</code></dt>
310<dt><code>mul(a, b[, e])</code></dt>
311<dt><code>div(a, b[, e])</code></dt>
312<dd><p>Perform the specified floating point operation and round the floating
313point number <code>a</code> according to the floating point environment
314<code>e</code> or the global environment if <code>e</code> is undefined. If
315<code>e</code> is specified, the floating point status flags are updated.
316</p>
317</dd>
318<dt><code>floor(x)</code></dt>
319<dt><code>ceil(x)</code></dt>
320<dt><code>round(x)</code></dt>
321<dt><code>trunc(x)</code></dt>
322<dd><p>Round to an integer. No additional rounding is performed.
323</p>
324</dd>
325<dt><code>abs(x)</code></dt>
326<dd><p>Return the absolute value of x. No additional rounding is performed.
327</p>
328</dd>
329<dt><code>fmod(x, y[, e])</code></dt>
330<dt><code>remainder(x, y[, e])</code></dt>
331<dd><p>Floating point remainder. The quotient is truncated to zero (fmod) or
332to the nearest integer with ties to even (remainder). <code>e</code> is an
333optional floating point environment.
334</p>
335</dd>
336<dt><code>sqrt(x[, e])</code></dt>
337<dd><p>Square root. Return a rounded floating point number. <code>e</code> is an
338optional floating point environment.
339</p>
340</dd>
341<dt><code>sin(x[, e])</code></dt>
342<dt><code>cos(x[, e])</code></dt>
343<dt><code>tan(x[, e])</code></dt>
344<dt><code>asin(x[, e])</code></dt>
345<dt><code>acos(x[, e])</code></dt>
346<dt><code>atan(x[, e])</code></dt>
347<dt><code>atan2(x, y[, e])</code></dt>
348<dt><code>exp(x[, e])</code></dt>
349<dt><code>log(x[, e])</code></dt>
350<dt><code>pow(x, y[, e])</code></dt>
351<dd><p>Transcendental operations. Return a rounded floating point
352number. <code>e</code> is an optional floating point environment.
353</p>
354</dd>
355</dl>
356
357<a name="BigFloat_002eprototype"></a>
358<h4 class="subsection">4.5.2 <code>BigFloat.prototype</code></h4>
359
360<p>The following properties are modified:
361</p>
362<dl compact="compact">
363<dt><code>valueOf()</code></dt>
364<dd><p>Return the bigfloat primitive value corresponding to <code>this</code>.
365</p>
366</dd>
367<dt><code>toString(radix)</code></dt>
368<dd>
369<p>For floating point numbers:
370</p>
371<ul>
372<li> If the radix is a power of two, the conversion is done with infinite
373precision.
374</li><li> Otherwise, the number is rounded to nearest with ties to even using
375the global precision. It is then converted to string using the minimum
376number of digits so that its conversion back to a floating point using
377the global precision and round to nearest gives the same number.
378
379</li></ul>
380
381<p>The exponent letter is <code>e</code> for base 10, <code>p</code> for bases 2, 8,
38216 with a binary exponent and <code>@</code> for the other bases.
383</p>
384</dd>
385<dt><code>toPrecision(p, rnd_mode = BigFloatEnv.RNDNA, radix = 10)</code></dt>
386<dt><code>toFixed(p, rnd_mode = BigFloatEnv.RNDNA, radix = 10)</code></dt>
387<dt><code>toExponential(p, rnd_mode = BigFloatEnv.RNDNA, radix = 10)</code></dt>
388<dd><p>Same semantics as the corresponding <code>Number</code> functions with
389BigFloats. There is no limit on the accepted precision <code>p</code>. The
390rounding mode and radix can be optionally specified. The radix must be
391between 2 and 36.
392</p>
393</dd>
394</dl>
395
396<a name="BigFloatEnv-constructor"></a>
397<h4 class="subsection">4.5.3 <code>BigFloatEnv</code> constructor</h4>
398
399<p>The <code>BigFloatEnv([p, [,rndMode]]</code> constructor cannot be invoked as a
400function. The floating point environment contains:
401</p>
402<ul>
403<li> the mantissa precision in bits
404
405</li><li> the exponent size in bits assuming an IEEE 754 representation;
406
407</li><li> the subnormal flag (if true, subnormal floating point numbers can
408be generated by the floating point operations).
409
410</li><li> the rounding mode
411
412</li><li> the floating point status. The status flags can only be set by the floating point operations. They can be reset with <code>BigFloatEnv.prototype.clearStatus()</code> or with the various status flag setters.
413
414</li></ul>
415
416<p><code>new BigFloatEnv([p, [,rndMode]]</code> creates a new floating point
417environment. The status flags are reset. If no parameter is given the
418precision, exponent bits and subnormal flags are copied from the
419global floating point environment. Otherwise, the precision is set to
420<code>p</code>, the number of exponent bits is set to <code>expBitsMax</code> and the
421subnormal flags is set to <code>false</code>. If <code>rndMode</code> is
422<code>undefined</code>, the rounding mode is set to <code>RNDN</code>.
423</p>
424<p><code>BigFloatEnv</code> properties:
425</p>
426<dl compact="compact">
427<dt><code>prec</code></dt>
428<dd><p>Getter. Return the precision in bits of the global floating point
429environment. The initial value is <code>113</code>.
430</p>
431</dd>
432<dt><code>expBits</code></dt>
433<dd><p>Getter. Return the exponent size in bits of the global floating point
434environment assuming an IEEE 754 representation. The initial value is
435<code>15</code>.
436</p>
437</dd>
438<dt><code>setPrec(f, p[, e])</code></dt>
439<dd><p>Set the precision of the global floating point environment to <code>p</code>
440and the exponent size to <code>e</code> then call the function
441<code>f</code>. Then the Float precision and exponent size are reset to
442their precious value and the return value of <code>f</code> is returned (or
443an exception is raised if <code>f</code> raised an exception). If <code>e</code>
444is <code>undefined</code> it is set to <code>BigFloatEnv.expBitsMax</code>.
445</p>
446</dd>
447<dt><code>precMin</code></dt>
448<dd><p>Read-only integer. Return the minimum allowed precision. Must be at least 2.
449</p>
450</dd>
451<dt><code>precMax</code></dt>
452<dd><p>Read-only integer. Return the maximum allowed precision. Must be at least 113.
453</p>
454</dd>
455<dt><code>expBitsMin</code></dt>
456<dd><p>Read-only integer. Return the minimum allowed exponent size in
457bits. Must be at least 3.
458</p>
459</dd>
460<dt><code>expBitsMax</code></dt>
461<dd><p>Read-only integer. Return the maximum allowed exponent size in
462bits. Must be at least 15.
463</p>
464</dd>
465<dt><code>RNDN</code></dt>
466<dd><p>Read-only integer. Round to nearest, with ties to even rounding mode.
467</p>
468</dd>
469<dt><code>RNDZ</code></dt>
470<dd><p>Read-only integer. Round to zero rounding mode.
471</p>
472</dd>
473<dt><code>RNDD</code></dt>
474<dd><p>Read-only integer. Round to -Infinity rounding mode.
475</p>
476</dd>
477<dt><code>RNDU</code></dt>
478<dd><p>Read-only integer. Round to +Infinity rounding mode.
479</p>
480</dd>
481<dt><code>RNDNA</code></dt>
482<dd><p>Read-only integer. Round to nearest, with ties away from zero rounding mode.
483</p>
484</dd>
485<dt><code>RNDA</code></dt>
486<dd><p>Read-only integer. Round away from zero rounding mode.
487</p>
488</dd>
489<dt><code>RNDF<a name="DOCF4" href="#FOOT4"><sup>4</sup></a></code></dt>
490<dd><p>Read-only integer. Faithful rounding mode. The result is
491non-deterministically rounded to -Infinity or +Infinity. This rounding
492mode usually gives a faster and deterministic running time for the
493floating point operations.
494</p>
495</dd>
496</dl>
497
498<p><code>BigFloatEnv.prototype</code> properties:
499</p>
500<dl compact="compact">
501<dt><code>prec</code></dt>
502<dd><p>Getter and setter (Integer). Return or set the precision in bits.
503</p>
504</dd>
505<dt><code>expBits</code></dt>
506<dd><p>Getter and setter (Integer). Return or set the exponent size in bits
507assuming an IEEE 754 representation.
508</p>
509</dd>
510<dt><code>rndMode</code></dt>
511<dd><p>Getter and setter (Integer). Return or set the rounding mode.
512</p>
513</dd>
514<dt><code>subnormal</code></dt>
515<dd><p>Getter and setter (Boolean). subnormal flag. It is false when
516<code>expBits = expBitsMax</code>.
517</p>
518</dd>
519<dt><code>clearStatus()</code></dt>
520<dd><p>Clear the status flags.
521</p>
522</dd>
523<dt><code>invalidOperation</code></dt>
524<dt><code>divideByZero</code></dt>
525<dt><code>overflow</code></dt>
526<dt><code>underflow</code></dt>
527<dt><code>inexact</code></dt>
528<dd><p>Getter and setter (Boolean). Status flags.
529</p>
530</dd>
531</dl>
532
533<a name="BigDecimal"></a>
534<h2 class="chapter">5 BigDecimal</h2>
535
536<p>This extension adds the <code>BigDecimal</code> primitive type. The
537<code>BigDecimal</code> type represents floating point numbers in base
53810. It is inspired from the proposal available at
539<a href="https://github.com/littledan/proposal-bigdecimal">https://github.com/littledan/proposal-bigdecimal</a>.
540</p>
541<p>The <code>BigDecimal</code> floating point numbers are always normalized and
542finite. There is no concept of <code>-0</code>, <code>Infinity</code> or
543<code>NaN</code>. By default, all the computations are done with infinite
544precision.
545</p>
546<a name="Operators-1"></a>
547<h3 class="section">5.1 Operators</h3>
548
549<p>The following builtin operators support BigDecimal:
550</p>
551<dl compact="compact">
552<dt><code>+</code></dt>
553<dt><code>-</code></dt>
554<dt><code>*</code></dt>
555<dd><p>Both operands must be BigDecimal. The result is computed with infinite
556precision.
557</p></dd>
558<dt><code>%</code></dt>
559<dd><p>Both operands must be BigDecimal. The result is computed with infinite
560precision. A range error is throws in case of division by zero.
561</p>
562</dd>
563<dt><code>/</code></dt>
564<dd><p>Both operands must be BigDecimal. A range error is throws in case of
565division by zero or if the result cannot be represented with infinite
566precision (use <code>BigDecimal.div</code> to specify the rounding).
567</p>
568</dd>
569<dt><code>**</code></dt>
570<dd><p>Both operands must be BigDecimal. The exponent must be a positive
571integer. The result is computed with infinite precision.
572</p>
573</dd>
574<dt><code>===</code></dt>
575<dd><p>When one of the operand is a BigDecimal, return true if both operands
576are a BigDecimal and if they are equal.
577</p>
578</dd>
579<dt><code>==</code></dt>
580<dt><code>!=</code></dt>
581<dt><code>&lt;=</code></dt>
582<dt><code>&gt;=</code></dt>
583<dt><code>&lt;</code></dt>
584<dt><code>&gt;</code></dt>
585<dd>
586<p>Numerical comparison. When one of the operand is not a BigDecimal, it is
587converted to BigDecimal by using ToString(). Hence comparisons between
588Number and BigDecimal do not use the exact mathematical value of the
589Number value.
590</p>
591</dd>
592</dl>
593
594<a name="BigDecimal-literals"></a>
595<h3 class="section">5.2 BigDecimal literals</h3>
596
597<p>BigDecimal literals are decimal floating point numbers with a trailing
598<code>m</code> suffix.
599</p>
600<a name="Builtin-Object-changes-1"></a>
601<h3 class="section">5.3 Builtin Object changes</h3>
602
603<a name="The-BigDecimal-function_002e"></a>
604<h4 class="subsection">5.3.1 The <code>BigDecimal</code> function.</h4>
605
606<p>It returns <code>0m</code> if no parameter is provided. Otherwise the first
607parameter is converted to a bigdecimal by using ToString(). Hence
608Number values are not converted to their exact numerical value as
609BigDecimal.
610</p>
611<a name="Properties-of-the-BigDecimal-object"></a>
612<h4 class="subsection">5.3.2 Properties of the <code>BigDecimal</code> object</h4>
613
614<dl compact="compact">
615<dt><code>add(a, b[, e])</code></dt>
616<dt><code>sub(a, b[, e])</code></dt>
617<dt><code>mul(a, b[, e])</code></dt>
618<dt><code>div(a, b[, e])</code></dt>
619<dt><code>mod(a, b[, e])</code></dt>
620<dt><code>sqrt(a, e)</code></dt>
621<dt><code>round(a, e)</code></dt>
622<dd><p>Perform the specified floating point operation and round the floating
623point result according to the rounding object <code>e</code>. If the
624rounding object is not present, the operation is executed with
625infinite precision.
626</p>
627<p>For <code>div</code>, a <code>RangeError</code> exception is thrown in case of
628division by zero or if the result cannot be represented with infinite
629precision if no rounding object is present.
630</p>
631<p>For <code>sqrt</code>, a range error is thrown if <code>a</code> is less than
632zero.
633</p>
634<p>The rounding object must contain the following properties:
635<code>roundingMode</code> is a string specifying the rounding mode
636(<code>&quot;floor&quot;</code>, <code>&quot;ceiling&quot;</code>, <code>&quot;down&quot;</code>, <code>&quot;up&quot;</code>,
637<code>&quot;half-even&quot;</code>, <code>&quot;half-up&quot;</code>). Either
638<code>maximumSignificantDigits</code> or <code>maximumFractionDigits</code> must
639be present to specify respectively the number of significant digits
640(must be &gt;= 1) or the number of digits after the decimal point (must
641be &gt;= 0).
642</p>
643</dd>
644</dl>
645
646<a name="Properties-of-the-BigDecimal_002eprototype-object"></a>
647<h4 class="subsection">5.3.3 Properties of the <code>BigDecimal.prototype</code> object</h4>
648
649<dl compact="compact">
650<dt><code>valueOf()</code></dt>
651<dd><p>Return the bigdecimal primitive value corresponding to <code>this</code>.
652</p>
653</dd>
654<dt><code>toString()</code></dt>
655<dd><p>Convert <code>this</code> to a string with infinite precision in base 10.
656</p>
657</dd>
658<dt><code>toPrecision(p, rnd_mode = &quot;half-up&quot;)</code></dt>
659<dt><code>toFixed(p, rnd_mode = &quot;half-up&quot;)</code></dt>
660<dt><code>toExponential(p, rnd_mode = &quot;half-up&quot;)</code></dt>
661<dd><p>Convert the BigDecimal <code>this</code> to string with the specified
662precision <code>p</code>. There is no limit on the accepted precision
663<code>p</code>. The rounding mode can be optionally
664specified. <code>toPrecision</code> outputs either in decimal fixed notation
665or in decimal exponential notation with a <code>p</code> digits of
666precision.  <code>toExponential</code> outputs in decimal exponential
667notation with <code>p</code> digits after the decimal point. <code>toFixed</code>
668outputs in decimal notation with <code>p</code> digits after the decimal
669point.
670</p>
671</dd>
672</dl>
673
674<a name="Math-mode"></a>
675<h2 class="chapter">6 Math mode</h2>
676
677<p>A new <em>math mode</em> is enabled with the <code>&quot;use math&quot;</code>
678directive. It propagates the same way as the <em>strict mode</em>. It is
679designed so that arbitrarily large integers and floating point numbers
680are available by default. In order to minimize the number of changes
681in the Javascript semantics, integers are represented either as Number
682or BigInt depending on their magnitude. Floating point numbers are
683always represented as BigFloat.
684</p>
685<p>The following changes are made to the Javascript semantics:
686</p>
687<ul>
688<li> Floating point literals (i.e. number with a decimal point or an exponent) are <code>BigFloat</code> by default (i.e. a <code>l</code> suffix is implied). Hence <code>typeof 1.0 === &quot;bigfloat&quot;</code>.
689
690</li><li> Integer literals (i.e. numbers without a decimal point or an exponent) with or without the <code>n</code> suffix are <code>BigInt</code> if their value cannot be represented as a safe integer. A safe integer is defined as a integer whose absolute value is smaller or equal to <code>2**53-1</code>. Hence <code>typeof 1 === &quot;number &quot;</code>, <code>typeof 1n === &quot;number&quot;</code> but <code>typeof 9007199254740992 === &quot;bigint&quot; </code>.
691
692</li><li> All the bigint builtin operators and functions are modified so that their result is returned as a Number if it is a safe integer. Otherwise the result stays a BigInt.
693
694</li><li> The builtin operators are modified so that they return an exact result (which can be a BigInt) if their operands are safe integers. Operands between Number and BigInt are accepted provided the Number operand is a safe integer. The integer power with a negative exponent returns a BigFloat as result. The integer division returns a BigFloat as result.
695
696</li><li> The <code>^</code> operator is an alias to the power operator (<code>**</code>).
697
698</li><li> The power operator (both <code>^</code> and <code>**</code>) grammar is modified so that <code>-2^2</code> is allowed and yields <code>-4</code>.
699
700</li><li> The logical xor operator is still available with the <code>^^</code> operator.
701
702</li><li> The modulo operator (<code>%</code>) returns the Euclidian remainder (always positive) instead of the truncated remainder.
703
704</li><li> The integer division operator can be overloaded with <code>Operators.updateBigIntOperators(dictionary)</code>.
705
706</li><li> The integer power operator with a non zero negative exponent can be overloaded with <code>Operators.updateBigIntOperators(dictionary)</code>.
707
708</li></ul>
709
710<div class="footnote">
711<hr>
712<h4 class="footnotes-heading">Footnotes</h4>
713
714<h3><a name="FOOT1" href="#DOCF1">(1)</a></h3>
715<p>The
716rationale is that the rounding mode changes must always be
717explicit.</p>
718<h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
719<p>The rationale is to avoid side effects for the built-in
720operators.</p>
721<h3><a name="FOOT3" href="#DOCF3">(3)</a></h3>
722<p>Base 10 floating point literals cannot usually be
723exactly represented as base 2 floating point number. In order to
724ensure that the literal is represented accurately with the current
725precision, it must be evaluated at runtime.</p>
726<h3><a name="FOOT4" href="#DOCF4">(4)</a></h3>
727<p>Could be removed in case a deterministic behavior for floating point operations is required.</p>
728</div>
729<hr>
730
731
732
733</body>
734</html>
735