473,480 Members | 4,827 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using C++ for equation, Gaussian probability

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;
  6.  
  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
2 2937
donbock
2,426 Recognized Expert Top Contributor
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
Oralloy
988 Recognized Expert Contributor
Simon,

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

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

Similar topics

1
5864
by: Michael Allen | last post by:
Hi all you Java 2D gurus out there. I wonder if you might be able to help me. I've been dipping my toe into the world of Gaussian blurring and with the help of a few articles and some bashing my...
4
8829
by: Xcal | last post by:
Anyone knows how to make a Gaussian fit to a histogram data using Python, or where I can find a library that helps me in this task? Tks everyone... Paxcal
2
3892
by: GBartgo | last post by:
Is there someone who knows how to generate a gaussian mixture in c++?
9
8897
by: Stud Muffin | last post by:
Hey Basically, I'm trying to take objects created in microsoft word using equation editor (for creating clean looking math/physics equations) and putting them into some sort of webpage format....
11
4199
by: Mayank Kaushik | last post by:
hi everyone, im trying to create a function that generates a 1 or a 0, with the probability of a 1 being generated equal to X (which is passed to the function as a parameter). any ideas?...
1
3746
by: j.f.c.neves | last post by:
Hello, I need some help in using the rlft3 (Numerical Recipes in c++ book, Chapter 12) to apply a Gaussian smoothing to a 2D image. How do I create a suitable filter function (page 535)? ...
6
14821
by: Trev17 | last post by:
Hello, I am new to C++ and i have tried for several hours to make a program my teacher has given me as a lab. Here is the Lab question: the roots of the quadratic equation ax^2 + bx + c = 0, a...
3
7768
by: bob | last post by:
I was wondering if anyone can recommend a way to generate a Gaussian random variable with standard deviation 1. Thank you.
2
4440
by: Gerdus van Zyl | last post by:
Does anyone have a relatively fast gaussian blur implemented in pure python? Below is my attempt but it takes 2.9 seconds for a 320x240 image. Image comes from byte string: self.array =...
5
2585
by: Deviate | last post by:
Hi again guys :) Ok i have this problem, i need to solve the equation phi(x) = 0.1 (where phi is the area under the gaussian normal distribution function) apparently im supposed to solve it...
0
6905
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7041
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7080
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6736
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6908
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5331
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4772
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1299
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
561
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.