to-exponential
Version:
Converts any big/small/precise decimal number represented as String, to exponential notation.
162 lines (153 loc) • 5.71 kB
JavaScript
const toExponential = require('./index.js');
const addBigInts = toExponential.addBigInts;
const chai = require('chai');
const expect = chai.expect; // eslint-disable-line no-unused-vars
chai.should();
const sizeOfRandomTests = 30;
describe('toExponential()', function() {
describe('preset test cases', function() {
[
['5.0', '5e+0'],
['10.5', '1.05e+1'],
['0.105E2', '1.05e+1'],
['30e-1', '3e+0'],
['30.0e-1', '3e+0'],
['0.3e1', '3e+0'],
['00.30e1', '3e+0'],
['-12345.678e-2', '-1.2345678e+2'],
['-123.45E-1' , '-1.2345e+1'],
[ '12' , '1.2e+1'],
['-12' , '-1.2e+1'],
[ '12.' , '1.2e+1'],
['-12.' , '-1.2e+1'],
[ '12.34' , '1.234e+1'],
['-12.34' , '-1.234e+1'],
[ '12e56' , '1.2e+57'],
['-12e56' , '-1.2e+57'],
[ '12.34e56', '1.234e+57'],
['-12.34e56', '-1.234e+57'],
['1000.0e-2', '1e+1'],
['0.00100e4', '1e+1'],
['-12345' , '-1.2345e+4'],
['-0012345', '-1.2345e+4'],
[ '12300' , '1.23e+4'],
[ '1230.0' , '1.23e+3'],
['-12345.678e-2', '-1.2345678e+2'],
['-12345.678e+2', '-1.2345678e+6'],
[ '12345.678e2' , '1.2345678e+6'],
[ '.678', '6.78e-1'],
['+.678', '6.78e-1'],
['+.9e2', '9e+1'],
[ '1234.5e+0' , '1.2345e+3'],
['001234.500e+0', '1.2345e+3'],
['001234.000e+0', '1.234e+3'],
[ '0.0012345e+0', '1.2345e-3'],
['00.0001230e10', '1.23e+6'],
['000.00000e+000', '0e+0'],
['000.00e+010' , '0e+0'],
['000' , '0e+0'],
[ '.0' , '0e+0'],
[ '0' , '0e+0'],
['-000.0067800000E5001' , '-6.78e+4998'],
[ '000.0067800000E5001' , '6.78e+4998'],
['-000.0067800000E-5001', '-6.78e-5004'],
[ '000.0067800000E-5001', '6.78e-5004'],
['-12324.3434716349863831487632112E-5642',
'-1.23243434716349863831487632112e-5638'],
['34155446886984000e-16', '3.4155446886984e+0'],
[ '10000e9998' , '1e+10002'], // pos exp, pos add.
['0.0001e10002' , '1e+9998' ], // pos exp, neg add = pos exp, subtr.
[ '10000e-10002', '1e-9998' ], // neg exp, pos add = neg(pos exp, sub).
['0.0001e-9998' , '1e-10002'], // neg exp, neg add = neg(pos exp, add).
['1234e+10203040506070802', '1.234e+10203040506070805'],
['-12324.3434716349863831487632112E-561640934731461387461933142',
'-1.23243434716349863831487632112e-561640934731461387461933138'],
['', false],
['NaN', false],
['5a', false],
[ 'test', false],
[ '', false],
[ '.', false],
[ '+', false],
[ '-', false],
[ '+e2', false],
[ '+.e2', false],
['1.0.1', false],
['300000000000000000000000000000000000000000000000000e1000', '3e+1050'],
].forEach(pair => {
it('correctly converts ' + pair[0] + ' to ' + pair[1], function() {
toExponential(pair[0]).should.equal(pair[1]);
});
});
});
describe('random tests, should equal Number.toExponential()', function() {
var rand = () =>
((Math.random() - 0.5) * 1e2).toString().substring(0, 12) + // Short rand
'e' + ~~((Math.random() - 0.5) * 100); // An exponent that JS can handle.
for (var i = 0; i < sizeOfRandomTests; i++) {
var a = rand();
var b = toExponential(a);
it('correctly converts ' + a + ' to ' + b, function() {
Number(a).toExponential().should.equal(b);
});
}
});
});
describe('addBigInts()', function() {
describe('preset test cases', function() {
[
[ '0', '0', '0'],
['-00', '-000', '0'],
[ '1', '1', '2'],
[ '1', '-1', '0'],
['-1', '1', '0'],
['-1', '-1', '-2'],
[ '16', '16', '32'],
[ '16', '-16', '0'],
['-16', '16', '0'],
['-16', '-16', '-32'],
[ '51', '52', '103'],
['-51', '52', '1'],
[ '51', '-52', '-1'],
['-51', '-52', '-103'],
[ '0', '0', '0'],
[ '1', '2', '3'],
[ '01', '02', '3'],
[ '10', '1', '11'],
[ '1', '10', '11'],
['0001', '002', '3'],
[ '999', '1', '1000'],
[ '123', '456', '579'],
[ '999', '999', '1998'],
[ '9998', '4', '10002'],
[ '10002', '-4', '9998'],
['-10002', '4', '-9998'],
[ '-9998', '-4', '-10002'],
['10203040506070892', '33', '10203040506070925'],
['33', '10203040506070892', '10203040506070925'],
['-561640934731461387461933142', '49', '-561640934731461387461933093'],
['49', '-561640934731461387461933142', '-561640934731461387461933093'],
['8565425879963214589', '-8565425879963214589', '0'],
[ '8565425879963214589632547896200778565425879963214',
'8565425879963214589632547896200778565425879963214',
'17130851759926429179265095792401557130851759926428' ],
].forEach(arr => {
it('finds that ' + arr[0] + ' + ' + arr[1] + ' = ' + arr[2], function() {
addBigInts(arr[0], arr[1]).should.equal(arr[2]);
});
});
});
describe('random tests, should equal Number addition', function() {
var rand = () =>
( ~~((Math.random() - 0.5) * 1e12) ).toString() // Get large integer.
.substring(0, 2 + 10 * Math.random()); // Cut-off to a variable length.
for (var i = 0; i < sizeOfRandomTests; i++) {
var a = rand();
var b = rand();
var c = (Number(a) + Number(b)).toString();
it('finds that ' + a + ' + ' + b + ' = ' + c, function() {
addBigInts(a, b).should.equal(c);
});
}
});
});