1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
|
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<meta name="Author" content="M Mclaughlin">
<title>Testing Big against Number</title>
<style>
body,ul,form,h1,div{margin:0;padding:0;}
body{font-family:Calibri, Arial, Sans-Serif;}
form{width:48em;border:2px solid #c8c8c8;margin:1.6em auto;}
h1{text-align:center;font-size:1.2em;background-color:#c8c8c8;padding:.6em 0;}
ul{list-style-type:none;color:#555;padding:1em 1em 0 2em;}
.input,ul{background-color:#f5f5f5;}
.input,.output{padding:0 1em 1em 2em;}
.output,.methods{background-color:#e1e1e1;}
.methods{padding-bottom:1em;}
.output{padding-bottom:2em;}
.size{width:80%;}
label{width:10em;color:#000;margin-left:.6em;display:inline-block;}
span{font-size:.9em;}
.arg,.result,.dp{width:5em;margin-right:.8em;margin-top:1.2em;}
.dp{width:auto;}
.code{width:60em;font-family:Courier New, Courier, monospace;font-size:.8em;margin:0 auto;}
</style>
<script src='../../big.js'></script>
</head>
<body>
<form>
<h1>Testing Big against Number</h1>
<ul class='methods'>
<li>
<input type='radio' id='toExponential' name=1/>
<label for='toExponential'>toExponential</label>
<span>[ decimal places ]</span>
</li>
<li>
<input type='radio' id='toFixed' name=1/>
<label for='toFixed'>toFixed</label>
<span>[ decimal places ]</span>
</li>
<li>
<input type='radio' id='toPrecision' name=1/>
<label for='toPrecision'>toPrecision</label>
<span>[ significant digits ]</span>
</li>
<li>
<input type='radio' id='round' name=1/>
<label for='round'>round</label>
<span>[ decimal places [ , rounding mode ] ]</span>
</li>
<li>
<input type='radio' id='sqrt' name=1/>
<label for='sqrt'>sqrt</label>
</li>
</ul>
<ul id='roundings'>
<li>
<input type='radio' id=0 name=2/>
<label for=0 >Round down</label>
<span>Round towards zero. I.e. truncate, no rounding</span>
</li>
<li>
<input type='radio' id=1 name=2/>
<label for=1 >Round half up</label>
<span>Round towards nearest neighbour. If equidistant, round up</span>
</li>
<li>
<input type='radio' id=2 name=2/>
<label for=2 >Round half even</label>
<span>Round towards nearest neighbour. If equidistant, round towards even neighbour</span>
</li>
<li>
<input type='radio' id=3 name=2/>
<label for=3 >Round up</label>
<span>Round away from zero.</span>
</li>
</ul>
<div class='input'>
<div class='dpDiv'>
<label class='dp' id='dpLabel' for='dp'>Decimal places:</label>
<input type='text' id='dp' name='dp' size=20 />
</div>
<label class='arg' for='input'>Input:</label>
<input class='size' type='text' id='input' name='input' />
</div>
<div class='output'>
<label class='result' for='big'>Big:</label>
<input class='size' type='text' id='big' name='big' readonly />
<div id='number'>
<label class='result' for='num'>Number:</label>
<input class='size' type='text' id='num' name='num' readonly />
</div>
</div>
</form>
<div class= 'code' id='code'></div>
<script>
(function () {
var i, lastFocus,
d = document,
$ = function (id) {return d.getElementById(id)},
$input = $('input'),
$dp = $('dp'),
$dpLabel = $('dpLabel'),
$num = $('num'),
$big = $('big'),
$number = $('number'),
$roundings = $('roundings'),
$code = $('code'),
$inputs = d.getElementsByTagName('input');
function round() {
var i, rb, method, mode, includeNumber, numVal,
bigVal, isSqrt, oldDP, noDP,
code = '',
dp = ( $dp.value = $dp.value.replace(/\s+/g, '') ),
input = ($input.value = $input.value.replace(/\s+/g, ''));
if ( noDP = !/(^0$|^[1-9]\d*$)/.test(dp) ) {
dp = ( $dp.value = '' );
}
if (input) {
for (i = 0; i < 9; i++) {
rb = $inputs[i];
if (rb.checked) {
if (i < 5) method = rb.id;
else mode = rb.id;
}
}
$num.value = $big.value = $code.innerHTML = '';
isSqrt = method == 'sqrt';
if (includeNumber = method != 'round') {
try {
numVal = isSqrt
? Math.sqrt(input)
: Number(input)[method]( noDP ? undefined : +dp );
} catch(e) {
numVal = e;
}
if (isSqrt) {
if (!noDP) {
oldDP = Big.DP;
try {
Big.DP = +dp;
} catch(e) {
Big.DP = oldDP;
$big.value = e;
return;
}
}
code = 'Big.DP = ' + Big.DP;
}
Big.RM = +mode;
if (code) code += '<br>';
code += 'Big.RM = ' + mode;
}
code += "<br><br>Big('" + input + "')." + method + "(";
if (!isSqrt) {
if (method == 'round') {
if (noDP) {
noDP = dp = null;
}
code += dp + ", " + mode;
} else code += dp;
}
code += ')<br><br>';
if (includeNumber) {
if (isSqrt) {
code += "Math.sqrt('" + input + "')";
} else {
code += "Number('" + input + "')." + method + "(";
if (!noDP) code += dp;
code += ")";
}
}
try {
bigVal = new Big(input)[method]( noDP ? undefined : +dp, +mode);
} catch(e) {
bigVal = e;
}
setTimeout(function () {
$big.value = bigVal;
if (includeNumber) $num.value = numVal;
$code.innerHTML = code;
}, 100);
if (window.console && console.log) {
input = new Big(input);
console.log('\nc: ' + input.c +
'\ne: ' + input.e +
'\ns: ' + input.s);
}
}
lastFocus.focus();
}
oldDP = Big.DP = 20;
Big.RM = 1;
$input.value = $dp.value = $num.value = $big.value = '';
setTimeout(function () {$input.focus()}, 0);
for (i = 0; i < 9; i++) {
$inputs[i].checked = false;
$inputs[i].onclick = function () {
if (this.id >= 0) {
round();
} else {
$dpLabel.innerHTML = this.id == 'toPrecision'
? 'Significant digits:'
: 'Decimal places:';
$number.style.display = this.id == 'round'
? 'none'
: 'block';
$roundings.style.display = 'block';
$dp.value = $big.value = $num.value = $code.innerHTML = '';
lastFocus.focus();
}
};
}
$input.onfocus = $dp.onfocus = function () {
lastFocus = this;
};
$inputs[0].checked = $inputs[6].checked = true;
document.onkeypress = function (e) {
if ((e || window.event).keyCode == 13) {
round();
return false;
} else $num.value = $big.value = '';
};
})();
</script>
</body>
</html>
|