1)如何計算乘方
站在用戶的角度思考問題,與客戶深入溝通,找到綏化網(wǎng)站設(shè)計與綏化網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗,讓設(shè)計與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個性化、用戶體驗好的作品,建站類型包括:網(wǎng)站制作、網(wǎng)站設(shè)計、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣、主機域名、網(wǎng)站空間、企業(yè)郵箱。業(yè)務覆蓋綏化地區(qū)。
題一:3的4次方(不會打,請原諒 ==!!!)
3的4次方=3*3*3*3
var a = Math.pow(3,4);
console.log(a);
說明:Math.pow()是用來計算乘方的語法
注意:Math的M是大寫;
題二:3的4*5次方
var a =Math.pow(3,4*5);
console.log(a);
2)如何計算根號
題目:根號81
var a = Math.sqrt(81);
console.log(a);
在新公司的第一個項目是區(qū)塊鏈相關(guān)的管理后臺和交易所,其中就涉及了很多的計算問題。而JavaScript因為存在計算的精度問題,所以直接計算就可能會導致各種各樣的bug,為了解決這個問題,就要使用BigNumber.js這個庫。
至于為什么JavaScript會有精度問題呢,可以看 這里 。簡單來說就是因為: JavaScript中所有的數(shù)字(包括整數(shù)和小數(shù))都只有一種類型–Number。它的實現(xiàn)遵循IEEE 754標準,使用64位固定長度來表示,也就是標準的double雙精度浮點數(shù)。它的優(yōu)點是可以歸一化處理整數(shù)和小數(shù),節(jié)省儲存空間。而實際計算的時候會轉(zhuǎn)換成二進制計算再轉(zhuǎn)成十進制。進制轉(zhuǎn)換之后會很長,舍去一部分,計算再轉(zhuǎn)回來,就有了精度誤差。
BigNumber.js是一個用于任意精度計算的js庫。可以在? 官方文檔 ?的console中測試使用。也可以通過npm install bignumber.js --save來安裝。然后?import BigNumber from 'bignumber.js'?來引入使用。他的大概原理是將所有數(shù)字當做字符串,重新實現(xiàn)了計算邏輯。缺點是性能比原生的差很多。
現(xiàn)在 TC39 已經(jīng)有一個 Stage 3 的提案 proposal bigint,大數(shù)問題有望徹底解決。在瀏覽器正式支持前,可以使用 Babel 7.0 來實現(xiàn),它的內(nèi)部是自動轉(zhuǎn)換成 big-integer 來計算,要注意的是這樣能保持精度但運算效率會降低。
具體用法可以參考以下資料:
官方文檔
bignumber.js使用記錄
BigNumber 講解
就不再敖述了,下邊隨便寫點常用的方法:
// 轉(zhuǎn)為 bignumberconstx=newBigNumber('123456789.123456789');// 轉(zhuǎn)為 普通數(shù)字x.toNumber()// 格式化(小數(shù)點)x.toFormat()// '123,456,789.123456789'x.toFormat(3)// '123,456,789.123'// 計算x.plus(0.1)// 加法x.minus(0.1)// 減法x.times(0.1)// 乘法x.div(0.1)// 除法x.mod(3)// 取模/取余// 比較大小x.eq(y)// isEqualTo 的簡寫,是否相等x.gt(y)// isGreaterThan 的簡寫,是否大于x.gte(y)// isGreaterThanOrEqualTo 的簡寫,是否大于等于x.lt(y)// isLessThan 的簡寫,是否小于x.lte(y)// isLessThanOrEqualTo 的簡寫,是否小于等于// 取非,改變數(shù)字的正負號x.negated()
#include stdio.h
int main() {
int a = 1, b = 2, sum = 0;
for (int i = 1; i = 20 / 2; i++) {
sum += a * b - b * (a + b);
a = a + b;
b = a + b;
}
printf("%d\n", sum);
return 0;
}
擴展資料
JavaScript腳本語言具有以下特點:
1、腳本語言。JavaScript是一種解釋型的腳本語言,C、C++等語言先編譯后執(zhí)行,而JavaScript是在程序的運行過程中逐行進行解釋。
2、基于對象。JavaScript是一種基于對象的腳本語言,它不僅可以創(chuàng)建對象,也能使用現(xiàn)有的對象。
3、簡單。JavaScript語言中采用的是弱類型的變量類型,對使用的數(shù)據(jù)類型未做出嚴格的要求,是基于Java基本語句和控制的腳本語言,其設(shè)計簡單緊湊。
JavaScript中有多種方式可以計算這個結(jié)果。
1、最簡單粗暴的方式:
var sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + … + 997 + 998 + 999 + 1000;
2、使用while循環(huán)
var cnt = 1;
var sum = 0;
while( cnt = 1000 ) {
sum+= cnt;
++cnt;
}
3、使用do-while循環(huán)
var cnt = 1;
var sum = 0;
do {
sum+= cnt;
++cnt;
} while( cnt = 1000 );
4、使用for循環(huán)
var sum = 0;
for( var i = 1; i=1000; ++i ) {
sum += i;
}
從上面四種可以看出,使用for循環(huán)是最清晰的。我在秒秒學上js程序的循環(huán)章節(jié)也看過類似的題目,推薦你去看看。
首先,由于JS的存在數(shù)值的精度誤差問題:
0.1+0.2 ? //0.30000000000000004
0.3-0.1 ? //0.19999999999999998
所以在編寫計算器是應首先解決計算精度問題,以下四個代碼段分別是js中精確的加減乘除運算函數(shù)
//浮點數(shù)加法運算
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
}
//浮點數(shù)減法運算
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));
//動態(tài)控制精度長度
n=(r1=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}
//浮點數(shù)乘法運算
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)
}
//浮點數(shù)除法運算
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
tdinput type="button" value="←" ? ? ? class="btn_color1 btn_operation"/td
tdinput type="button" value="全清" ? ? class="btn_color1 btn_operation"/td
tdinput type="button" value="清屏" ? ? class="btn_color1"/td
tdinput type="button" value="﹢/﹣" ? ?class="btn_color2 btn_operation"/td
tdinput type="button" value="1/×" ? ? class="btn_color2 btn_operation"/td
/tr
tr
tdinput type="button" ?value="7" ? ? class="btn_color3 btn_number"/td
tdinput type="button" ?value="8" ? ? class="btn_color3 btn_number"/td
tdinput type="button" ?value="9" ? ? class="btn_color3 btn_number"/td
tdinput type="button" ?value="÷" ? ?class="btn_color4 btn_operation"/td
tdinput type="button" ?value="%" ? ?class="btn_color2 btn_operation"/td
/tr
tr
tdinput type="button" ? value="4" ? class="btn_color3 btn_number"/td
tdinput type="button" ? value="5" ? class="btn_color3 btn_number"/td
tdinput type="button" ? value="6" ? class="btn_color3 btn_number"/td
tdinput type="button" ? value="×" ?class="btn_color4 btn_operation"/td
tdinput type="button" ? value="√" ?class="btn_color2 btn_operation"/td
/tr
tr
tdinput type="button" ?value="1" ? class="btn_color3 btn_number"/td
tdinput type="button" ?value="2" ? class="btn_color3 btn_number"/td
tdinput type="button" ?value="3" ? class="btn_color3 btn_number"/td
tdinput 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
tdinput type="button" ?value="." ? class="btn_color3 btn_number" /td
tdinput 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"); //獲取數(shù)字輸入按鈕
var btns_operation = document.getElementsByClassName("btn_operation"); //獲取操作按鈕
var simpleEqu = document.getElementById("simpleEqu"); //獲取"="按鈕
var temp = "";
var num1= 0,num2=0;
//獲取第一個數(shù)
for(var i=0;ibtns_number.length;i++){
btns_number[i].onclick=function (){
temp += this.value;
resultIpt.value = temp;
};
}
//對獲取到的數(shù)進行操作
for(var j=0;jbtns_operation.length;j++) {
btns_operation[j].onclick = function () {
num1=parseFloat(resultIpt.value);
oper = this.value;
if(oper=="1/×"){
num1 = Math.pow(num1,-1); //取倒數(shù)
resultIpt.value = num1.toString();
}else if(oper=="﹢/﹣"){ ? ?//取相反數(shù)
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=="全清"){ ?//清除數(shù)字
resultIpt.value = "";
}
else{ ? ? ? ? ?//oper=="+" "-" "×" "÷" "%"時,繼續(xù)輸入第二數(shù)字
temp = "";
resultIpt.value = temp;
}
}
}
//輸出結(jié)果
simpleEqu.onclick=function(){
num2=parseFloat(temp); ?//取得第二個數(shù)字
calculate(num1, num2, oper);
resultIpt.value = result.toString();
}
};
//定義一個計算函數(shù)
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; ?//求余數(shù)
break;
}
}
//精確計算
//浮點數(shù)加法運算
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
}
//浮點數(shù)減法運算
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));
//動態(tài)控制精度長度
n=(r1=r2)?r1:r2;
return ((arg1*m-arg2*m)/m).toFixed(n);
}
//浮點數(shù)乘法運算
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)
}
//浮點數(shù)除法運算
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);
}
}
分享文章:javascript計算,javascript計算圓的面積
網(wǎng)站網(wǎng)址:http://chinadenli.net/article10/dsicjdo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、服務器托管、網(wǎng)站營銷、微信公眾號、域名注冊、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)