<html>
<head>
<title>Number Check - RJM Programming - July, 2022 ... thanks to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger</title>
<style> * { align: center; } </style>
<script type='text/javascript'>
var bi=false;
// Number.MIN_SAFE_INTEGER to Number.MAX_SAFE_INTEGER
// Number.isSafeInteger()
function biwarn(xx, yy) {
var x=BigInt('' + xx);
var y=BigInt('' + yy);
//if (Number.isSafeInteger(x)) {
document.getElementById('result').innerHTML='<textarea style="width:90%;">' + (x ** y) + '</textarea>';
//alert('Precision safe for Math.pow(' + document.getElementById('base').value + ',' + document.getElementById('tpo').value + ')=' + Math.pow(document.getElementById('base').value, document.getElementById('tpo').value));
//} else if (('' + x).indexOf('-') != -1) {
//document.getElementById('result').innerHTML='Precision may be lost! Lowest integer that can be handled is ' + Number.MIN_SAFE_INTEGER + ' versus ' + BigInt(x);
//alert('Precision may be lost!');
//} else {
//document.getElementById('result').innerHTML='Precision may be lost! Highest integer that can be handled is ' + Number.MAX_SAFE_INTEGER + ' versus ' + BigInt(x);
//alert('Precision may be lost!');
//}
return false;
}
function warn(x) {
if (Number.isSafeInteger(x)) {
document.getElementById('result').innerHTML=Math.pow(document.getElementById('base').value, document.getElementById('tpo').value);
//alert('Precision safe for Math.pow(' + document.getElementById('base').value + ',' + document.getElementById('tpo').value + ')=' + Math.pow(document.getElementById('base').value, document.getElementById('tpo').value));
} else if (('' + x).indexOf('-') != -1) {
document.getElementById('result').innerHTML='Precision may be lost! Lowest integer that can be handled is ' + Number.MIN_SAFE_INTEGER + ' versus ' + BigInt(x);
//alert('Precision may be lost!');
} else {
document.getElementById('result').innerHTML='Precision may be lost! Highest integer that can be handled is ' + Number.MAX_SAFE_INTEGER + ' versus ' + BigInt(x);
//alert('Precision may be lost!');
}
return false;
}
//console.log(warn(Math.pow(2, 53)));
// expected output: "Precision may be lost!"
//console.log(warn(Math.pow(2, 53) - 1));
// expected output: "Precision safe."
</script>
</head>
<body>
<h2>Number Check</h2>
<h3>RJM Programming - July, 2022</h3>
<h4>Thanks to <a target=_blank href='//developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger' title='https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger'>https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isSafeInteger</a></h4>
<form onsubmit="if (bi) { return biwarn('' + document.getElementById('base').value, '' + document.getElementById('tpo').value); } return warn(Math.pow(document.getElementById('base').value, document.getElementById('tpo').value));" method=GET action='./number_checks.html'>
<table border=20>
<tr><th>Base</th><th>Power</th></tr>
<tr><td><input type=number value=2 step=1 id=base name=base></input></td><td><input type=number value=6 step=1 id=tpo name=tpo></input></td></tr>
<tr><td><input onclick='bi=false;' type=submit value="Check as Normal Integer" style='background-color:yellow;'></input><br><br><input onclick='bi=true;' type=submit value="Check as BigInt" name=bigint style='background-color:orange;'></input></td><td id=result></td></tr>
</table>
</form>
</body>
</html>