Number.EPSILON

ES6 在Number对象上面,新增一个极小的常量Number.EPSILON。根据规格,它表示 1 与大于 1 的最小浮点数之间的差。

对于 64 位浮点数来说,大于 1 的最小浮点数相当于二进制的1.00..001,小数点后面有连续 51 个零。这个值减去 1 之后,就等于 2 的 -52 次方。

Number.EPSILON === Math.pow(2, -52)
// true
Number.EPSILON
// 2.220446049250313e-16
Number.EPSILON.toFixed(20)
// "0.00000000000000022204"

 

Number.EPSILON实际上是 JavaScript 能够表示的最小精度。误差如果小于这个值,就可以认为已经没有意义了,即不存在误差了。

引入一个这么小的量的目的,在于为浮点数计算,设置一个误差范围。我们知道浮点数计算是不精确的。

0.1 + 0.2
// 0.30000000000000004

0.1 + 0.2 - 0.3
// 5.551115123125783e-17

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

 

上面代码解释了,为什么比较0.1 + 0.2与0.3得到的结果是false。

0.1 + 0.2 === 0.3 // false

 

Number.EPSILON可以用来设置“能够接受的误差范围”。比如,误差范围设为 2 的-50 次方(即Number.EPSILON * Math.pow(2, 2)),即如果两个浮点数的差小于这个值,我们就认为这两个浮点数相等。

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

 

因此,Number.EPSILON的实质是一个可以接受的最小误差范围。

function withinErrorMargin (left, right) {
  return Math.abs(left - right) < Number.EPSILON * Math.pow(2, 2);
}

0.1 + 0.2 === 0.3 // false
withinErrorMargin(0.1 + 0.2, 0.3) // true

1.1 + 1.3 === 2.4 // false
withinErrorMargin(1.1 + 1.3, 2.4) // true

 

上面的代码为浮点数运算,部署了一个误差检查函数。



作者:帅气滴糟老头
链接:https://www.jianshu.com/p/4c5e12c78063
来源:简书

转载于:https://www.cnblogs.com/hjbky/p/10953709.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
优化finding函数,#include<algorithm> #include<iostream> #include<vector> #include<string> #include<cmath> #include <cstdio> #include <map> #include <unordered_map> #include <queue> using namespace std; const int INF = 0x3f3f3f3f; int n, gamma, time_count=0; int time[10]; string alpha; vector<int> Length(50005, 0); unordered_map<string, int> number; unordered_map<int, string> nega_number; vector<unordered_map<int, int>> edge(50005); vector<int> trace(50005, 0); vector<int> final_trace; void finding(string alpha) { int a=number[alpha], b; char beta; string epsilon; for(int i=9; i>=0; i--) { for(int j=1; j<10; j++) { epsilon = alpha; epsilon[i] = '0' + (int(epsilon[i]) + j) % 10; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[i]; } } for(int j=i-1; j>=0; j--) { epsilon = alpha; beta = epsilon[j]; epsilon[j] = epsilon[i]; epsilon[i] = beta; if(number.find(epsilon) != number.end() and epsilon != alpha) { b = number[epsilon]; edge[a][b]= time[j]; } } } } void dijkstra(int i) { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; vector<bool> vis(n+1, false); q.push({0, i}); Length[i] = 0; while(!q.empty()) { int u = q.top().second; q.pop(); if(vis[u]) continue; vis[u] = true; for(auto j : edge[u]) { int v = j.first, w = j.second; if(Length[v] > Length[u] + w) { Length[v] = Length[u] + w; trace[v] = u; q.push({Length[v], v}); } } } } int main() { cin>>n; for(int i=2; i<n+1;i++) { Length[i] = INF; } for(int i=0; i<10; i++) { cin>>time[i]; } for(int i=0; i<n; i++) { cin>>alpha; nega_number[i] = alpha; number[alpha] = i+1; } for(int i=0; i<n; i++) { alpha = nega_number[i]; finding(alpha); } dijkstra(1); if(Length[n] == INF) { cout<<"-1"; } else { gamma = n; final_trace.push_back(gamma); cout<<Length[n]<<endl; while(gamma != 1) { gamma = trace[gamma]; final_trace.push_back(gamma); } cout<<final_trace.size()<<endl; for(int i=final_trace.size()-1;i>-1;i--) { cout<<final_trace[i]<<" "; } } system("pause"); return 0; }
05-28

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值