1
0
mirror of https://github.com/MikeMcl/decimal.js.git synced 2024-10-27 20:34:12 +00:00
MikeMcl_decimal.js/test/floor.js
Michael Mclaughlin b141f3480d v2.0.0
Avoid potential confusion over the round method: ceil, floor, round and
trunc no longer accept arguments and so they match their JS Math object
equivalents. Removed toInteger as round now handles rounding to integer. Added
toSignificantDigits as round no longer rounds to precision. Updated tests
accordingly. Calling config without argument no longer throws.
2014-04-10 19:30:38 +01:00

227 lines
7.7 KiB
JavaScript

var count = (function floor(Decimal) {
var start = +new Date(),
log,
error,
passed = 0,
total = 0;
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 assertException(func, message) {
var actual;
total++;
try {
func();
} catch (e) {
actual = e;
}
if (actual && actual.name == 'Decimal Error') {
passed++;
//log('\n Expected and actual: ' + actual);
} else {
error('\n Test number: ' + total + ' failed');
error('\n Expected: ' + message + ' to raise a Decimal Error.');
error(' Actual: ' + (actual || 'no exception'));
//process.exit();
}
}
function T(expected, value) {
assert(String(expected), new Decimal(String(value)).floor().toString());
}
log('\n Testing floor...');
Decimal.config({
precision: 20,
rounding: 4,
toExpNeg: -1e3,
toExpPos: 1e3,
minE: -9e15,
maxE: 9e15,
errors: true
});
T('-2075365', '-2075364.364286541923');
T('60593539780450631', '60593539780450631');
T('65937898671515', '65937898671515');
T('-39719494751819198566799', '-39719494751819198566798.578');
T('92627382695288166556', '92627382695288166556.8683774524284866028260448205069');
T('-881574', '-881574');
T('-3633239210', '-3633239209.654526163275621746013315304191073405508491056');
T('-23970335459820625362', '-23970335459820625362');
T('131869457416154038', '131869457416154038');
T('-2685', '-2685');
T('-4542227861', '-4542227860.9511298545226');
T('-834103872107533086', '-834103872107533086');
T('-1501493189970436', '-1501493189970435.74866616700317');
T('70591', '70591.2244675522123484658978887');
T('4446128540401735117', '4446128540401735117.435836700611264749985822486641350492901');
T('-597273', '-597273');
T('729117', '729117.5');
T('-729118', '-729117.001');
T('4803729546823170064608098091', '4803729546823170064608098091');
T('-6581532150677269472830', '-6581532150677269472829.38194951340848938896000325718062365494');
T('2949426983040959', '2949426983040959.8911208825380208568451907');
T('25166', '25166.125888418871654557352055849116604612621573251770362');
T('4560569286495', '4560569286495.98300685103599898554605198');
T('13', '13.763105480576616251068323541559825687');
T('9050999219306', '9050999219306.7846946346757664893036971777');
T('39900924', '39900924.00000005');
T('115911043168452445', '115911043168452445');
T('20962819101135667464733349383', '20962819101135667464733349383.8959025798517496777183');
T('4125789711001606948191', '4125789711001606948191.4707575965791242737346836');
T('-6935502', '-6935501.294727166142750626019282');
T('-2', '-1.518418076611593764852321765899');
T('-35416', '-35416');
T('6912783515683955988122411164548', '6912783515683955988122411164548.393');
T('657', '657.0353902852');
T('0', '0.00000000000000000000000017921822306362413915');
T('1483059355427939255846407887', '1483059355427939255846407887.011361095342689876');
T('7.722e+999999999999999', '7.722e+999999999999999');
T('7722', '7722');
T('0', '0.00000005');
T('8551283060956479352', '8551283060956479352.5707396');
T('0', '0.000000000000000000000000019904267');
T('321978830777554620127500539', '321978830777554620127500539.339278568133088682532238002577');
T('2073', '2073.532654804291079327244387978249477171032485250998396');
T('677676305591', '677676305591.2');
T('39181479479778357', '39181479479778357');
T('0', '0.00000000000000000087964700066672916651');
T('115083055948552475', '115083055948552475');
T('9105942082143427451223', '9105942082143427451223');
T('0', '0.00000000000000000000004');
T('0', '0.000250427721966583680168028884692015623739');
T('0', '0.000000000001585613219016120158734661293405081934');
T('0', '0.00009');
T('0', '0.000000090358252973411013592234');
T('276312604693909858427', '276312604693909858427.21965306055697011390137926559');
T('0', '0.0000252');
// ---------------------------------------------------------------- v8 start
T(0, 0);
T(0, '0.000');
T(-0, -0);
T(Infinity, Infinity);
T(-Infinity, -Infinity);
T(NaN, NaN);
T(0, 0.1);
T(0, 0.49999999999999994);
T(0, 0.5);
T(0, 0.7);
T(-1, -0.1);
T(-1, -0.49999999999999994);
T(-1, -0.5);
T(-1, -0.7);
T(1, 1);
T(1, 1.1);
T(1, 1.5);
T(1, 1.7);
T(-1, -1);
T(-2, -1.1);
T(-2, -1.5);
T(-2, -1.7);
Decimal.toExpNeg = -100;
Decimal.toExpPos = 100;
T(-1, -1e-308);
T(-1e308, -1e308);
T('2.1e+308', '2.1e308');
T(-1, '-1e-999');
T(0, '1e-999');
T(0, Number.MIN_VALUE);
T(-1, -Number.MIN_VALUE);
T(Number.MAX_VALUE, Number.MAX_VALUE);
T(-Number.MAX_VALUE, -Number.MAX_VALUE);
T(Infinity, Infinity);
T(-Infinity, -Infinity);
var two_30 = 1 << 30;
T(two_30, two_30);
T(two_30, two_30 + 0.1);
T(two_30, two_30 + 0.5);
T(two_30, two_30 + 0.7);
T(two_30 - 1, two_30 - 1);
T(two_30 - 1, two_30 - 1 + 0.1);
T(two_30 - 1, two_30 - 1 + 0.5);
T(two_30 - 1, two_30 - 1 + 0.7);
T(-two_30, -two_30);
T(-two_30, -two_30 + 0.1);
T(-two_30, -two_30 + 0.5);
T(-two_30, -two_30 + 0.7);
T(-two_30 + 1, -two_30 + 1);
T(-two_30 + 1, -two_30 + 1 + 0.1);
T(-two_30 + 1, -two_30 + 1 + 0.5);
T(-two_30 + 1, -two_30 + 1 + 0.7);
var two_52 = (1 << 30) * (1 << 22);
T(two_52, two_52);
T(two_52, two_52 + 0.1);
T(two_52, two_52 + 0.5);
T(two_52 + 1, two_52 + 0.7);
T(two_52 - 1, two_52 - 1);
T(two_52 - 1, two_52 - 1 + 0.1);
T(two_52 - 1, two_52 - 1 + 0.5);
T(two_52 - 1, two_52 - 1 + 0.7);
T(-two_52, -two_52);
T(-two_52, -two_52 + 0.1);
T(-two_52, -two_52 + 0.5);
T(-two_52, -two_52 + 0.7);
T(-two_52 + 1, -two_52 + 1);
T(-two_52 + 1, -two_52 + 1 + 0.1);
T(-two_52 + 1, -two_52 + 1 + 0.5);
T(-two_52 + 1, -two_52 + 1 + 0.7);
// ------------------------------------------------------------------ v8 end
assert('1', new Decimal('1.9999999999').floor().toString());
T('0', '1e-9000000000000000');
T('-1', '-1e-9000000000000000');
T('0', '-9.9e-9000000000000001');
T('9.999999e+9000000000000000', '9.999999e+9000000000000000');
T('-9.999999e+9000000000000000', '-9.999999e+9000000000000000');
T('Infinity', '1E9000000000000001');
T('-Infinity', '-1e+9000000000000001');
T('5.5879983320336874473209567979e+287894365', '5.5879983320336874473209567979e+287894365');
T('-5.5879983320336874473209567979e+287894365', '-5.5879983320336874473209567979e+287894365');
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;