简体   繁体   English

比较两个包含双打的文件

[英]compare two files containing doubles

I want to check equality of two files containing double numbers. 我想检查两个包含double数字的文件是否相等。

I should consider near numbers equal eg differences of at most 0.0001 . 我应该考虑接近的数字相等,例如最多相差0.0001

It's easy to write a tester for that with C but is there an easier way? C编写测试器很容易,但是有没有更简单的方法呢? eg bash commands? 例如bash命令?

Here is one way you can do it: 这是您可以执行的一种方法:

paste file1 file2  | awk '{d=$1-$2;if((d<0?-1*d:d)>0.0001) print $0 " " d }'

First use paste to print out corresponding lines. 首先使用paste打印出相应的行。 Then pass them to awk to subtract. 然后将它们传递给awk减去。 Calculate the absolute difference and check if it is more than your tolerance of 0.00001. 计算绝对差,并检查它是否大于您的0.00001公差。 If so, print out both values and the difference. 如果是这样,请打印出两个值和差异。

Bash does not provide operators for floating point manipulations. Bash不提供用于浮点操作的运算符。 You can look up bc and it should be fairly easy to integrate it in a bash script. 您可以查找bc,将其集成到bash脚本中应该很容易。

See this article on Linux Journal. 请参阅Linux Journal上的这篇文章 That should provide you with a starting point. 那应该为您提供一个起点。 It is upto you to work through the file structure. 您可以自行决定文件的结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM