problem using floor function

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster

problem using floor function

 
0
  #1
Oct 16th, 2009
i am writing a function to convert fractions to intgers for example:
.636 -> 636 or .45 -> 45

  1. int ret_int(double input)
  2. {
  3. while(true)
  4. {
  5. // this line is for debugging only
  6. cout << input << " " << floor(input) << endl;
  7.  
  8. if(input == floor(input)) return input;
  9. input = input * 10;
  10. }
  11. }

for some inputs it is working fine for example:

Click image for larger version

Name:	1.jpg
Views:	8
Size:	20.4 KB
ID:	12148


But for some inputs :
Click image for larger version

Name:	2.jpg
Views:	8
Size:	29.6 KB
ID:	12149

which i cant understand the floor and the input are the same so why the function does not return

some inputs also do this:

Click image for larger version

Name:	3.jpg
Views:	7
Size:	25.5 KB
ID:	12150

floor(23450) = 23499

weird?
if anybody has another approach (another method) to do the job i would be thankful
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 54
Reputation: ryuslash is an unknown quantity at this point 
Solved Threads: 10
ryuslash's Avatar
ryuslash ryuslash is offline Offline
Junior Poster in Training
 
-1
  #2
Oct 16th, 2009
I had very similar output in one of my project, it seems I declared a double somewhere, but didn't instantiate it...
  1. double somedouble;
I don't know if this is applicable to you, but now that I've instantiated it, it seems to be better...
  1. double somedouble = 0.0;
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster
 
0
  #3
Oct 16th, 2009
No there is nothing to initialize
and even if why?
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 17
Reputation: Zjarek is an unknown quantity at this point 
Solved Threads: 6
Zjarek Zjarek is offline Offline
Newbie Poster
 
1
  #4
Oct 16th, 2009
Remember: 2.0+2.0!=4.0. I think this is just roundoff error, .245*1000 can be smaller then 245.
edit: Solution: define new class for double and make new operator== which will do something like that:
  1. if(a<b*(1+epsilon) && a>b*(1-epsilon)) return true;
  2. return fals;
where epsilon is desired precision (10**-10 would be good).
Last edited by Zjarek; Oct 16th, 2009 at 6:53 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster
 
0
  #5
Oct 17th, 2009
constructing a whole class to solve such a problem!
is nt there any simpler solutions
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso
 
0
  #6
Oct 17th, 2009
Originally Posted by I_Empire View Post
constructing a whole class to solve such a problem!
is nt there any simpler solutions
http://www.parashift.com/c++-faq-lit...html#faq-29.17
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 10
Reputation: I_Empire is an unknown quantity at this point 
Solved Threads: 0
I_Empire I_Empire is offline Offline
Newbie Poster
 
0
  #7
Oct 23rd, 2009
ok i did what the website told me to do , writing a function to check if two double numbers are equal or not , this solved the first problem when the input is .23 for eample (see the second picture in my first post)

but it did not solve the second problem (see the third picture in my first post) : floor(2345) = 2344

this is a problem with the floor function .

any suggestions !
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC