User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 456,562 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,507 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 652 | Replies: 4
Reply
Join Date: Oct 2007
Posts: 2
Reputation: jonbuckets is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonbuckets jonbuckets is offline Offline
Newbie Poster

Troubleshooting Need Help Fast!!!

  #1  
Oct 25th, 2007
Need to figure out what is going wrong with my source code. When I imput the values of 72, 69, and 2.8 respectively I should output pdf=0.85801, however I am not getting that value. I don't know why. I am doing the adaptive simpsons method to find the probability density of data under a normal curve. I believe that I might have the "approx" in the wrong spot, or I'm not calling my functions correctly. This like will take you to a PDF of what I am trying to do, I'm totally lost at this point.
http://ezekiel.vancouver.wsu.edu/~cs...ts/pdf/pdf.pdf

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double INV_SQRT_2PI= 0.39894228;						/*1/sqrt(2*pi)*/
double a, b, u, U, O, c, left, right, d, approx, x;
double epsilon= 0.0000001;								/*epsilon defined as 0.0000001*/
double f(double x);										/*prototype functions*/
double S(double a, double b);
double asimpson(double a, double b, double approx, double epsilon);


int main(void)
{	
	printf("Enter x Value: ");
	scanf("%lf", &x);
	printf("Enter Mean: ");
	scanf("%lf", &U);
	printf("Enter Standard Deviation: ");
	scanf("%lf", &O);
	double pdf, u;
	u=((x-U)/O);
	pdf=((1/2)+INV_SQRT_2PI * asimpson(0,u,S(0,u),epsilon));

	printf("pdf= %f",pdf);

return 0;
}
double f(double x)
{	
	return (exp(double(x*x*(-1))/2));
}
double S(double a,double b)
{	
	return ((b-a)/6)*(f(a)+(4*f((a+b)/2))+f(b));
}
double asimpson(double a,double b,double approx, double epsilon)
{
	approx= S(a,b);
	c=((a+b)/2);
	left=S(a,c);
	right=S(c,b);
	d=((left+right-approx)/15);
	if(abs(d)<=epsilon)
		return (left+right+d);
	return (asimpson(a,c,left,epsilon/2)+asimpson(c,b,right,epsilon/2));
}

Thanks
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,018
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 6
Solved Threads: 68
ithelp ithelp is offline Offline
Veteran Poster

Re: Need Help Fast!!!

  #2  
Oct 25th, 2007
Looks like it is a precision problem.
Reply With Quote  
Join Date: Oct 2007
Location: Cherry Hill, NJ
Posts: 1,878
Reputation: Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold Duoas is a splendid one to behold 
Rep Power: 13
Solved Threads: 193
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Need Help Fast!!!

  #3  
Oct 25th, 2007
This is actually one of the most insidious problems people learning C/C++ get.

The problem is one of type.

1/2 is integer
1/2.0 is floating point

So, for example, when you go to calculate the PDF you say
pdf=((1/2)+INV_SQRT_2PI * ...

That one-half there is an integer expression, which gets promoted to double after it is evaluated. Hence, 1 idiv 2 is 0, promoted to double is 0.0, + INV_SQRT_2PI etc..

All the places where you have something like /2 check to make sure your numerator is a double before division occurs. If it isn't, change your denominator to a double (say, /2.0).

Hope this helps.
Reply With Quote  
Join Date: May 2006
Posts: 2,781
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: Need Help Fast!!!

  #4  
Oct 25th, 2007
Originally Posted by Duoas View Post
All the places where you have something like /2 check to make sure your numerator is a double before division occurs. If it isn't, change your denominator to a double (say, /2.0).

Or, more generally, make sure at least one of the values being divided is a floating point value.
Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
Reply With Quote  
Join Date: Oct 2007
Posts: 2
Reputation: jonbuckets is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
jonbuckets jonbuckets is offline Offline
Newbie Poster

Thanks for the help!

  #5  
Oct 25th, 2007
Thanks for the help, that was exactly what was wrong. That was a pain in the ass... I wont forget the way that works anytime soon. Thanks alot everyone! This thread can be removed/deleted/whatever now.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 5:41 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC