導航:首頁 > 編程語言 > js數字計算精度

js數字計算精度

發布時間:2023-06-28 22:17:56

Ⅰ 關於js中小數運算丟失精度的處理辦法

浮點數值的最高精度是17位小數,但在進行運算的毀基時候其精確度卻遠遠不如整數;整數在進行運算的時候都會轉成10進制; 而java和JavaScript中計算小數運算時,都會先將十進制的小數換算到對應的二進制,一部分散高小數並不能完整的換算為二進制,這里就出現了第一次的誤差。待小數都換算為二進制後,再進行二進制間的運算,得到二進制結果。然後再將二進制結果換算為十進制,這里通常會出現第二次的誤差。

所以(0.1+0.2)!=03

解決這種問題,可以將小數變成整數進行運算,然後再將結果變為小數。

//乘法

function multiNum (a,b){

  var c = 0,

d = a.toString(),

e = b.toString();

try {

      c += d.split(".")[1].length;

} catch (f) { }

  try {

      c += e.split(".")[1].length;

} catch (f) { }

  return Number(d.replace(".","")) * Number(e.replace(".","")) / Math.pow(10,c);

}

//除法

function divide (a,b){

  var c,d,e = 0,

f = 0;

try {

      e = a.toString().split(".")[1].length;

} catch (g) { }

  try {

     沖余尺 f = b.toString().split(".")[1].length;

} catch (g) { }

  return c = Number(a.toString().replace(".","")),d = Number(b.toString().replace(".","")),this.mul(c / d,Math.pow(10,f - e));

}

//加法

function addNum (a,b){

  var c,d,e;

try {

      c = a.toString().split(".")[1].length;

} catch (f) {

      c = 0;

}

  try {

      d = b.toString().split(".")[1].length;

} catch (f) {

      d = 0;

}

  return e = Math.pow(10,Math.max(c,d)),(multiNum(a,e) + multiNum(b,e)) / e;

}

//減法

function subNum (a,b) {

  var c,d,e;

try {

      c = a.toString().split(".")[1].length;

} catch (f) {

      c = 0;

}

  try {

      d = b.toString().split(".")[1].length;

} catch (f) {

      d = 0;

}

  return e = Math.pow(10,Math.max(c,d)),(multiNum(a,e) - multiNum(b,e)) / e;

}

Ⅱ JavaScript為什麼浮點數會丟失精度

JS浮點計算問題

問題

用js進行浮點數計算,結果可能會「超出預期」,大部分計算結果還是對的,但是我們可不想在計算這么嚴謹的事情上還有意外的驚喜。比如:

以上是網上找的

我以前遇到過問題2中瀏覽器計算的結果 是兩種,所以和瀏覽器也有問題

Ⅲ js 雙精度浮點數

一、怎樣將一個數據轉成浮點數   https://www.hu.com/question/21711083

二、js 的 Number

在 JavaScript 中整數和浮點數都屬於 Number 數據類型,所有數字都是以 64 位浮點數形式儲存,即便整數也是如此。

三、造成哪些問題?

1、小數計算精度丟失,比如 0.1+0.2 不等於 0.3

2、整數最大范圍

整數是按最大54位來算最大(253 - 1,Number.MAX_SAFE_INTEGER,9007199254740991) 和最小(-(253 - 1),Number.MIN_SAFE_INTEGER,-9007199254740991) 安全整數范圍的。所以只要超過這個范圍,就會存在被捨去的精度問題。

四、解決辦法

開源的庫、bigInt、

0.1+0.2-0.3     // 5.551115123125783e-17

5.551115123125783e-17.toFixed(20)      //脊顫   '0.00000000000000005551'

5.551115123125783e-17<Number.EPSILON*Math.pow(2,2)    // true

重新整理

https://zhuanlan.hu.com/p/73699947

回顧一個基礎問題,js 中的精度丟失問題。

一、在 js 中只有雙精度浮點數來存儲的Number,數據存儲會有三個步驟:1、十進制轉二進制 2、二進制轉科學技術法 3、按 IEEE754 標准存儲。 

二、雙精度浮點一共有 64位,64位比特又可分為三個部分:森輪

符號位S:第 1 位是正負數符號位(sign),0代表正數,1代表負數

指數位E:中間的 11 位存儲指數(exponent),用來表示次方數

尾數位M:最後的 52 位是尾數(mantissa),超出的部分自動進一櫻春敗舍零

三、基於以上知識,在數據小數位在進行轉換二進制時,會出現無線循環的情況,而數據轉成 IEEE754標准時又僅支持 52 位,所以要發生一個數據截斷,也就是精度丟失。

四、常見的丟失場景,

 0.1 + 0.2 === 0.30000000000000004

parseInt(0.58*100,10)=57

(1.335).toFixed(2)

四、解決辦法

math.js

bignumber.js

等庫以及 es6 針對整數精度丟失的新數據類型BigInt 

Ⅳ js對於計算浮點數據不準確怎麼處理

一種就是加ROUND呀。如何不行了? 另一種是:工具-選項-重新計算,勾選「以顯示精度為准」。 如果你能描述詳細些,就會找到問題出在哪裡。

Ⅳ js小數加減為什麼會失精

JavaScript的數字使用的是浮點數,浮點數可以表示的數的個數有限(只有很少一部分數可以表示),其他的數在浮點書裡面不存在
其他語言同樣有這個問題,只是在語言層面做了處理,開發者可能不需要考慮精度問題,但JavaScript語言並沒有對結果進行底層處理,所以就會有問題
提示:js的加減乘除等計算都有問題

Ⅵ js浮點數的加減乘除解決方案

一直知道js的浮點數計算是不精確的, 0.1 + 0.2 !== 0.3,但是也就知道而已,解決方法卻不怎麼注意盯桐,所以剛做一個項目,盡管了解浮點數精度不精確的問題,但是還是掉坑裡了。在此再次默默告訴自己要警惕,端正心態,不可掉以輕心!!!所以下面就分享一些加減乘除的方法。

原理: 把數字轉換成字元串,然後從小數點部分切割成兩部分,分別算出兩個因數的小數點右邊的長度,然後用兩個因數的小數點右邊長度最大的數再乘以10,相當於兩個都放大了n倍,然後相加,然後縮小n倍。
注意,這里的放大用了乘法times函數(下面介紹),因為浮點數直接乘以100有可能出現精度不夠的情況,如下圖

原理和加法一樣,放大n倍後相減再縮小n倍

乘法原理稍微變點,放大倍數n是 『兩個小數點後面長度之和』 而不是 『兩個小數點後面長度這兩者之間的最大值』

除法原理和乘法一樣

這也是一個坑,比如你要保留兩位小數,四捨五入的話就要看小數點第三位後面的數字來決定,如2.445四捨五入後就是2.45; 2.444四捨五入就是2.44;做這個需物磨求的時候,我第一反應是Math.toFixed(2),結果是bug百出啊,這里就不舉例了,有興趣可以自己嘗試。然後我是怎麼解決的呢?網路了一下,也是得到一些半成品不嚴謹的函數,原理也很簡單,先放大倍數,然後利用Math.round()取整

以上加減乘除方法基本滿足一般業務需求了,尤其是電商。但是如果數字計算時超出了 2的1024次方減1 ,也就是 9007199254740992 這個數字的話就不適合了,因為從 2^1024 開始就變成了 Infinity。凱螞坦

Ⅶ 如何使用javascript編寫一個計算器

首先,由於JS的存在數值的精度誤差問題:

0.1+0.2 //0.30000000000000004
0.3-0.1 //0.19999999999999998

所以在編寫計算器是應首先解決計算精度問題,以下四個代碼段分別是js中精確的加減乘除運算函數

//浮點數加法運算
function floatAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m
}

//浮點數減法運算
function floatSub(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
//動態控制精度長度
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}

//浮點數乘法運算
function floatMul(arg1,arg2){
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}

//浮點數除法運算
function floatDiv(arg1,arg2) {
var t1 = 0, t2 = 0, r1, r2;
try {t1 = arg1.toString().split(".")[1].length} catch (e) {}
try {t2 = arg2.toString().split(".")[1].length} catch (e) {}
with (Math) {
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) * pow(10, t2 - t1);
}
}

以下是詳細的計算器代碼:
HTML5

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>簡單計算器</title>
<link href="main.css" rel="stylesheet">
</head>
<body>
<div id="calculator">

<div id="calculator_container">
<h3>計算器</h3>
<table id="calculator_table">
<tbody>
<tr>
<td colspan="5">
<input type="text" id="resultIpt" readonly="readonly" value="" size="17" maxlength="17" style="width:294px;color: black">
</td>
</tr>
<tr>
<td><input type="button" value="←" class="btn_color1 btn_operation"></td>
<td><input type="button" value="全清" class="btn_color1 btn_operation"></td>
<td><input type="button" value="清屏" class="btn_color1"></td>
<td><input type="button" value="﹢/﹣" class="btn_color2 btn_operation"></td>
<td><input type="button" value="1/×" class="btn_color2 btn_operation"></td>
</tr>
<tr>
<td><input type="button" value="7" class="btn_color3 btn_number"></td>
<td><input type="button" value="8" class="btn_color3 btn_number"></td>
<td><input type="button" value="9" class="btn_color3 btn_number"></td>
<td><input type="button" value="÷" class="btn_color4 btn_operation"></td>
<td><input type="button" value="%" class="btn_color2 btn_operation"></td>
</tr>
<tr>
<td><input type="button" value="4" class="btn_color3 btn_number"></td>
<td><input type="button" value="5" class="btn_color3 btn_number"></td>
<td><input type="button" value="6" class="btn_color3 btn_number"></td>
<td><input type="button" value="×" class="btn_color4 btn_operation"></td>
<td><input type="button" value="√" class="btn_color2 btn_operation"></td>
</tr>
<tr>
<td><input type="button" value="1" class="btn_color3 btn_number"></td>
<td><input type="button" value="2" class="btn_color3 btn_number"></td>
<td><input type="button" value="3" class="btn_color3 btn_number"></td>
<td><input type="button" value="-" class="btn_color4 btn_operation"></td>
<td rowspan="2">
<input type="button" value="=" class="btn_color2" style="height: 82px" id="simpleEqu">
</td>
</tr>
<tr>
<td colspan="2">
<input type="button" value="0" class="btn_color3 btn_number" style="width:112px">
</td>
<td><input type="button" value="." class="btn_color3 btn_number" ></td>
<td><input type="button" value="+" class="btn_color4 btn_operation"></td>
</tr>
</tbody>
</table>

</div>
</div>
<script type="text/javascript" src="calculator.js"></script>
</body>
</html>


CSS3

* {
margin: 0;
padding: 0;
}
#calculator{
position: relative;
margin: 50px auto;
width: 350px;
height: 400px;
border: 1px solid gray;
-webkit-border-radius: 10px;
-moz-border-radius: 10px;
border-radius: 10px;
-webkit-box-shadow: 2px 2px 4px gray;
-moz-box-shadow: 2px 2px 4px gray;
box-shadow: 2px 2px 4px gray;
behavior:url("ie-css3.htc"); /*IE8-*/
}
#calculator_table{
position: relative;
margin: 10px auto;
border-collapse:separate;
border-spacing:10px 20px;
}
h3{
position: relative;
width: 60px;
height: 30px;
margin: 0 auto;
}
#calculator_table td{
width: 50px;
height: 30px;
border: 1px solid gray;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
behavior:url("ie-css3.htc"); /*IE8-*/
}
#calculator_table td input{
font-size: 16px;
border: none;
width: 50px;
height: 30px;
color: white;
}
input.btn_color1{
background-color: orange;
}
input.btn_color2{
background-color: #133645;
}
input.btn_color3{
background-color: #59807d;
}
input.btn_color4{
background-color: seagreen;
}
input:active{
-webkit-box-shadow: 3px 3px 3px gray;
-moz-box-shadow: 3px 3px 3px gray;
box-shadow: 3px 3px 3px gray;
behavior:url("ie-css3.htc"); /*IE8-*/
}

JS

window.onload=function() {
var resultIpt = document.getElementById("resultIpt"); //獲取輸出文本框
var btns_number = document.getElementsByClassName("btn_number"); //獲取數字輸入按鈕
var btns_operation = document.getElementsByClassName("btn_operation"); //獲取操作按鈕
var simpleEqu = document.getElementById("simpleEqu"); //獲取"="按鈕
var temp = "";
var num1= 0,num2=0;
//獲取第一個數
for(var i=0;i<btns_number.length;i++){
btns_number[i].onclick=function (){
temp += this.value;
resultIpt.value = temp;
};
}
//對獲取到的數進行操作
for(var j=0;j<btns_operation.length;j++) {
btns_operation[j].onclick = function () {
num1=parseFloat(resultIpt.value);
oper = this.value;
if(oper=="1/×"){
num1 = Math.pow(num1,-1); //取倒數
resultIpt.value = num1.toString();
}else if(oper=="﹢/﹣"){ //取相反數
num1 = -num1;
resultIpt.value = num1.toString();
}else if(oper=="√"){ //取平方根
num1 =Math.sqrt(num1);
resultIpt.value = num1.toString();
}else if(oper=="←"){ //刪除個位
resultIpt.value = resultIpt.value.substring(0, resultIpt.value.length - 1);

}else if(oper=="全清"){ //清除數字
resultIpt.value = "";
}
else{ //oper=="+" "-" "×" "÷" "%"時,繼續輸入第二數字
temp = "";
resultIpt.value = temp;
}
}
}
//輸出結果
simpleEqu.onclick=function(){
num2=parseFloat(temp); //取得第二個數字
calculate(num1, num2, oper);
resultIpt.value = result.toString();
}
};

//定義一個計算函數
function calculate(num1, num2, oper) {
switch (oper) {
case "+":
result=floatAdd(num1, num2); //求和
break;
case "-":
result=floatSub(num1, num2); //求差
break;
case "×":
result=floatMul(num1, num2); //求積
break;
case "÷":
result=floatDiv(num1, num2); //求商
break;
case "%":
result=num1%num2; //求余數
break;
}
}

//精確計算
//浮點數加法運算
function floatAdd(arg1,arg2){
var r1,r2,m;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
return (arg1*m+arg2*m)/m
}
//浮點數減法運算
function floatSub(arg1,arg2){
var r1,r2,m,n;
try{r1=arg1.toString().split(".")[1].length}catch(e){r1=0}
try{r2=arg2.toString().split(".")[1].length}catch(e){r2=0}
m=Math.pow(10,Math.max(r1,r2));
//動態控制精度長度
n=(r1>=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}
//浮點數乘法運算
function floatMul(arg1,arg2){
var m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
}
//浮點數除法運算
function floatDiv(arg1,arg2) {
var t1 = 0, t2 = 0, r1, r2;
try {t1 = arg1.toString().split(".")[1].length} catch (e) {}
try {t2 = arg2.toString().split(".")[1].length} catch (e) {}
with (Math) {
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) * pow(10, t2 - t1);
}
}

閱讀全文

與js數字計算精度相關的資料

熱點內容
怎麼開通移動4g網路 瀏覽:130
岳陽微信公司 瀏覽:96
win10如何從備份中恢復出廠設置密碼 瀏覽:659
什麼軟體修改wifi密碼錯誤 瀏覽:582
遇見不安全網站怎麼辦 瀏覽:251
哪個app有慶余年電視劇 瀏覽:420
iphone5s視頻時很黑 瀏覽:601
js獲取埠號 瀏覽:347
手機短息發的鏈接病毒蘋果手機 瀏覽:724
win10專業忘記家庭組密碼 瀏覽:176
南寧applestore幾樓 瀏覽:296
java字元串怎麼初始化 瀏覽:349
醫美哪個app好 瀏覽:768
代碼編程和機器人編程哪個好 瀏覽:875
90版本男法 瀏覽:796
win10桌面字體難看 瀏覽:848
三菱fx5u支持哪些編程 瀏覽:7
優酷在文件夾在哪裡 瀏覽:91
趣列印的數據文件是什麼 瀏覽:871
linuxjava程序 瀏覽:483

友情鏈接