C 使用数学函数打开MP代码未按预期工作
我有一段代码,可以在给定漂移、波动性和随机数的任何一天计算股票价格。但当我检查输出列表时,它们是算术级数,而不是几何级数(幂函数)。我共享的变量是否有问题 代码如下:C 使用数学函数打开MP代码未按预期工作,c,openmp,C,Openmp,我有一段代码,可以在给定漂移、波动性和随机数的任何一天计算股票价格。但当我检查输出列表时,它们是算术级数,而不是几何级数(幂函数)。我共享的变量是否有问题 代码如下: #include <stdio.h> #include <stdlib.h> #include <omp.h> #include <time.h> #include <math.h> #include <limits.h> #include <strin
#include <stdio.h>
#include <stdlib.h>
#include <omp.h>
#include <time.h>
#include <math.h>
#include <limits.h>
#include <string.h>
int main()
{
long double drift_year,volatility_year,volatility_day,drift_day,drift_mean,stockprice_initial,stockprice[100000],prefixsum[100000];
int i,len=0;
printf("Enter the yearly drift in percentage : "); //Drift factor of stock
scanf("%Lg",&drift_year);
printf("Enter the yearly volatility in percentage : "); //Volatility factor of stock - how much the "random shock" must affect the stock price
scanf("%Lg",&volatility_year);
printf("Enter initial stock price : $"); //Initial Stock Price
scanf("%Lg",&stockprice_initial);
FILE *myFile = fopen("RNG2.txt", "r"); //File with the random numbers I have
while (!feof(myFile))
{
long double number;
fscanf(myFile, "%Lg", &number); //Reading the random numbers from file
prefixsum[len++]=number;
}
fclose(myFile);
drift_day = drift_year/(100*252); //converting per annum drift to per day drift
volatility_day = volatility_year/(100*sqrt(252)); //similarly for volatility
drift_mean = drift_day - (0.5*pow(volatility_day,2)); //average it out
#pragma omp parallel for default(none) private(i) shared(drift_mean,volatility_day,stockprice,stockprice_initial,len,prefixsum) //parallelising code in OpenMP - check for missing variable in shared - mifght be causing error
for(i=0;i<len;i++)
{
stockprice[i] = stockprice_initial*pow(2.71828,((drift_mean*(i+1))+(volatility_day*prefixsum[i]))); //Must give me an exponential curve if volatility is 0;
}
for(i=0;i<len;i++)
{
printf("%d : %Lg\n",i,stockprice[i]);
}
FILE *fp = fopen("StockPrice.txt", "w");
for(i=0;i<len-1;i++)
{
fprintf(fp, "%Lg", stockprice[i]);
fprintf(fp, "\n");
}
fprintf(fp, "%Lg", stockprice[len-1]);
fclose(fp);
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
长双漂移年,波动年,波动日,漂移日,漂移平均值,股价初始值,股价[100000],前缀[100000];
int i,len=0;
printf(“输入年度漂移百分比:”;//库存漂移系数
scanf(“%Lg”和漂移年);
printf(“以百分比形式输入年度波动率:”;//股票波动系数-随机冲击对股票价格的影响程度
scanf(“%Lg”、&u年);
printf(“输入初始股价:$”;//初始股价
scanf(“%Lg”和股票价格首字母);
FILE*myFile=fopen(“RNG2.txt”,“r”);//包含随机数的文件
而(!feof(myFile))
{
长双数;
fscanf(myFile,“%Lg”,&number);//从文件中读取随机数
前缀sum[len++]=数字;
}
fclose(myFile);
漂移日=漂移年/(100*252);//将每年漂移转换为每天漂移
波动率\日=波动率\年/(100*sqrt(252));//波动率也是如此
漂移平均值=漂移日-(0.5*pow(波动率,2));//求平均值
#pragma omp parallel for default(none)private(i)shared(漂移平均值、波动率日、股价、股价首字母、len、前缀)//OpenMP中的并行代码-检查shared中缺少的变量-mifght可能导致错误
对于(i=0;i打开警告
drift\u year
在此行未初始化的情况下使用:
drift_day = drift_year/(100*252);
挑剔:pow(2.71828,x)
通常呈现为exp(x)
.exp也没有帮助..我在没有openmp的情况下使用了它,它仍然会产生同样的问题顺便说一句,我上传了随机数文件也用于诊断,你可以用它自己编译并检查实际上,我在放这行时漏掉了那一行..我把它作为我代码中的输入