1 <html></html>
2 <script>
3 // 0.1+0.2
4 let a = null,
5 b = null;
6 a=0.1
7 b=0.2
8 function sum(a,b){
9 return a+b
10 }
11 // console.log(sum(a,b)) // 0.30000000000000004
12 // 思路,通过放大倍数再缩小
13 let times1 = 10
14 // console.log(sum(a*times1,b*times1)/times1) // 0.3
15 // 引出问题,该放大多大倍数
16 // 从上述可以看出a扩大了10的小数部分长度的幂 a整数部分长度为1,扩大倍数为10,参数b同理扩大
17 // 假如是0.1+0.02
18 // console.log(sum(0.1,0.02)) // 0.12000000000000001
19 let times2 = 100
20 // console.log(sum(0.1*times2,0.02*times2)/times2) // 0.12
21 // 通过再次分析,可知,扩大倍数=10的相加参数小数部分长度最大的次幂
22 // 引出两个方法
23 // 1.Math.pow(x,y) x的y次幂
24 // 2. Math.max(x...) 取最大的值
25 // 封装函数 加法
26 function accuracySum(a,b) {
27 if(a === undefined || b === undefined) {
28 return console.log('a或b不存在')
29 }
30 // 定义a,b 小数部分的长度
31 let lenA = 0,
32 lenB = 0;
33 // 将a,b类型转化成string
34 let strA = typeof a === 'string' ? a :a.toString(),
35 strB = typeof b === 'string' ? b : b.toString();
36 if(strA.indexOf('.')!== -1) {
37 lenA = strA.split('.')[1].length > 0 ? strA.split('.')[1].length : 0
38 }
39 if(strB.indexOf('.')!== -1) {
40 lenB = strB.split('.')[1].length > 0 ? strB.split('.')[1].length : 0
41 }
42 // 倍数
43 let times = Math.pow(10,Math.max(lenA,lenB))
44 // console.log(times,'【times】',a*times,b*times,lenA,lenB)
45 return (a*times+b*times)/times
46 }
47 console.log('【accuracySum】',accuracySum(-1.1,0.2))
48 // 封装函数 减法
49 function accuracySubtraction(a,b) {
50 if(a === undefined || b === undefined) {
51 return console.log('a或b不存在')
52 }
53 // 定义a,b 小数部分的长度
54 let lenA = 0,
55 lenB = 0;
56 // 将a,b类型转化成string
57 let strA = typeof a === 'string' ? a :a.toString(),
58 strB = typeof b === 'string' ? b : b.toString();
59 if(strA.indexOf('.')!== -1) {
60 lenA = strA.split('.')[1].length > 0 ? strA.split('.')[1].length : 0
61 }
62 if(strB.indexOf('.')!== -1) {
63 lenB = strB.split('.')[1].length > 0 ? strB.split('.')[1].length : 0
64 }
65 // 倍数
66 let times = Math.pow(10,Math.max(lenA,lenB))
67 // console.log(times,'【times】',a*times,b*times,lenA,lenB)
68 return (a*times-b*times)/times
69 }
70 console.log('【accuracySubtraction】',accuracySubtraction(-1.1,-0.2)) // -0.9
71 // 封装乘法
72 function accuracyMulti(a,b) {
73 if(!a || !b) {
74 return
75 }
76 if(a === 0 || b === 0) {
77 return 0
78 }
79 // 定义a,b 小数部分的长度
80 let lenA = 0,
81 lenB = 0;
82 // 将a,b类型转化成string
83 let strA = typeof a === 'string' ? a :a.toString(),
84 strB = typeof b === 'string' ? b : b.toString();
85 if(strA.indexOf('.')!== -1) {
86 lenA = strA.split('.')[1].length > 0 ? strA.split('.')[1].length : 0
87 }
88 if(strB.indexOf('.')!== -1) {
89 lenB = strB.split('.')[1].length > 0 ? strB.split('.')[1].length : 0
90 }
91 // 倍数
92 let times = Math.pow(10,Math.max(lenA,lenB))
93 // console.log(times,'【times】',a*times,b*times,lenA,lenB)
94 return (a*times)*(b*times)/(times*times)
95 }
96 console.log(accuracyMulti(0.1,0.02),'【accuracyMulti】') // 0.002
97
98 // 封装除法
99 function accuracyDivision(a,b) {
100 if(!a || !b) {
101 return
102 }
103 if(a === 0 || b === 0) {
104 return 0
105 }
106 // 定义a,b 小数部分的长度
107 let lenA = 0,
108 lenB = 0;
109 // 将a,b类型转化成string
110 let strA = typeof a === 'string' ? a :a.toString(),
111 strB = typeof b === 'string' ? b : b.toString();
112 if(strA.indexOf('.')!== -1) {
113 lenA = strA.split('.')[1].length > 0 ? strA.split('.')[1].length : 0
114 }
115 if(strB.indexOf('.')!== -1) {
116 lenB = strB.split('.')[1].length > 0 ? strB.split('.')[1].length : 0
117 }
118 // 倍数
119 let times = Math.pow(10,Math.max(lenA,lenB))
120 // console.log(times,'【times】',a*times,b*times,lenA,lenB)
121 return (a*times)/(b*times)
122 }
123 console.log(accuracyDivision(0.1,0.002),'【accuracyMulti】') // 50
124 </script>