当前位置:网站首页>Division optimization of JS decimal calculation on the Internet
Division optimization of JS decimal calculation on the Internet
2022-07-28 17:37:00 【yoki 】
as everyone knows ,js The decimal calculation of is easy to be inaccurate . Baidu found some calculations to solve this problem , But I find that there are still some problems with division , So I rewrite the division method . The code is as follows
var computeTool={
// Subtraction function
"jianfa": function (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));
//last modify by deeka
// Dynamic control precision length
n = (r1 >= r2) ? r1 : r2;
return ((arg1 * m - arg2 * m) / m).toFixed(n);
},
// Addition function , To get accurate addition results
// explain :javascript There will be an error in the addition of , This is obvious when you add two floating point Numbers . This function returns a more accurate addition result .
// Return value :arg1 add arg2 The exact result of
"jiafa": function (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;
},
// Multiplication function , To get accurate multiplication results
// explain :javascript There is an error in the result of multiplication , This is obvious when you multiply two floating point Numbers . This function returns a more precise multiplication result .
// Return value :arg1 multiply arg2 The exact result of
"chengfa": function (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);
},
// Division function , To get accurate division results
// explain :javascript There is an error in the result of division , It's more obvious when you're dividing two floating point Numbers . This function returns a more precise division result .
// Return value :arg1 Divide arg2 The exact result of
"chufa": function (arg1, arg2) {
var t1 = 0, t2 = 0;
try {
t1 = arg1.toString().split(".")[1].length
} catch (e) {
}
try {
t2 = arg2.toString().split(".")[1].length
} catch (e) {
}
with (Math) {
return this.chenfa((arg1*Math.pow(10,t1))/(arg2*Math.pow(10,t2)),(Math.pow(10,t2)/Math.pow(10,t1)));
}
}
}
边栏推荐
- [Presto] common commands of Presto
- 【atlas】atlas 编译报错整理(全)
- Opencv based real-time stitching of simple dual camera images
- How important is baseline safety from non child websites
- Shell脚本之免交互操作
- strsplit()函数
- The practice of beego framework developed by goweb: Section 4 database configuration and connection
- Zero foundation uses unity3d to develop AR applications and download 3D models remotely
- Azure Devops developed by visual studio 2015 team
- Mysql database development specification
猜你喜欢

Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP

部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装

数据库性能分析与优化(爱测未来团队内训材料)

漫谈测试平台—平台建设思路(上)

AMQP协议详解

Several methods of importing excel file data by C #

Arya-专业web自动化测试平台

Linear algebra and matrix theory (IX)

Ant financial mobile testing tool solopi monitoring part of the source code guide.. Continuous update

2021 年全国大学生数据统计与分析竞赛
随机推荐
Blue Bridge Cup embedded competition resources and skills
Use Alibaba cloud's free SSL certificate
JDWP未授权快速利用
Verilog daily question (vl5 signal generator)
Verilog daily question (simple implementation of VL30 RAM)
【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息
About standard IO buffers
MySQL PgSQL realizes the merging of multiple lines of records into one line, grouping and merging, and dividing with specified characters
SNAT、DNAT 防火墙规则的备份和还原
With a total data volume of more than trillions of lines, Yuxi cigarette factory can easily deal with it by correctly selecting the timing database
一篇带你走近Kubernetes概貌与原理
No interactive operation of shell script
Export word according to the template, generate compound format tables and variable column tables
Talk about the measurement of "post release problems"
[kibana] problem sorting kibana 7.x no indices match pattern "APM-*“
Shell编程之Sed
Verilog 每日一题(VL26 简易秒表)
Opencv based real-time stitching of simple dual camera images
Verilog 每日一题 (VL28 加减计数器)
Shell脚本之免交互操作