【发布时间】:2013-01-14 21:17:46
【问题描述】:
这是编写正态分布函数http://en.wikipedia.org/wiki/Normal_distribution 的正确方法还是我应该使用 pow 函数?我真的很困惑,所以非常感谢您的帮助:)
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <stdlib.h>
int main()
{
double u,s, N, x1,math1, math2, math3,n, v, x;
printf("Enter Mean: ");
scanf("%lf", &u);
printf("Enter Standard Deviation: ");
scanf("%lf", &s);
printf("Enter Number Of Inputs: ");
scanf("%lf", &N);
for (v=1; v<=N; v++)
{
printf("Enter Value: ");
scanf("%lf", &x);
n=(-1/2);
printf("f(x)= ");
math1 =1/(s*sqrt(2*M_PI));
math2= (x-u)/s * (x-u)/s;
math3= M_E * exp(n);
x1 = math1 * exp(math3)*exp(math2);
printf("%lf \n", x1);
}
system("Pause");
}
【问题讨论】:
-
理想情况下,您应该在同一个函数中混合来自用户的输入和输出。
-
非常接近 stackoverflow.com/questions/2328258/… 的副本 - cmets 中甚至有一些关于为什么不应该使用
pow()的讨论。 -
-1/2- 这不是你想的那样。
标签: c