gpt4 book ai didi

c - 对两个 float4[] 数组求和,而不是 int[] 数组(代码更改)

转载 作者:行者123 更新时间:2023-11-30 17:33:17 33 4
gpt4 key购买 nike

我想知道,以下代码需要进行哪些更改才能对两个 float4[] 数组而不是两个 int[] 数组求和?

我尝试了一些方法,但没有成功

#include <postgres.h>
#include <fmgr.h>
#include <utils/array.h>

PG_MODULE_MAGIC;

Datum int4_array_add(PG_FUNCTION_ARGS);
PG_FUNCTION_INFO_V1(int4_array_add);

/**
* Returns the sum of two int arrays.
* by Matt Solnit on Stack Overflow - http://stackoverflow.com/questions/16992339
*/
Datum
int4_array_add(PG_FUNCTION_ARGS)
{
// The formal PostgreSQL array objects:
ArrayType *array1, *array2;

// The array element types (should always be INT4OID):
Oid arrayElementType1, arrayElementType2;

// The array element type widths (should always be 4):
int16 arrayElementTypeWidth1, arrayElementTypeWidth2;

// The array element type "is passed by value" flags (not used, should always be true):
bool arrayElementTypeByValue1, arrayElementTypeByValue2;

// The array element type alignment codes (not used):
char arrayElementTypeAlignmentCode1, arrayElementTypeAlignmentCode2;

// The array contents, as PostgreSQL "datum" objects:
Datum *arrayContent1, *arrayContent2;

// List of "is null" flags for the array contents:
bool *arrayNullFlags1, *arrayNullFlags2;

// The size of each array:
int arrayLength1, arrayLength2;

Datum* sumContent;
int i;
ArrayType* resultArray;

// Determine the array element types.
arrayElementType1 = ARR_ELEMTYPE(array1);
get_typlenbyvalalign(arrayElementType1, &arrayElementTypeWidth1, &arrayElementTypeByValue1, &arrayElementTypeAlignmentCode1);
arrayElementType2 = ARR_ELEMTYPE(array2);
get_typlenbyvalalign(arrayElementType2, &arrayElementTypeWidth2, &arrayElementTypeByValue2, &arrayElementTypeAlignmentCode2);

// Extract the array contents (as Datum objects).
deconstruct_array(array1, arrayElementType1, arrayElementTypeWidth1, arrayElementTypeByValue1, arrayElementTypeAlignmentCode1,
&arrayContent1, &arrayNullFlags1, &arrayLength1);
deconstruct_array(array2, arrayElementType2, arrayElementTypeWidth2, arrayElementTypeByValue2, arrayElementTypeAlignmentCode2,
&arrayContent2, &arrayNullFlags2, &arrayLength2);

// Create a new array of sum results (as Datum objects).
sumContent = palloc(sizeof(Datum) * arrayLength1);

// Generate the sums.
for (i = 0; i < arrayLength1; i++)
{
sumContent[i] = arrayContent1[i] + arrayContent2[i];
}

// Wrap the sums in a new PostgreSQL array object.
resultArray = construct_array(sumContent, arrayLength1, arrayElementType1, arrayElementTypeWidth1, arrayElementTypeByValue1, arrayElementTypeAlignmentCode1);

// Return the final PostgreSQL array object.
PG_RETURN_ARRAYTYPE_P(resultArray);
}

我尝试过的是强制执行操作:

sumContent[i] = arrayContent1[i] + arrayContent2[i];

至:

sumContent[i] =  Float4GetDatum(arrayContent1[i]) +  Float4GetDatum(arrayContent2[i]);

并致:

sumContent[i] =  DatumGetFloat4(arrayContent1[i]) +  DatumGetFloat4(arrayContent2[i]);

但它只给了我 0 和奇怪的负数作为我的 c 函数的输出:

create or replace function sumar(float4[],float4[])
returns float4[]
as 'example.so', 'sumar'
Language C strict;

最佳答案

这一行

sumContent[i] =  Float4GetDatum(arrayContent1[i]) +  Float4GetDatum(arrayContent2[i]);

肯定是无稽之谈 - 你不能通过这个语句对两个数据求和,你应该使用反向宏:

sumContent[i] = Float4GetDatum( DatumGetFloat4(arrayContent1[i]) +  DatumGetFloat4(arrayContent2[i]));

下一个建议 - 请确保在函数开始时使用 elog(NOTICE, "sometext"),这样 SQL 才真正使用您的函数。

关于c - 对两个 float4[] 数组求和,而不是 int[] 数组(代码更改),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23797115/

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