如果文件(而不是模块)中有一个独立函数,并且您使用单精度调用它,而它需要双精度数字:
主.f90:
program main
call test(1.0)
end program main
测试.f90:
subroutine test(a)
double precision :: a
print *, "a", a
end subroutine
在这种情况下,编译器如何从单精度“转换”为双精度?使用浮点格式,我希望这些位在转换期间保持不变,但会附加额外的零。那是:
1 = 0 01111111 00000000000000000000000 in single-precision
我预计最终值为 2^(-7):
0 01111111000 0000000000000000000000000000000000000000000000000000 in double precision
令人惊讶的是,对于 gfortran 6.4.0,最终值为 5.2635442471208903E-315。
编译器不进行强制转换。你写的不是Fortran。
主程序中的子程序test
有一个隐式接口。本质上,编译器对此一无所知,除了它是一个子例程。您还告诉它它有一个(默认)实参。
在引用子例程时提供正确的参数类型和种类是您的责任,而不是编译器的责任。你失败了,所以你没有兼容的 Fortran 程序。 Fortran 编译器不欠你任何东西。
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".
如果您为主程序中的子例程提供显式接口,仍然不会进行强制转换,但编译器会注意到不匹配。同样,即使存在隐式接口,某些编译器(可能具有某些编译标志)也会进行一些检查。
1 For details of the likely pass-by-reference, see the comment by user5713492.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)