JSFiddle - React, Tailwind, and code Playground
by Veryape
HTML
BigInt MATH Object
<div id="support_BI"> </div>
<br/> BigInt Fraction constructor
<div id="support_Fraction"> </div>
<br/><br/>
BigInt Denominator: <input type="text" id="denominator" value="2**512" /> <i>2**256 means 2^256 .... error +- 1/denominator </i> <br/>
INT DString comma digits <input type="text" size="10" id="comma_digits" value="150">
PI <input type="radio" name="PIorSQRT2">
SQRT2 <input type="radio" name="PIorSQRT2" checked>
<button id="calc">
calculate
</button>
<br/><br/><br/>
<div>
<p id="fraction_result"> </p><br/><br/>
<p id="DString_result" style="word-wrap:break-word"> </p>
</div>
JavaScript
// ----------------------------------------------------------------------------
//
// File: BI.js
// Version: 1.0
// Creation Date: 06/05/20
// Last Modified: 15/07/20
//
// Purpose: Higher Accuracy with BigInt fractions in calculations than DoubleFloat 64bit
//
// Compatibility: Mozilla Firefox, Google Chrome, Microsoft Edge
//
// ----------------------------------------------------------------------------
"use strict"
const MATH=new Object ();
//*********************************************************
//-------------------------------------- Object BI (BigInt)
//*********************************************************
MATH.BI={
//-- For BigInt and Number
abs:function (a)
{
if (a<0) return -a;
else return a;
},
/* divident, divisor BigInt or Number (both must be the same type!)
returns a BigInt or Number, when division by Zero -> returns a BigInt fraction or NaN */
roundDIV:function (divident, divisor)
{
console.log (divident+" : "+ divisor);
if (divident.constructor==BigInt)
{
let u;
if (divisor==0) //-- returns a infinity fraction
return new this.Fraction (divident,divisor);
u=(divident * 2n)/divisor;
console.log (u/2n);
if (u<0) u-=1n;
else u+=1n;
return u / 2n;
}
else return Math.round (divident/divisor);
},
/* divident, divisor BigInt or Number (both must be the same type!)
returns a BigInt or Number, when division by Zero -> returns a BigInt fraction or NaN */
floorDIV:function (divident, divisor)
{
if (divident.constructor==BigInt)
{
let u, v;
if (divisor==0) //-- returns a infinity fraction
return new this.Fraction (divident,divisor);
u=divident/divisor, v=divident%divisor;
if (u<0 && v!=0) return u-1;
return u;
}
else return Math.floor (divident/divisor);
},
/* divident, divisor | BigInt or Number (both must be the same type!)
returns a BigInt or Number, when division by Zero -> returns a BigInt fraction...