mistake in simple math function

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2008
Posts: 2
Reputation: ucza is an unknown quantity at this point 
Solved Threads: 0
ucza ucza is offline Offline
Newbie Poster

mistake in simple math function

 
0
  #1
Jan 8th, 2009
I made some simple mistake (conversion?) in my program.. I will be gratefull if anyone has an idea what's wrong with it The problem is not with the algorithm itself so I am not explaining it - the problem is: why the condition placed in the code between // HERE and // HERE seems to be never true? (The function does not work correctly) Try this piece of code with 0.7 and 4 on entry.

  1. int s(int k) // factorial for integers
  2. {
  3. if (k==0 || k==1) {return 1;}
  4. else {return (s(k-1))*k;}
  5. }
  6.  
  7. double newtonsymbol(double r, int k)
  8. {
  9.  
  10. if (k<0) return 0;
  11. int s(int k); //that's factorial above
  12. static double t=r;
  13. double h;
  14. h=r+k-1.0;
  15. static double numerator=r;
  16. double denominator;
  17. std::cout<<h-t<<" "<<t<<std::endl;
  18.  
  19. /* that's optional; it shows the mistake; the condition would be fulfilled if h-t==0 */
  20. //HERE
  21. if (t==h)
  22. //HERE
  23. {denominator=s(k); std::cout<<"ok"<<std::endl;
  24. return (numerator/denominator);}
  25. if (h<-10) {return 3;}
  26. // that is added to stop the function so you can see what's going on
  27. {std::cout<<"not ok"<<std::endl;
  28. numerator=r*newtonsymbol(r-1,k));}
  29.  
  30. }
  31.  
  32. int main ()
  33. {
  34. double p=0.7; int l=4;
  35. std::cout<<newtonsymbol(p,l);
  36. system("pause");
  37. return 0;
  38. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,047
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: mistake in simple math function

 
1
  #2
Jan 8th, 2009
Why would they be equal? You would only expect them to be equal if k is 1.0.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: mistake in simple math function

 
0
  #3
Jan 8th, 2009
Never compare floats using ==
http://c-faq.com/fp/fpequal.html
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 2
Reputation: ucza is an unknown quantity at this point 
Solved Threads: 0
ucza ucza is offline Offline
Newbie Poster

Re: mistake in simple math function

 
0
  #4
Jan 8th, 2009
Thanks for quick help! These are my first steps in programming
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC