js 大數計算
原理
JavaScript 安全整數 是 -253-1 ~ 253-1 ,即: -9007199254740991 ~ 9007199254740991; 換句話說,整數超過這個范圍就會丟失精度,那超過這個范圍的基本運算也別指望有多精確了;
換一種思路,用字符串表示數字,比如 "9007199254740999",不存在四舍五入精度丟失的問題,這樣不管數值多大都沒影響;
用數字表示字符串之后,那么數字的運算就轉換成字符串的運算;
big number add
從最后一個數字開始向前依次相加,逢10進1,下一位數字相加的時候加上次計算的進位;
const add = (num1, num2) => {
// 獲取最大長度
const len = Math.max(num1.length, num2.length);
// 對齊
num1 = num1.padStart(len, 0);
num2 = num2.padStart(len, 0);
let flag = 0;
let result = ``;
let temp = 0;
for(let i=len-1; i>=0; i--){
temp = flag + parseInt(num1[i]) + parseInt(num2[i])
result = (temp%10) + result
flag = parseInt(temp/10)
}
// 判斷是否進位
return result = (flag === 1 ? '1' : '') + result;
}
const n1 = "9007199254740990"
const n2 = "1229007199254740993443"
add(n1, n2);
// "1229016206453995734433"
MathML
Mathematical Markup Language (MathML) Version 3.0 2nd Edition
W3C Recommendation 10 April 2014
HTML var
tag
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/var
<table width="100%">
<tbody>
<tr>
<td id="eqnoc2"><var>E</var> = <var>m</var><var>c</var><sup>2</sup></td>
</tr>
</tbody>
</table>
E = mc2 |
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/kbd
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/code
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/samp
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sup
<p><var>a<sup>2</sup></var> + <var>b<sup>2</sup></var> = <var>c<sup>2</sup></var></p>
a2 + b2 = c2
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/sub
H<sub>2</sub>O
H2O
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/wbr
MathJax
xml for math
https://mathjax.github.io/MathJax-demos-web/tex-chtml.html
©xgqfrms 2012-2020
www.cnblogs.com 發布文章使用:只允許注冊用戶才可以訪問!