1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00

Shortened test lengths, and made the shifts slightly faster.

This commit is contained in:
Favian Contreras 2014-12-08 03:05:05 -08:00
parent dd381f11c7
commit c3fd516671
7 changed files with 29657 additions and 30312 deletions

View File

@ -12,7 +12,7 @@
*/
var bitwise, convertBase, DecimalConstructor, noConflict,
var convertBase, DecimalConstructor, noConflict,
crypto = global['crypto'],
external = true,
id = 0,
@ -392,12 +392,22 @@
*/
P['leftShift'] = function (n) {
var Decimal = this['constructor'],
x = this['trunc'](),
x = this['trunc']();
var twoPowN = null;
if (typeof n === 'object' || !isFinite(n) || +n >= 50) {
n = new Decimal(n)['trunc']();
} else {
if (isNaN(parseInt(n))) {
return new Decimal(NaN);
}
twoPowN = new Decimal(mathpow(2, n | 0));
n = new Decimal(n | 0);
}
// Are both infinity or is shift amount negative or amount is negative and shift is infinite?
if (!x['s'] || !n['s'] || (n['s'] < 0 && !n['isZero']()) ||
(!this['c'] && !n['c']) || (this['s'] < 0 && !n['c'])) {
(!x['c'] && !n['c']) || (x['s'] < 0 && !n['c'])) {
return new Decimal(NaN);
}
if (x['isZero']() || n['isZero']()) {
@ -408,7 +418,10 @@
external = false;
Decimal['precision'] = MAX_DIGITS;
var outVal = x['times'](new Decimal(2)['pow'](n));
if (!twoPowN) {
twoPowN = new Decimal(2)['pow'](n);
}
var outVal = x['times'](twoPowN);
external = true;
Decimal['precision'] = prevPrec;
@ -1091,6 +1104,7 @@
* -I >> n = -I
* -I >> I = -I
* n >> I = I
* -n >> I = -1
* 0 >> n = 0
*
* Return a new Decimal whose value is this Decimal >> n, rounded to precision
@ -1099,12 +1113,21 @@
*/
P['rightShift'] = function (n) {
var Decimal = this['constructor'],
x = this['trunc'](),
x = this['trunc']();
var twoPowN = null;
if (typeof n === 'object' || !isFinite(n) || +n >= 50) {
n = new Decimal(n)['trunc']();
} else {
if (isNaN(parseInt(n))) {
return new Decimal(NaN);
}
twoPowN = new Decimal(mathpow(2, n | 0));
n = new Decimal(n | 0);
}
// Are both infinity or is shift amount negative or amount is negative and shift is infinite?
if (!x['s'] || !n['s'] || (n['s'] < 0 && !n['isZero']()) ||
(!x['c'] && !n['c'])) {
if (!x['s'] || !n['s'] || (n['s'] < 0 && !n['isZero']()) || (!x['c'] && !n['c'])) {
return new Decimal(NaN);
}
if (x['isZero']() || n['isZero']() || x['eq'](-1)) {
@ -1117,7 +1140,10 @@
external = false;
Decimal['precision'] = MAX_DIGITS;
var outVal = x['div'](new Decimal(2)['pow'](n))['floor']();
if (!twoPowN) {
twoPowN = new Decimal(2)['pow'](n);
}
var outVal = x['div'](twoPowN)['floor']();
external = true;
Decimal['precision'] = prevPrec;

19880
test/and.js

File diff suppressed because it is too large Load Diff

View File

@ -42,7 +42,7 @@ var count = (function leftShift(Decimal) {
assert(String(expected), String(new Decimal(leftShiftendA).leftShift(leftShiftendB)));
}
log('\n Testing left shift...');
log('\n Testing leftShift...');
Decimal.config({
precision: 20,

19874
test/or.js

File diff suppressed because it is too large Load Diff

View File

@ -1,292 +0,0 @@
import random
from decimal import *
header_str = """var count = (function or(Decimal) {
var start = +new Date(),
log,
error,
undefined,
passed = 0,
total = 0,
n = 'null',
N = 'NaN',
I = 'Infinity';
if (typeof window === 'undefined') {
log = console.log;
error = console.error;
} else {
log = function (str) { document.body.innerHTML += str.replace('\\n', '<br>') };
error = function (str) { document.body.innerHTML += '<div style="color: red">' +
str.replace('\\n', '<br>') + '</div>' };
}
if (!Decimal && typeof require === 'function') Decimal = require('../decimal');
function assert(expected, actual) {
total++;
if (expected !== actual) {
error('\\n Test number: ' + total + ' failed');
error(' Expected: ' + expected);
error(' Actual: ' + actual);
//process.exit();
} else {
passed++;
//log('\\n Expected and actual: ' + actual);
}
}
function T(orendA, orendB, expected, sd, rm) {
if ( sd != null ) {
Decimal.precision = sd;
Decimal.rounding = rm;
}
assert(String(expected), String(new Decimal(orendA).or(orendB)));
}
log('\\n Testing or...');
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -9e15,
toExpPos: 9e15,
minE: -9e15,
maxE: 9e15,
errors: false
});
T(1, 0, 1);
T(1, -0, 1);
T(-1, 0, -1);
T(-1, -0, -1);
T(1, N, N);
T(-1, N, N);
T(1, I, I);
T(1, -I, -I);
T(-1, I, -1);
T(-1, -I, -1);
T(0, 1, 1);
T(0, -1, -1);
T(-0, 1, 1);
T(-0, -1, -1);
T(0, N, N);
T(-0, N, N);
T(0, I, I);
T(0, -I, -I);
T(-0, I, I);
T(-0, -I, -I);
T(N, 1, N);
T(N, -1, N);
T(N, 0, N);
T(N, -0, N);
T(N, N, N);
T(N, I, N);
T(N, -I, N);
T(I, 1, I);
T(I, -1, -1);
T(-I, 1, -I);
T(-I, -1, -1);
T(I, 0, I);
T(I, -0, I);
T(-I, 0, -I);
T(-I, -0, -I);
T(I, N, N);
T(-I, N, N);
T(I, I, I);
T(I, -I, -1);
T(-I, I, -1);
T(-I, -I, -I);
T(1, '0', '1');
T(1, '1', '1');
T(1, '-45', '-45');
T(1, '22', '23');
T(1, '0144', '145');
T(1, '6.1915', '7');
T(1, '-1.02', '-1');
T(1, '0.09', '1');
T(1, '-0.0001', '1');
T(1, '8e5', '800001');
T(1, '9E12', '9000000000001');
T(1, '1e-14', '1');
T(1, '3.345E-9', '1');
T(1, '-345.43e+4', '-3454299');
T(1, '-94.12E+0', '-93');
T(1, '', N);
T(1, ' ', N);
T(1, '\\t\\t', N);
T(1, 'ertgrt546', N);
T(1, 'qweqwdewee', N);
T(1, true, N);
T(1, false, N);
T(1, 'e 4.3', N);
T(1, '4 .3', N);
T(1, '4.0 01e', N);
T(1, ' 4.001', '5');
T(1, '4.001 ', '5');
T(1, ' 4.001 ', '5');
T(1, ' 4.001', '5');
T(1, ' 4.0 01', N);
T(1, '4. 001', N);
T(1, '4. 001 ', N);
T(1, ' 4.001e ', N);
T(1, ' 4 .001 e ', N);
T(1, undefined, N);
T(1, null, N);
T(1, Number.POSITIVE_INFINITY, I);
T(1, Number.NEGATIVE_INFINITY, -I);
T(1, new Date(2012, 11, 4), N);
T(1, new Object(), N);
T(1, function () {}, N);
T('0', 0, '0');
T(0, '+0', '0');
T('0', '0', '0');
T(3, -0, '3');
T(9.654, 0, '9');
T(0, '0.001', '0');
T(0, '111.1111111110000', '111');
T(N, '0', N);
T(-1, 1, '-1');
T(-0.01, 0.01, '0');
T(54, -54, '-2');
T(9.99, '-9.99', '-1');
T('0.0000023432495704937', '-0.0000023432495704937', '0');
T(NaN, NaN, N);
T(NaN, N, N);
T(N, NaN, N);
T(N, 4, N);
T(N, '4534534.45435435', N);
T(N, 99999.999, N);
T(Infinity, '354.345341', I);
T(3, -I, -I);
T(-Infinity, -I, -I);
T(-I, -Infinity, -I);
T(I, '-999e999', -1);
T('1.21123e43', -I, -I);
T('-999.0', I, -1);
T('657.342e-45', -I, -I);
T(I, 123, I);
T(100, 100, '100');
T(-999.99, '0.01', '-999');
T('10 ', 4, '14');
T('03.333', -4, '-1');
T(-1, -0.1, '-1');
T(43534.5435, '0.054645', '43534');
T('99999', '1', '99999');
T('3e', 8, N);
T('-3..0', 13, N);
T('0 0', -0.4, N);
T(' +3e0', 4, '7');
T(9.9806, '+ 1', N);
T(' +2 0', '1e1', N);
T('e3', 4, N);
T(' ', 0, N);
T(323, null, N);
T(undefined, undefined, N);
T('undefined', undefined, N);
T(null, null, N);
Decimal.errors = true;
"""
modes = [
'ROUND_UP',
'ROUND_DOWN',
'ROUND_CEILING',
'ROUND_FLOOR',
'ROUND_HALF_UP',
'ROUND_HALF_DOWN',
'ROUND_HALF_EVEN',
'ROUND_05UP'
]
def getRand(i,d):
return Decimal('%d.%0*d' % (random.randint(0, 10**i-1), d, random.randint(0, 10**d-1)))
def getRandExp(max_digits=20, max_exp=3):
s = '' if random.randint(1, 2) == 1 else '-'
s += str(random.randint(1, 9)) + '.'
for i in range(random.randint(0, max_digits - 1)):
s += str(random.randint(0, 9))
s += 'e-' if random.randint(1, 4) == 1 else 'e+'
for i in range(random.randint(1, max_exp)):
s += str(random.randint(0, 9))
try:
d = Decimal(s)
except Exception, e:
d = getRandExp(max_digits, max_exp)
return d
def sci_str(dec):
return ('{:.' + str(len(str(dec)) - 1) + 'e}').format(dec)
file = open('or.js', 'w')
file.write(header_str + '\n')
ctx = getcontext()
ctx.Emax = 9000000000000000
ctx.Emin = -9000000000000000
max = Decimal('9.9e+300')
min = Decimal('-9.9e300')
i = 0
biggest = Decimal('0')
while i < 200000:
x = getRandExp()
y = getRandExp()
if ((x < min or x > max or x == Decimal('0')) or
(y < min or y > max or y == Decimal('0'))):
continue
# Maximum precision of result.
pr = random.randint(1, 100)
# Rounding mode
rm = random.randint(0, 6)
result_rm = modes[rm]
ctx.prec = pr
ctx.rounding = result_rm
try:
result = Decimal(int(x) | int(y))
except Exception, e:
print(e.message)
print( str(i) + ': sqrt(' + str(x) + ')' )
continue
if result < min or result > max or result == Decimal('0') or result == Decimal('1'):
continue
result = ( " T('" + str(x) + "', '" + str(y) + "', '" + str(result) + "', " + str(pr) + ", " + str(rm) + ");\n" )
if x > biggest:
biggest = x
if y > biggest:
biggest = y
if i % 100 == 0:
print(i)
file.write(result)
#print(result)
i += 1
print('biggest: ' + sci_str(biggest))
file.write("""
log('\\n ' + passed + ' of ' + total + ' tests passed in ' + (+new Date() - start) + ' ms \\n');
return [passed, total];
})(this.Decimal);
if (typeof module !== 'undefined' && module.exports) module.exports = count;""")
file.close()

View File

@ -42,7 +42,7 @@ var count = (function rightShift(Decimal) {
assert(String(expected), String(new Decimal(rightShiftendA).rightShift(rightShiftendB)));
}
log('\n Testing right shift...');
log('\n Testing rightShift...');
Decimal.config({
precision: 20,
@ -1893,23 +1893,18 @@ var count = (function rightShift(Decimal) {
T('1.67E+304', '7.56839720', '130468750000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 40, 4);
T('8.540176E+865', '5', '2668805000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 43, 3);
T('-8.65523103E+187', '51.19', '-38436947091824436029128264635801315307617187500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 90, 5);
T('2.783203540E+13', '1.335149', '13916017700000', 37, 1);
T('91002165.6938343301644457460443', '8.380659302', '355477', 78, 2);
T('-9.739432200030021516964707E+632', '3.77', '-121742902500375268962058837500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 21, 0);
T('1.655E+717', '3.194887192', '206875000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 97, 3);
T('-2.22356266282863426539952810372E+75', '28.2721', '-8283416415857651328293711333423852920532226562500000000000000000000', 89, 2);
T('2.792922831183826E+978', '2.64251', '698230707795956500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 29, 6);
T('3.955220189133E+849', '6.27', '61800315455203125000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 83, 0);
T('-5.4E+7', '2.66892', '-13500000', 44, 5);
T('3829.98253', '5.9', '119', 80, 2);
T('-6.521396866E+734', '41.99', '-296558794889278942719101905822753906250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 7, 0);
T('-2.8620310219356424158490882E+998', '9.7137100', '-558990433971805159345525039062500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 88, 6);
T('5.1038E+11', '5', '15949375000', 14, 4);
T('5.32698489241189564E+35', '73.73095', '56401646417484', 94, 2);
T('-5.9504735827E+28', '6.1', '-929761497296875000000000000', 69, 2);
T('-4.70855128928E+94', '4.8', '-2942844555800000000000000000000000000000000000000000000000000000000000000000000000000000000000', 90, 1);
T('-1.97598115221061832646450399E+47', '2.64922', '-49399528805265458161612599750000000000000000000', 57, 6);
T('-7789615727.2', '12.4740356', '-1901762', 93, 0);
T('9.4736860E+268', '6.0457786', '1480263437500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 37, 6);
T('3.725248911907549E+270', '7.8', '29103507124277726562500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 74, 5);
T('-2.878501167449676987787558551E+962', '80.846', '-238104035892562909770975952225867727901609770257973508478244184516370296478271484375000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 51, 5);
@ -1984,7 +1979,6 @@ var count = (function rightShift(Decimal) {
T('-45284.1218731496141', '9.3613462', '-89', 90, 5);
T('9.6052762747037758743E+945', '96.8059', '121235630991371659105091011320369078640002272802961291731094206625130027532577514648437500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 32, 0);
T('-5.65149998701316861473377963E+220', '3.602290', '-7064374983766460768417224537500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 58, 3);
T('3.25443E+47', '18', '1241466522216796875000000000000000000000000', 72, 1);
T('-4.8771429791080564591192192E+69', '34.57644', '-283887084754420005432284623384475708007812500000000000000000', 43, 3);
T('5.20014075664859E+599', '8.10891', '2031304983065855468750000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 25, 2);
T('-6.2243329679656880219E+619', '6.853', '-972552026244638753421875000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000', 94, 1);

19871
test/xor.js

File diff suppressed because it is too large Load Diff