gpt4 book ai didi

c - 并内并和 splinter 矩阵加法

转载 作者:行者123 更新时间:2023-11-30 16:20:59 26 4
gpt4 key购买 nike

XMFLOAT4X4 在矩阵定义中使用并内的并,但是当我尝试使用和不使用额外并的相同方法时,我会在访问模式中得到不同的结果。我不确定这是否是在 union 的初始化中,或者我在实现中可能会出错的地方。在我的代码中,单 union 不起作用,但现在在此代码中,双 union 不起作用。这是为什么?

#include <stdio.h>

typedef union{
union{
struct{float xx,xy,xz,xw, yx,yy,yz,yw, zx,zy,zz,zw, wx,wy,wz,ww;};
float m[4][4];
};
}Mat4DoubleUnion;

typedef union{
struct{float xx,xy,xz,xw, yx,yy,yz,yw, zx,zy,zz,zw, wx,wy,wz,ww;};
float m[4][4];
}Mat4SingleUnion;

int main(void){
Mat4SingleUnion mat_single_1 =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
Mat4SingleUnion mat_single_2 =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
Mat4DoubleUnion mat_double_1 =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};
Mat4DoubleUnion mat_double_2 =
{1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1};

Mat4DoubleUnion out_double;
Mat4SingleUnion out_single;
for(unsigned i = 0; i < 4; ++i){
for(unsigned j = 0; j < 4; ++j){
out_double.m[i][j] = mat_double_1.m[i][j] + mat_double_2.m[i][j];
out_single.m[i][j] = mat_single_1.m[i][j] + mat_single_2.m[i][j];
}
printf("Double Union\n");
for(unsigned i = 0; i < 4; ++i){
printf("[%f %f %f %f]\n", out_double.m[i][0], out_double.m[i][1], out_double.m[i][2], out_double.m[i][3]);
}
printf("\n");
printf("Single Union\n");
for(unsigned i = 0; i < 4; ++i){
printf("[%f %f %f %f]\n", out_single.m[i][0], out_single.m[i][1], out_single.m[i][2], out_single.m[i][3]);
}
printf("\n");
}


}

输出如下。您可以注意到整个循环过程中出现的大量数字,并且当您到达末尾时,两个矩阵的总和不是正确的输出。

Double Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 -170141183460469231731687303715884105728.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]
[304728612076653271699817077145600.000000 18030524136642386826155261952.000000 269154975309704986624.000000 73908519441581681410048.000000]

Single Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]

Double Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]
[304728612076653271699817077145600.000000 18030524136642386826155261952.000000 269154975309704986624.000000 73908519441581681410048.000000]

Single Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]

Double Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 2.000000 0.000000]
[304728612076653271699817077145600.000000 18030524136642386826155261952.000000 269154975309704986624.000000 73908519441581681410048.000000]

Single Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 2.000000 0.000000]
[0.000000 0.000000 0.000000 0.000000]

Double Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 2.000000 0.000000]
[0.000000 0.000000 0.000000 2.000000]

Single Union
[2.000000 0.000000 0.000000 0.000000]
[0.000000 2.000000 0.000000 0.000000]
[0.000000 0.000000 2.000000 0.000000]
[0.000000 0.000000 0.000000 2.000000]

最佳答案

尝试更改这两行。这是一个非常微妙的使用前启动问题。

Mat4DoubleUnion out_double; memset(&out_double, 0, sizeof(out_double));
Mat4SingleUnion out_single; memset(&out_single, 0, sizeof(out_single));

在每个外循环中,您只启动了一行 out_single 和 out_double,因此其余行都是垃圾。在最后一个外循环中,所有行均已正确启动,输出正确。我认为未启动的使用是未定义的行为,所以...

你可以尝试内存检查工具valgrind,在这种情况下会告诉很多信息:

valgrind --tool=memcheck ./a.out

关于c - 并内并和 splinter 矩阵加法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55055449/

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