gpt4 book ai didi

c - 统计收集程序中的舍入误差 (C)

转载 作者:行者123 更新时间:2023-12-04 05:26:47 36 4
gpt4 key购买 nike

我写了一个计算 1000000 的程序!使用 FFT。

(请允许我简短并省略一些理论共鸣:))

我想要做的是测量 double 值之间的所有舍入误差,并且是 round() -ed 值(使用 math.h 函数)来检查此错误的行为方式(以及是否高于 1/2)。

我这样做是通过打印 a 之间的差异来实现的和 round(a)每次我进行四舍五入并将结果写入文件时,我们称之为 diffs.txt , 即 ~532Mb ,使用
fprintf(my_file,"%e\n",a-round(a));
我现在需要计算出现在该文件中的每个值的出现次数。

在我看来,我这样做是一种错综复杂的方式,使用 grep , sort和一个 bash for如下:

./compute-rounding-err #It creates diffs.txt
sort -u diffs.txt -o diff-sorted-unique
for i in `cat diff-sorted-unique`
do
grep -e "$i" | wc -l >> diff-counted
done

结果是两个文件。如果我配对我获得的文件
diff-sorted-unique:     diff_counted:
-9.013892e-20 1
... ...
0.000000e0 200
... ...
9.930234e 1

我可以获取这些值并从中制作直方图。

我担心在带有 ~532Mb 的笔记本电脑上这样做文件需要很长时间。

有谁知道如何加快速度?

谢谢。

最佳答案

假设您要写入每个 8 字节的 double,其中包含 11-12 个字符,那么您需要的总内存应该约为 450MB,这意味着您拥有的项目数量应该约为 50,000,000。

对 5000 万个值进行排序应该不会花很长时间。需要多长时间的是您的for循环扫描每个项目的整个文件。

更有效的方法是对文件进行排序,但保留重复值。然后,您所需要的只是遍历文件,将相似的值(或相等的值,基于直方图的精度)分组并用值-计数对替换它们。

例如,如果您有以下文件:

1
0.6
-2
0
-1
-0.6
0
0
3

排序后你会得到:
-2
-1
-0.6
0
0
0
0.6
1
3

如果你遵循这个算法:
current_bucket = first value in file, floored to histogram_precision
bucket_count = 0
for all values v
; write current bucket + additional empty buckets
while v > current_bucket + histogram_precision
output current_bucket bucket_count
current_bucket += histogram precision
bucket_count = 0
; add v to current_bucket
bucket_count += 1

给定 histogram_precision以 1 为例,您将获得:
-2       1
-1 2
0 4
1 1
2 0
3 1

其中每一行 num count显示范围内的值 ( count ) 的数量 [num, num+histogram_precision) .

你可能想使用像 [0.5, 1.5) 这样的桶例如代替 [1 2) ,在这种情况下,您应该只调整计算初始存储桶的第一行,或者更改 while 的条件循环到 v > current_bucket + histogram_precision / 2 .

关于c - 统计收集程序中的舍入误差 (C),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13121052/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com