将float数组传递给(void *)函数参数

时间:2013-01-29 20:42:13

标签: c arrays

我正在使用cblas_icamax。我想将浮点向量z13]传递给cblas_icamax(见下文)。我的代码包括以下内容。

    float z[13] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f ,0.f, 0.f, 0.f, 0.f, 0.f};
//...

    for (i = 0; i < 13; i++) {
        printf("%.4f\n", z[i]);
    }
    int cardIndex = cblas_icamax(13,(float*) z,1);
    NSLog(@"cardIndex: %d", cardIndex);

我也尝试了同样的结果。

int cardIndex = cblas_icamax(13,&z,1);

打印结果如下:最大绝对值是138.1086,即位置10,但函数cblas_icamax正在返回5.出了什么问题?

-1.2624
74.1524
52.3533
89.9426
28.8639
-7.6203
-30.2820
48.9747
124.8693
29.4351
138.1086
36.2638
-45.0410

cblas_icamax

返回向量中具有最大绝对值的元素的索引(单精度复合体)。

int cblas_icamax (
   const int N,
   const void *X,
   const int incX
);

2 个答案:

答案 0 :(得分:2)

您的阵列中有13个floats

您承诺在您的数组中有13个float complex值的库(即26 floats)。这种行为并不像预期的那样令人惊讶。将一组复杂数据作为输入传递,或者使用cblas_isamax代替。

为了更准确地解释发生的事情:cblas_icamax返回具有最大L1范数的复数浮点数的索引。每个复数浮点数都是输入的一对浮点值。如果我们查看输入的L1规范:

index        value            L1 norm
0      -1.2624 + 74.1524i     75.4148
1      52.3533 + 89.9426i    142.2959
2      28.8639 -  7.6203i     36.4842
3     -30.2820 + 48.9747i     79.2567
4     124.8693 + 29.4351i    154.3044
5     138.1086 + 36.2638i    174.3724
6     -45.0410 + ???i           ???
7          ??? + ???i           ???
8          ??? + ???i           ???
9          ??? + ???i           ???
10         ??? + ???i           ???
11         ??? + ???i           ???
12         ??? + ???i           ???

'???'字段表示未初始化的数据。因此cblas_icamax返回的结果肯定是合理的;在初始化的字段中,它具有最大的L1范数。当然,实际行为是未定义的,因为您告诉函数从未初始化(可能未映射)的内存中读取。

答案 1 :(得分:1)

我应该调用cblas_isamax,而不是cblas_icamax,因为我的数组不是复数。