{"id":56312,"date":"2022-10-13T03:01:42","date_gmt":"2022-10-12T17:01:42","guid":{"rendered":"http:\/\/www.rjmprogramming.com.au\/ITblog\/?p=56312"},"modified":"2022-10-18T21:07:16","modified_gmt":"2022-10-18T11:07:16","slug":"javascript-integer-precision-primer-tutorial","status":"publish","type":"post","link":"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-integer-precision-primer-tutorial\/","title":{"rendered":"Javascript Integer Precision Primer Tutorial"},"content":{"rendered":"<div style=\"width: 230px\" class=\"wp-caption alignnone\"><a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/number_checks.html\"><img decoding=\"async\" style=\"border: 15px solid pink;\" alt=\"Javascript Integer Precision Primer Tutorial\" src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/number_checks.jpg\" title=\"Javascript Integer Precision Primer Tutorial\"  style=\"float:left;\" \/><\/a><p class=\"wp-caption-text\">Javascript Integer Precision Primer Tutorial<\/p><\/div>\n<p>Javascript&#8217;s abilities to handle large counting numbers, called integers, is not there as any default with its &#8220;untyped&#8221; instinctual tendencies.  There are, at least, two approaches to take, if you think you are going to run into a scenario where you really need to use large counting numbers in production &#8230;<\/p>\n<ul>\n<li>use its <a target=_blank title='BigInt' href='https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/BigInt'><i>BigInt<\/i><\/a> type potentials &#8230;<\/li>\n<li>use <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'>Number.isSafeInteger<\/a>() function testing, <font color=blue>as per<\/font>, 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>, code like &#8230;\n<p><code><br \/>\nfunction warn(x) {<br \/>\n  if (<font color=blue>Number.isSafeInteger(x)<\/font>) {<br \/>\n    document.getElementById('result').innerHTML=Math.pow(document.getElementById('base').value, document.getElementById('tpo').value);<br \/>\n    \/\/alert('Precision safe for Math.pow(' + document.getElementById('base').value + ',' + document.getElementById('tpo').value + ')=' + Math.pow(document.getElementById('base').value, document.getElementById('tpo').value));<br \/>\n  } else if (('' + x).indexOf('-') != -1) {<br \/>\n    document.getElementById('result').innerHTML='Precision may be lost!  Lowest integer that can be handled is ' + Number.MIN_SAFE_INTEGER + ' versus ' + BigInt(x);<br \/>\n    \/\/alert('Precision may be lost!');<br \/>\n  } else {<br \/>\n    document.getElementById('result').innerHTML='Precision may be lost!  Highest integer that can be handled is ' + Number.MAX_SAFE_INTEGER + ' versus ' + BigInt(x);<br \/>\n    \/\/alert('Precision may be lost!');<br \/>\n  }<br \/>\n  return false;<br \/>\n}<br \/>\n<\/code>\n<\/li>\n<\/ul>\n<p> &#8230; as in today&#8217;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/number_checks.html_GETME\" title=\"number_checks.html\">number_checks.html<\/a>&#8216;s <a target=_blank href=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/number_checks.html\" title=\"Click picture\">web application<\/a> usage, you can also try below &#8230;<\/p>\n<p><iframe src=\"http:\/\/www.rjmprogramming.com.au\/HTMLCSS\/number_checks.html\" style=\"width:100%;height:320px;\"><\/iframe><\/p>\n<p>If this was interesting you may be interested in <a title='Click here to see topics in which you might be interested' href='#d56312' onclick='var dv=document.getElementById(\"d56312\"); dv.innerHTML = \"&lt;iframe width=670 height=600 src=\" + \"https:\/\/www.rjmprogramming.com.au\/ITblog\/tag\/integer\" + \"&gt;&lt;\/iframe&gt;\"; dv.style.display = \"block\";'>this<\/a> too.<\/p>\n<div id='d56312' style='display: none; border-left: 2px solid green; border-top: 2px solid green;'><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Javascript&#8217;s abilities to handle large counting numbers, called integers, is not there as any default with its &#8220;untyped&#8221; instinctual tendencies. There are, at least, two approaches to take, if you think you are going to run into a scenario where &hellip; <a href=\"https:\/\/www.rjmprogramming.com.au\/ITblog\/javascript-integer-precision-primer-tutorial\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,37],"tags":[4016,2991,652,2394,997,1319,1671,4017,1721],"class_list":["post-56312","post","type-post","status-publish","format-standard","hentry","category-elearning","category-tutorials","tag-bigint","tag-integer","tag-javascript","tag-number","tag-programming","tag-tutorial","tag-type","tag-untyped","tag-variable"],"_links":{"self":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56312"}],"collection":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/comments?post=56312"}],"version-history":[{"count":7,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56312\/revisions"}],"predecessor-version":[{"id":57007,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/posts\/56312\/revisions\/57007"}],"wp:attachment":[{"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/media?parent=56312"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/categories?post=56312"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rjmprogramming.com.au\/ITblog\/wp-json\/wp\/v2\/tags?post=56312"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}