在使用错误类型的参数调用外部Fortran函数时会发生什么? [英] What happens when calling external an Fortran function with the wrong type of arguments?

查看:95
本文介绍了在使用错误类型的参数调用外部Fortran函数时会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文件中有独立功能(而不是模块中),并且以单精度调用它,而它需要双精度数:

If you have a standalone function in a file (not in a module) and you call it with a single-precision while it expects a double precision number:

main.f90:

program main

  call test(1.0)
end program main

test.f90:

subroutine test(a)
    double precision :: a
    print *, "a", a
end subroutine

在这种情况下,编译器如何从单精度转换"为双精度? 使用浮点格式,我希望这些位在转换期间保持不变,但会附加附加零.那就是:

How does the compiler "casts" from single to double precision in this case ? Using floating-point format, I would expect the bits to stay the same during the cast but additionnal zeroes to be appended. That is:

1 = 0 01111111 00000000000000000000000 in single-precision

,我希望最终值为2 ^(-7):

and I would expect the final value to be 2^(-7):

0 01111111000 0000000000000000000000000000000000000000000000000000 in double precision

令人惊讶的是,对于gfortran 6.4.0,最终值为5.2635442471208903E-315.

Surprisingly, with gfortran 6.4.0, the final value is 5.2635442471208903E-315.

推荐答案

编译器不进行任何强制转换.您写的不是Fortran.

The compiler does no casting. What you have written is not Fortran.

在主程序中,子例程test具有隐式接口.本质上,编译器对此是一无所知,只不过它是一个子例程.您还告诉它它只有一个(默认)实参.

In the main program the subroutine test has an implicit interface. Essentially, the compiler knows nothing about this except that it is a subroutine. You also tell it that it has a single (default) real argument.

在引用子例程时提供正确的类型和类型的参数是您的责任,而不是编译器的责任.您在此处失败,因此没有兼容的Fortran程序. Fortran编译器不欠您任何费用.

It is your responsibility, not the compiler's, to provide the correct type and kind of argument when referencing the subroutine. You failed there, so you do not have a compliant Fortran program. The Fortran compiler owes you nothing.

您将观察到的内容将取决于Fortran处理器的实现细节.该子例程需要一个双精度参数,并且没有理由相信它还有其他内容.不管有复制/复制输出还是某些地址通过 1 ,内存的解释都将不匹配.在哑元参数中,除与实际参数的默认实数对应的字节以外的所有字节均为垃圾".

What you will observe will depend on implementation details of the Fortran processor. The subroutine expects a double precision argument and has no reason to believe it has anything else. Whether there is copy-in/copy-out or some address passing1, the interpretation of memory will not match. In the dummy argument, all but the bytes corresponding to the default real of the actual argument are "junk".

如果在主程序中为子例程提供显式接口,则仍然不会进行强制转换,但是编译器会注意到不匹配.同样,即使存在隐式接口,某些编译器(可能带有某些编译标志)也会进行一些检查.

If you provide an explicit interface for the subroutine in the main program there will still be no casting, but the compiler will notice the mismatch. Equally, some compilers (perhaps with certain compile flags) will do some checking even when an implicit interface is there.

1 有关可能通过引用的详细信息,请参阅user5713492的注释.

1 For details of the likely pass-by-reference, see the comment by user5713492.

这篇关于在使用错误类型的参数调用外部Fortran函数时会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆