gpt4 book ai didi

c - 为什么在将复数从 fortran 传递到 c 时不能 printf

转载 作者:行者123 更新时间:2023-11-30 19:27:21 25 4
gpt4 key购买 nike

我正在尝试打印一个一维复数数组 (c_double_complex),该数组填充在 Fortran90 代码中并在 C 代码中分配。

但是,当从 Fortran 代码传递数组后在 C 中打印数组(使用 %f)时,仅打印数组的第一个复数,其余为零。

我已经尝试过使用整数和 double ,效果很好。

subroutine F_sub ( a, array ) bind (C, name="F_sub")
use, intrinsic :: iso_c_binding
implicit none
integer (c_int) :: a
complex (c_double_complex), dimension (a) :: array

array = [ 2.5 , 4.4 ]
a = 18
end subroutine F_sub
#include <stdio.h>
#include <stdlib.h>

void F_sub ( int *a, double _Complex * array_ptr );

int main ( void ) {

double _Complex * array_ptr;
int a;
a=2;
array_ptr = malloc (8* sizeof(double _Complex));

F_sub (&a, array_ptr);

printf ( "Values are: %f %f and a= %d\n", array_ptr [0], array_ptr [1] , a);
free(array_ptr);
return 0;
}
output: Values are: 2.500000 0.000000 and a= 18

谁能告诉我上面代码的问题出在哪里吗?

最佳答案

简单地说, printf() 的 POSIX 规范都不是也不是 Standard C specification包括用于打印复数的任何转换格式。您必须将复数的实部和虚部分别传递给 printf() ,并提供 2 个转换说明符。例如,您可以使用 cimag() creal() 来自<complex.h>和通用格式 %g ( + 确保始终有一个符号 +- ):

printf("%g%+gi", creal(array_ptr[0]), cimag(array_ptr[0]));

我可能会为自己创建一个函数来处理复数的格式,并在任何地方使用它,而不是重复编写代码。

<小时/>

来自comment :

… my concern is not related to the printing part of the complex number; it is about why the C code can not print the second number (4.4) in the array and only the first one is printed (2.5).

参见§6.2.5 Types ¶11ff :

¶11 There are three complex types, designated as float _Complex, double _Complex, and long double _Complex.43) (Complex types are a conditional feature that implementations need not support; see 6.10.8.3.) The real floating and complex types are collectively called the floating types.

¶12 For each floating type there is a corresponding real type, which is always a real floating type. For real floating types, it is the same type. For complex types, it is the type given by deleting the keyword _Complex from the type name.

¶13 Each complex type has the same representation and alignment requirements as an array type containing exactly two elements of the corresponding real type; the first element is equal to the real part, and the second element to the imaginary part, of the complex number.

因此,C 复数被视为两个值的数组。在 C 代码中,array_ptr[0]是一个复数。您应该使用 creal()cimag()或类似的函数将两个部分从一个数字中取出。 AFAICS,你的 Fortran 函数(我只知道 Fortran 77 - 我从来没有玩过 Fortran 90,并且显示的代码与 F77 完全不同!)仅设置一个复数。所以我不相信有array_ptr[1]供您访问;你会得到未定义的行为。我不确定我是否能很好地解释为什么你会得到任何明智的东西。我希望array_ptr[0]相当于一个指针,代码不会打印它指向的数字。

我有gfortran (从 GCC 8.3.0 开始);我稍后可以尝试你的代码。

我对出了什么问题有一些不好的想法 - 我怀疑你需要这个的一个小变体:

double _Complex value;

F_sub(&a, &value);

printf("Values are: %f %f and a= %d\n", creal(value), cimag(value), a);

如果这是正确的,那么您就可以摆脱蓝色谋杀(或未定义的行为),并且不幸的是代码没有崩溃。您也有可能不需要 &&value在调用 F_sub() 的电话中.

我还怀疑free(array_ptr)你的代码中是错误的——我不明白为什么 Fortran 会分配空间。

关于c - 为什么在将复数从 fortran 传递到 c 时不能 printf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55858269/

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