我需要从Fortran程序中的文件中读取大量数据.数据的大小是可变的,所以我想动态分配数组.我的想法是创建一个子程序,读取所有数据并分配内存.该计划的简化版本是:
program main
implicit none
real*8, dimension(:,:), allocatable :: v
integer*4 n
!This subroutine will read all the data and allocate the memory
call Memory(v,n)
!From here the program will have other subroutines to make calculations
end
subroutine Memory(v,n)
implicit none
real*8, dimension(:,:), allocatable :: v
integer*4 n,i
n=5
allocate(v(n,2))
do i=1,n
v(i,1)=1.0
v(i,2)=2.0
enddo
return
end subroutine Memory
Run Code Online (Sandbox Code Playgroud)
这个程序给我以下错误:
Error: Dummy argument 'v' of procedure 'memory' at (1) has an attribute that requieres an explicit interface for this procedure …Run Code Online (Sandbox Code Playgroud) 我正在尝试找到可以存储在单个精确浮点数中的最小值(最接近零).使用<limits>标题我可以得到值,但如果我把它变小,浮动仍然可以保持它,它给出了正确的结果.这是一个用g ++ 5.3.0编译的测试程序.
#include <limits>
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
float a = numeric_limits<float>::max();
float b = numeric_limits<float>::min();
a = a*2;
b = b/pow(2,23);
cout << a << endl;
cout << b << endl;
}
Run Code Online (Sandbox Code Playgroud)
正如我所预料的那样,"a"给出无穷大,但是"b"在将最小值除以2 ^ 23之后仍然保持良好的结果,之后它给出0.
给出的值numeric_limits<float>::min()是2 ^( - 126),我相信这是正确答案,但为什么我的程序上的浮点数保持这么小的数字?
所以我想创建一组浮点数,其中两个数字在给定一定容差时被认为是相等的.例如,如果我有一套
a = set([1, 2, 3])
Run Code Online (Sandbox Code Playgroud)
如果我添加元素1.00001,容差为1e-4,则结果集应为
{1, 2, 3}
Run Code Online (Sandbox Code Playgroud)
并不是
{1, 1.00001, 2, 3}
Run Code Online (Sandbox Code Playgroud) 所以我有以下代码:
#include <math.h>
int main (void) {
float max = fmax (1.0,2.0);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
哪个可以编译并正常运行,但是如果不是将1.0和2.0传递给函数,则传递带有这些值的a,b:
#include <math.h>
int main (void) {
float a = 1.0; float b = 2.0;
float max = fmax (a,b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
undefined reference to `fmax'
Run Code Online (Sandbox Code Playgroud)
有什么区别?我做错了什么?
我正在使用以下命令进行编译:
c99 fmax_test.c
Run Code Online (Sandbox Code Playgroud)