【发布时间】:2019-07-03 05:06:20
【问题描述】:
-使用双精度 - 使用 sqrt() 和指数函数 exp() - 使用 * 计算平方 - 不要使用 pow()
我得到的值与我的预期完全不同。我尝试让它们全部签名,但它没有改变任何东西,我尝试用 12 位小数打印,但似乎没有任何效果。我已经链接了数学库并定义了它。
double normal(double x, double sigma, double mu)
{
double func = 1.0/(sigma * sqrt(2.0*M_PI));
double raise = 1.0/2.0*((x-mu)/sigma);
double func1 = func * exp(raise);
double comp_func = (func1 * func1);
return comp_func;
}
int main(void)
{
// create two constant variables for μ and σ
const double sigma, mu;
//create a variable for x - only dynamic variable in equation
unsigned int x;
//create a variable for N values of x to use for loop
int no_x;
//scaniing value into mu
printf("Enter mean u: ");
scanf("%lf", &mu);
//scanning value into sigma
printf("Enter standard deviation: ");
scanf("%lf", &sigma);
//if sigma = 0 then exit
if(sigma == 0)
{
printf("error you entered: 0");
exit(0);
}
//storing number of x values in no_x
printf("Number of x values: ");
scanf("%d", &no_x);
//the for loop where i am calling function normal N times
for(int i = 1; i <= no_x; i++)
{
//printing i for the counter in prompted x values
printf("x value %d : ", i);
// scanning in x
scanf("%lf", &x);
x = normal(x,sigma,mu);
printf("f(x) = : %lf.12", x);
printf("\n");
}
return 0;
}
C:>.\a.exe 输入平均 u:3.489 输入 std dev s: 1.203 x 值的数量:3 x 值 1:3.4 f(X) = 0.330716549275 x 值 2:-3.4 f(X) = 0.000000025104 x 值 3:4 f(X) = 0.303015189801
但这是我收到的
C:\Csource>a.exe 输入平均 u:3.489 输入标准差:1.203 x 值的数量:3 x 值 1:3.4 f(x) = : 15086080.000000 x 值 2:-3.4 f(x) = : 15086080.000000 x 值 3 : 4 f(x) = : 1610612736.000000
【问题讨论】:
-
您发布的代码似乎不太可能是您编译和执行的代码。发布的代码未包含
<math.h>、<stdio.h>和<stdlib.h>。它将sigma和mu的地址传递给scanf,但它们是const,而scanf需要指向不是const的东西的指针。对于%lf,它还将x传递给scanf,但x被声明为unsigned int,而%lf需要double。除非您的编译器非常宽容并且您忽略了警告消息,否则不应该编译。 -
我确实包含了 '
' 和两个标准以及定义。我的代码使用 gcc 编译。所以也许它非常宽容,但我没有收到一条警告信息。
标签: c signed normal-distribution negative-number exp