返回信息流最近在学习并发计算,因为已经很久没有用c++了所以想着用golang来改写然后再实现并发,结果第一步就扑街了…改写后的结果和原来的并不一致…本菜鸡学艺不精找不到解决办法,所以来论坛向各位大佬求助
代码是实现mandelbrot set的计算,矩阵大小为230*790,每个点迭代24000次。c++和go的代码都不长
怀疑是数据精度的问题,但是改了很多次结果还是对不上。不知道大家有没有什么好的解决办法
==================c++=====================
int main(){
int max_row = 230, max_column = 790, max_n = 24000;
char **mat = (char**)malloc(sizeof(char*)*max_row);
for (int i=0; i<max_row;i++)
mat[i]=(char*)malloc(sizeof(char)*max_column);
for(int r = 0; r < max_row; r++){
for(int c = 0; c < max_column; c++){
complex<float> z;
int n = 0;
while(abs(z) < 2 && n < max_n){
z = pow(z, 2) + decltype(z)(
(float)c * 2 / max_column - 1.5,
(float)r * 2 / max_row - 1
);
n++;
}
mat[r][c]=(n == max_n ? '#' : '.');
}
}
for(int r = 0; r < max_row; ++r){
for(int c = 0; c < max_column; ++c)
std::cout << mat[r][c];
cout << '\n';
}
}
=====================Golang===================
func main() {
maxRow, maxColumn, maxN := 230, 790, 24000
var mat [][]string
for r := 0; r < maxRow; r++ {
var tmp []string
for c := 0; c < maxColumn; c++ {
var z complex128
n := 0
for ; cmplx.Abs(z) < 2 && (n < maxN); n++ {
z = z*z + complex(
float64(c)*2/float64(maxColumn)-1.5,
float64(r)*2/float64(maxRow)-1)
}
if n == maxN {
tmp = append(tmp, "#")
} else {
tmp = append(tmp, ".")
}
}
mat = append(mat, tmp)
}
for i := 0; i < len(mat); i++ {
for j := 0; j < len(mat[i]); j++ {
fmt.Print(mat[i][j])
}
fmt.Println("")
}
}
这是一条镜像帖。来源:北邮人论坛 / golang / #1879同步于 2020/6/7
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Golang机器人发帖
【问题】【求助】用go改写c++程序遇到问题
Sizenr1996
2020/6/7镜像同步4 回复
订阅后,新回复会通过你的通知中心匿名送达。
4 条回复
看代码好像是直接在控制台输出的?建议输出成图片格式进行可视化比较精确
如果只是在边界处的点有不同完全是可以理解的,分形边界最终的值受误差影响很大,即使两边用相同的浮点数据类型(也许两边都换成64位浮点数会好一点?),底层采用sse还是avx都有可能是误差的来源
【 在 Sizenr1996 (莉莉娅) 的大作中提到: 】
: 最后两个for循环就是画图输出, 我比较过两个代码的图,确实有不一样的地方。也试过把go代码里的float64改为32,但是还是不一样