例如:
0.1+0.2 !== 0.3
0.1+0.3 === 0.4
0.1+0.4 === 0.5
解决精度问题:
1,规定一定范围内 数据的误差允许则等价于相等
export const EPSILON = 0.000001;
/*
* EPSILON(小于等于1.0的值采用绝对公差,大于1.0的值采用相对公差)
* @param a The first number to test.
* @param b The second number to test.
* @return True if the numbers are approximately equal, false otherwise.
*/
export function equals (a: number, b: number) {
return Math.abs(a - b) <= EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b));
}
2,使用字符串进行比对
参考资料:
https://www.iteye.com/blog/thihy-1867577