### Deno Installation Source: https://github.com/mikemcl/big.js/blob/main/README.md Provides instructions for importing the big.js library into a Deno project using URLs from GitHub or unpkg. ```javascript import Big from 'https://raw.githubusercontent.com/mikemcl/big.js/v7.0.1/big.mjs'; import Big from 'https://unpkg.com/big.js@latest/big.mjs'; ``` -------------------------------- ### Node.js Installation and Usage Source: https://github.com/mikemcl/big.js/blob/main/README.md Shows how to install big.js using npm and import it into a Node.js project using either CommonJS or ES module syntax. ```bash $ npm install big.js ``` ```javascript const Big = require('big.js'); import Big from 'big.js'; ``` -------------------------------- ### Browser Installation Source: https://github.com/mikemcl/big.js/blob/main/README.md Demonstrates how to include the big.js library in a web page using script tags, including options for a CDN-hosted minified version and ES module import. ```html ``` -------------------------------- ### TypeScript Integration for Big.js Source: https://github.com/mikemcl/big.js/blob/main/README.md How to install the TypeScript type definitions for Big.js from the DefinitelyTyped project. This enables better type checking and autocompletion in TypeScript projects. ```bash $ npm install --save-dev @types/big.js ``` -------------------------------- ### DOM Element Initialization Source: https://github.com/mikemcl/big.js/blob/main/perf/big-vs-bigdecimal.html Initializes references to various HTML elements used in the benchmark interface, such as input fields, buttons, and display areas. This setup is crucial for interacting with the user interface and displaying results. ```JavaScript var $( id ) { return document.getElementById(id); }; $INPUTS = document.getElementsByTagName('input'), $BD = $( 'bd' ), $BIGINT = $( 'bigint' ), $DIGITS = $( 'digits' ), $GWT = $( 'gwt' ), $ICU4J = $( 'icu4j' ), $FIX = $( 'fix' ), $MAX = $( 'max' ), $INT = $( 'int' ), $DIV = $( 'division' ), $DP = $( 'dp' ), $R = $( 'r' ), $REPS = $( 'reps' ), $SHOW = $( 'show' ), $START = $( 'start' ), $COUNTER = $( 'counter' ), $TIME = $( 'time' ), $RESULTS = $( 'results' ); ``` -------------------------------- ### Big.js Error Handling Source: https://github.com/mikemcl/big.js/blob/main/docs/index.html Details the types of errors thrown by Big.js, which are instances of the `Error` class. Error messages typically start with '[big.js]'. The table lists methods, specific error messages, and the conditions under which they are thrown. ```APIDOC Errors: All errors thrown are instances of `Error`. Error messages always begin with `[big.js]`. Method(s) | Error message | Thrown on/when ----------|---------------|---------------- `Big`, `cmp`, `div`, `eq`, `gt`, `gte`, `lt`, `lte`, `minus`, `mod`, `plus`, `times` | Invalid value | Invalid value `Big` | String expected | `Big.strict` is `true` `div` | Division by zero | Division by zero `div` | Invalid decimal places | Invalid `Big.DP` `div` | Invalid rounding mode | Invalid `Big.RM` `mod` | Division by zero | Modulo zero `pow` | Invalid exponent | Invalid exponent `pow` | Invalid decimal places | Invalid `Big.DP` `pow` | Invalid rounding mode | Invalid `Big.RM` `prec` | Invalid precision | Invalid `sd` `prec` | Invalid rounding mode | Invalid `rm`/`Big.RM` `round` | Invalid decimal places | Invalid `dp` `round` | Invalid rounding mode | Invalid `rm`/`Big.RM` `sqrt` | No square root | Negative number `sqrt` | Invalid decimal places | Invalid `Big.DP` `sqrt` | Invalid rounding mode | Invalid `Big.RM` `toExponential` | Invalid decimal places | Invalid `dp` `toExponential` | Invalid rounding mode | Invalid `Big.RM` `toFixed` | Invalid decimal places | Invalid `dp` `toFixed` | Invalid rounding mode | Invalid `Big.RM` `toNumber` | Imprecise conversion | `Big.strict` is `true` `toPrecision` | Invalid precision | Invalid `sd` `toPrecision` | Invalid rounding mode | Invalid `Big.RM` `valueOf` | valueOf disallowed | `Big.strict` is `true` ``` -------------------------------- ### Running Big.js Tests Source: https://github.com/mikemcl/big.js/blob/main/README.md Instructions for running the test suite for Big.js. This includes commands for running all tests using npm and for testing a single method using Node.js. ```bash $ npm test ``` ```bash $ node test/toFixed ``` -------------------------------- ### bignumber.js Configuration and Addition Source: https://github.com/mikemcl/big.js/wiki/Home Demonstrates configuring decimal places and rounding mode in bignumber.js and performing an addition operation. ```javascript Bignumber.config({ DECIMAL_PLACES: 3, ROUNDING_MODE; 1 }); var x = new BigNumber('123.456789'); x.plus(1).toString(); // '124.456789' ``` -------------------------------- ### big.js Configuration and Division Source: https://github.com/mikemcl/big.js/wiki/Home Demonstrates how to configure decimal places and rounding mode in big.js and performs a division operation. ```javascript Big.DP = 7; Big.RM = 4; // round half-up var x = new Big(5); x.div(3).toString(); // '1.6666667' ``` -------------------------------- ### decimal.js Configuration and Addition Source: https://github.com/mikemcl/big.js/wiki/Home Shows how to set precision and rounding mode in decimal.js and perform an addition operation, demonstrating its precision handling. ```javascript Decimal.set({ precision: 7, rounding: 4 }); var y = new Decimal('123.456789'); y.plus(1).toString(); // '124.4568' ``` -------------------------------- ### Big.js Library Initialization and Core Methods Source: https://github.com/mikemcl/big.js/blob/main/docs/index.html Provides an overview of the Big.js library's structure, including its internal constants, error handling, and the implementation of core arithmetic methods like absolute value, comparison, division, equality, greater than, greater than or equal to, less than, less than or equal to, subtraction, and addition. ```javascript /* big.js v7.0.0 https://github.com/MikeMcl/big.js/LICENCE.md */ /* terser big.js -c -m -o big.min.js */ !function(r){"use strict";var e,t=1e6,n=1e6,i="[big.js] ",o=i+"Invalid ",s=o+"decimal places",c=o+"rounding mode",f=i+"Division by zero",u={},h=void 0,l=/^-?(\d+(\.\d*)?|\.\d+)(e[+-]?\d+)?$/i; function a(r,e,t,n){var i=r.c;if(t===h&&(t=r.constructor.RM),0!==t&&1!==t&&2!==t&&3!==t)throw Error(c); if(e<1)n=3===t&&(n||!!i[0])||0===e&&(1===t&&i[0]>=5||2===t&&(i[0]>5||5===i[0]&&(n||i[1]!==h))), i.length=1,n?(r.e=r.e-e+1,i[0]=1):i[0]=r.e=0; else if(e=5||2===t&&(i[e]>5||5===i[e]&&(n||i[e+1]!==h||1&i[e-1]))||3===t&&(n||!!i[0])), i.length=e,n) for(;++i[--e]>9;)if(i[e]=0,0===e){++r.e,i.unshift(1);break} for(e=i.length;!i[--e];)i.pop()} return r} function p(r,e,t){var n=r.e,i=r.c.join(""),o=i.length; if(e)i=i.charAt(0)+(o>1?"."+i.slice(1):"")+(n<0?"e":"e+")+n; else if(n<0){for(;++n;)i="0"+i;i="0."+i} else if(n>0)if(++n>o)for(n-=o;n--;)i+="0";else n1&&(i=i.charAt(0)+"."+i.slice(1)); return r.s<0&&t?"-"+i:i} u.abs=function(){var r=new this.constructor(this);return r.s=1,r}, u.cmp=function(r){var e,t=this,n=t.c,i=(r=new t.constructor(r)).c,o=t.s,s=r.s,c=t.e,f=r.e; if(!n[0]||!i[0])return n[0]?o:i[0]?-s:0; if(o!=s)return o; if(e=o<0,c!=f)return c>f^e?1:-1; for(s=(c=n.length)<(f=i.length)?c:f,o=-1;++oi[o]^e?1:-1; return c==f?0:c>f^e?1:-1}, u.div=function(r){var e=this,n=e.constructor,i=e.c,o=(r=new n(r)).c,c=e.s==r.s?1:-1,u=n.DP; if(u!==~~u||u<0||u>t)throw Error(s); if(!o[0])throw Error(f); if(!i[0])return r.s=c,r.c=[r.e=0],r; var l,p,g,w,v,d=o.slice(),m=l=o.length,E=i.length,b=i.slice(0,l),P=b.length,D=r,M=D.c=[],x=0,y=u+(D.e=e.e-r.e)+1; for(D.s=c,c=y<0?0:y,d.unshift(0);P++P?1:-1; else for(v=-1,w=0;++vb[v]?1:-1;break} if(!(w<0))break; for(p=P==l?o:d;P;){ if(b[--P]y&&a(D,y,n.RM,b[0]!==h),D}, u.eq=function(r){return 0===this.cmp(r)}, u.gt=function(r){return this.cmp(r)>0}, u.gte=function(r){return this.cmp(r)>-1}, u.lt=function(r){return this.cmp(r)<0}, u.lte=function(r){return this.cmp(r)<1}, u.minus=u.sub=function(r){var e,t,n,i,o=this,s=o.constructor,c=o.s,f=(r=new s(r)).s; if(c!=f)return r.s=-f,o.plus(r); var u=o.c.slice(),h=o.e,l=r.c,a=r.e; if(!u[0]||!l[0])return l[0]?r.s=-f:u[0]?r=new s(o):r.s=1,r; if(c=h-a){for((i=c<0)?(c=-c,n=u):(a=h,n=l),n.reverse(),f=c;f--;)n.push(0);n.reverse()} else for(t=((i=u.length0)for(;f--;)u[e++]=0; for(f=e;t>c;){if(u[--t] other. div(other: number | string | Big): Big Returns this Big number divided by another. eq(other: number | string | Big): boolean Returns true if this Big number is equal to another. gt(other: number | string | Big): boolean Returns true if this Big number is greater than another. gte(other: number | string | Big): boolean Returns true if this Big number is greater than or equal to another. lt(other: number | string | Big): boolean Returns true if this Big number is less than another. lte(other: number | string | Big): boolean Returns true if this Big number is less than or equal to another. minus(other: number | string | Big): Big Returns this Big number minus another. mod(other: number | string | Big): Big Returns the remainder of this Big number divided by another. neg(): Big Returns the negation of this Big number. plus(other: number | string | Big): Big Returns this Big number plus another. pow(other: number | string | Big): Big Returns this Big number raised to the power of another. prec(value: number): Big Sets the precision of this Big number. Parameters: value: The new precision value. round(dp?: number, rm?: number): Big Returns this Big number rounded to a specified number of decimal places and rounding mode. Parameters: dp: The number of decimal places to round to (optional). rm: The rounding mode (optional). sqrt(): Big Returns the square root of this Big number. times(other: number | string | Big): Big Returns this Big number multiplied by another. toExponential(dp?: number): string Returns a string of this Big number in exponential notation. Parameters: dp: The number of digits after the decimal point (optional). toFixed(dp?: number): string Returns a string of this Big number in fixed-point notation. Parameters: dp: The number of digits after the decimal point (optional). toJSON(): string Returns a string representation of this Big number. toNumber(): number Returns a primitive value of this Big number. toPrecision(prec?: number): string Returns a string of this Big number in fixed-point notation or exponential notation depending on its value. Parameters: prec: The number of significant digits (optional). toString(): string Returns a string representation of this Big number. valueOf(): Big Returns a primitive value of this Big number. INSTANCE Properties c: Array The coefficient of this Big number. e: number The exponent of this Big number. s: number The sign of this Big number (-1 for negative, 1 for positive). ``` -------------------------------- ### Configuring Decimal Places and Rounding Mode Source: https://github.com/mikemcl/big.js/blob/main/docs/index.html Explains how to configure the global decimal places (`Big.DP`) and rounding mode (`Big.RM`) for operations. It also shows how to create separate Big.js constructors with custom configurations or redefine prototype methods to pass these settings per operation. ```javascript // Global configuration Big.DP = 10; y = x.sqrt(); Big.DP = 0; Big.RM = 1; z = x.div(3); // Redefining prototype method for per-operation settings Big.prototype.div = (function () { const div = Big.prototype.div; return function (n, dp, rm) { const Big = this.constructor; const DP = Big.DP; const RM = Big.RM; if (dp != undefined) Big.DP = dp; if (rm != undefined) Big.RM = rm; let result = div.call(this, n); Big.DP = DP; Big.RM = RM; return result; } })(); var dp = 10; var round_up = 2; x = x.div(y, dp, round_up); ``` -------------------------------- ### Creating and Using Multiple Big.js Constructors Source: https://github.com/mikemcl/big.js/blob/main/docs/index.html Demonstrates how to create distinct Big.js constructors with custom DP and RM settings. It shows how numbers created from these constructors behave differently in division operations due to their unique configurations. It also highlights that Big numbers from different constructors can be used together, with the operation's settings determined by the Big number the method is called on. ```javascript let Big = require('big.js'); // Create a default Big constructor let bigDefault = Big; // Create a new constructor with default DP and RM let Big10 = Big(); // Set DP for the default constructor Big.DP = 3; // Set DP for the Big10 constructor Big10.DP = 10; // Create numbers using different constructors let x = Big(5); let y = Big10(5); // Perform division operations console.log(x.div(3).toString()); // Output: 1.667 console.log(y.div(3).toString()); // Output: 1.6666666667 // Using numbers from different constructors together console.log(x.plus(y).toString()); // Uses Big.DP (3) for the result ``` -------------------------------- ### Big.js Default Configuration and Initialization Source: https://github.com/mikemcl/big.js/blob/main/perf/big-vs-bigdecimal.html Sets default values for Big.js, including decimal places, rounding mode, and other configuration options. It also handles the loading of the ICU4J BigDecimal script. ```javascript Big.DP = 20; Big.RM = 1; // Set defaults $MAX.checked = $INPUTS[0].checked = $GWT.checked = true; $SHOW.checked = $INT.checked = false; $REPS.value = DEFAULT_REPS; $DIGITS.value = DEFAULT_DIGITS; $DP.value = DEFAULT_DECIMAL_PLACES; $R.option = DEFAULT_ROUNDING; BigDecimal_GWT = BigDecimal; BigDecimal = undefined; // Load ICU4J BigDecimal script = document.createElement("script"); script.src = ICU4J_URL; script.onload = script.onreadystatechange = function () { if (!script.readyState || /loaded|complete/.test(script.readyState)) { script = null; BigDecimal_ICU4J = BigDecimal; $START.onmousedown = begin; } }; document.getElementsByTagName("head")[0].appendChild(script); ``` -------------------------------- ### Big.js Constructor and Configuration Source: https://github.com/mikemcl/big.js/blob/main/docs/index.html Defines the Big.js constructor and its static properties for configuration. This includes setting the default precision (`DP`), rounding mode (`RM`), and exponential notation limits (`NE`, `PE`). The `strict` property controls behavior for imprecise conversions. ```javascript e=function r(){ function e(t){ var n=this; if(!(n instanceof e))return t===h&&0===arguments.length?r():new e(t); if(t instanceof e)n.s=t.s,n.e=t.e,n.c=t.c.slice(); else{ if("string"!=typeof t){if(!0===e.strict&&"bigint"!=typeof t)throw TypeError(o+"value");t=0===t&&1/t<0?"-0":String(t)} !function(r,e){ var t,n,i; if(!l.test(e))throw Error(o+"number"); r.s="-"==e.charAt(0)?(e=e.slice(1),-1):1; (t=e.indexOf("."))>-1&&(e=e.replace(".","")); (n=e.search(/e/i))>0?(t<0&&(t=n),t+=+e.slice(n+1),e=e.substring(0,n)):t<0&&(t=e.length); for(i=e.length,n=0;n0&&"0"==e.charAt(--i);); for(r.e=t-n-1,r.c=[],t=0;n<=i;)r.c[t++]=+e.charAt(n++) }} (n,t) } n.constructor=e } return e.prototype=u,e.DP=20,e.RM=1,e.NE=-7,e.PE=21,e.strict=false,e.roundDown=0,e.roundHalfUp=1,e.roundHalfEven=2,e.roundUp=3,e}(), e.default=e.Big=e,"function"==typeof define&&define.amd?define((function(){return e})):"undefined"!=typeof module&&module.exports?module.exports=e:r.Big=e}(this) ``` -------------------------------- ### Number Formatting Methods Source: https://github.com/mikemcl/big.js/blob/main/README.md Shows how to use the `toExponential`, `toFixed`, and `toPrecision` methods of Big.js to format numbers according to specific requirements. ```javascript x = new Big(255.5) x.toExponential(5) // "2.55500e+2" x.toFixed(5) // "255.50000" x.toPrecision(5) // "255.50" ``` -------------------------------- ### Chaining Big.js Methods Source: https://github.com/mikemcl/big.js/blob/main/README.md Illustrates the ability to chain multiple Big.js arithmetic methods together for concise and readable code. ```javascript x.div(y).plus(z).times(9).minus('1.234567801234567e+8').plus(976.54321).div('2598.11772') x.sqrt().div(y).pow(3).gt(y.mod(z)) // true ```