By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,064 Members | 1,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,064 IT Pros & Developers. It's quick & easy.

Using C++ for equation, Gaussian probability

P: n/a
I'm trying to write a programme a c++ program to evaluate the Gaussian probability for x=0,1.5,3 where standard deviation =0.5 and the mean is 1.5 this is what I have
Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <cmath>
  4. #include <cfloat>
  5. using namespace std;
  7. int main(int argc, char *argv[])
  8. {double x1,x2,x3,u,o,g1,g2,g3,j1,j2,j3;
  9. x1=0;
  10. x2=3/2;
  11. x3=3;
  12. u=3/2;
  13. o=1/2;
  14. j1=x1-u;
  15. j2=x2-u;
  16. j3=x3-u;
  17. g1=((exp(0)/2*pow(o,2))/(o*sqrt(2*M_PI)));
  18. g2=(1/(o*sqrt(2*M_PI)))*exp(-pow(j2,2)/2*pow(o,2));
  19. g3=(1/(o*sqrt(2*M_PI)))*exp(-pow(j3,2)/2*pow(o,2));
  20. cout<< "for x equal to 0 the guassian ="<<g1<<endl;
  21. cout<< "for x equal to 1.5 the guassian ="<<g2<<endl;
  22. cout<< "for x equal to 3.0 the guassian ="<<g3<<endl;
  23.     system("PAUSE");
  24.     return EXIT_SUCCESS;
  25. }
it returns for all of them 1.INF I have no clue what this means.
Sep 28 '10 #1
Share this Question
Share on Google+
2 Replies

Expert 100+
P: 2,400
Expand|Select|Wrap|Line Numbers
  1. x1=0; 
  2. x2=3/2; 
  3. x3=3; 
  4. u=3/2; 
  5. o=1/2;
It is unwise to use rational constants to initialize floating-point variables. The compiler sees an expression with integer terms so it uses integer math and assigns the integer result to the variable. For example, the value of 1/2 is 0. It is much better to use floating point constants or floating point terms. Some people [not me] prefer to use casts. For example:
Expand|Select|Wrap|Line Numbers
  1. x2 = 1.5;    // floating point constant.
  2. u = ((double)3)/((double)2);   // cast.
  3. o = 1.0/2.0;   // floating point terms.
Sep 28 '10 #2

Expert 100+
P: 983

The problem is that you're generating infinities. That's what the program is trying to tell you.

Don was exactly correct in observing that you can't use "rational" numbers in the way you have.

Basically, when you write
Expand|Select|Wrap|Line Numbers
  1. x2=3/2;
This means
Expand|Select|Wrap|Line Numbers
  1. x2=1;
In your program.

This is because you didn't include a decimal point or other floating point indicator in your constants three (3) and two (2). So the compiler did exactly what you told it to do, which is to divide one integer by the other.

What got you is that dividing integers returns an integer result. Thus, 3/2 is equal to the integer value one (1), and not the floating point value one-and-a-half (1.5).

So, if you want to write rational number style constants, you must include one of the floating-point forms that Don illustrated.

Good Luck!
Sep 28 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.